0

I'm sitting on an assignment for university and I'm at a point, where I fear I haven't really understood something fundamental in the concecpt of Java or OOP altogether. I'll try to make it as short as possible (maybe it's sufficient to just look at the 3rd code segment, but I just wanted to make sure, I included enough detail). I am to write a little employee management. One class within this project is the employeeManagement itself and this class should possess a method for sorting employees by first letter via bubblesort.

I have written 3 classes for this: The first one is "Employee", which contains a name and an ID (a running number) , getter and setter methods and one method for checking whether the first letter of one employee is smaller (lower in the alphabet) than the other. It looks like this:

static boolean isSmaller(Employee source, Employee target) {
    char[] sourceArray = new char[source.name.length()];
    char[] targetArray = new char[target.name.length()];

    sourceArray = source.name.toCharArray();
    targetArray = target.name.toCharArray();

    if(sourceArray[0] < targetArray[0])
        return true;
    else 
        return false;
}

I tested it and it seems to work for my case. Now there's another class called EmployeeList and it manages the employees via an array of employees ("Employee" objects). The size of this array is determined via constructor. My code looks like this:

public class EmployeeList {
/*attributes*/
private int size;
private Employee[] employeeArray;

/* constructor */
public EmployeeList(int size) {
    this.employeeArray = new Employee[size];
}
 /* methods */
public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}
/* adds employee to end of the list. Returns false, if list is too small */
boolean add(Employee m) {
    int id = m.getID();
    if (id > employeeArray.length) {
        return false;
    } else {
        employeeArray[id] = m;
        return true;
    }

}
/* returns employee at certain position */
Employee get(int index) {
    return employeeArray[index];
}
/* Sets employee at certain position. Returns null, if position doesn't exist. Else returns old value. */
Employee set(int index, Employee m) {
    if (employeeArray[index] == null) {
        return null;
    } else {
        Employee before = employeeArray[index];
        employeeArray[index] = m;
        return before;
    }
}

Now comes my real problem: In a third class called "employeeManagement" I am supposed to implement the sorting algorithm. The class looks like this:

public class EmployeeManagement {
private EmployeeList ml = new EmployeeList(3);

public boolean addEmployee(Employee e) {
    return ml.add(e);
}

public void sortEmployee() {
    System.out.println(ml.getSize()); // I wrote this for debugging, exactly here lies my problem
    for (int n = ml.getSize(); n > 1; n--) {
        for (int i = 0; i < n - 1; i++) {
            if (Employee.isSmaller(ml.get(i), ml.get(i + 1)) == false) {
                Employee old = ml.set(i, ml.get(i + 1));
                ml.set(i+1, old);
            }
        }
    }
}

The "println" before my comment returns "0" in console... I am expecting "3" as this is the size I gave the "EmployeeList" as parameter of the constructor within my "EmployeeManagement" class. Where is my mistake ? And how can I access the size of the object I created in the "EmployeeManagement" class (the "3") ? I'm really looking forward to your answers!

Thanks, Phreneticus

Phreneticus
  • 349
  • 2
  • 11
  • 2
    `isSmaller` is much more easily (and efficiently) written as `return source.name.charAt(0) < target.name.charAt(0);`. – Andy Turner Apr 17 '17 at 21:28
  • ...but then you also have the problem where the names start with the same character, or are zero-length. Is there a good reason not to use `return source.name.compareTo(target.name);`? – Andy Turner Apr 17 '17 at 21:36
  • Like Elliott's answer says, the problem probably doesn't have to do with your understanding of OOP, but based on some other things I suspect that you do have some misunderstanding. In `isSmaller` it looks like you think you are pre-allocating space for the arrays with `new char[source.name.length()]` but you don't need to do that and in fact that array is just discarded. A `char[]` in Java is actually more like a pointer in some other languages. `toCharArray` already allocates an array on the heap and returns a reference to it. See e.g. http://stackoverflow.com/q/40480/2891664 – Radiodef Apr 17 '17 at 21:37

2 Answers2

2

You are not storing size in your constructor. Something like,

public EmployeeList(int size) {
    this.employeeArray = new Employee[size];
    this.size = size; // <-- add this.
}

Also, setSize isn't going to automatically copy (and grow) the array. You will need to copy the array, because Java arrays have a fixed length. Finally, you don't really need size here since employeeArray has a length.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • (But, then, why store `size` at all, rather than just using `employeeArray.length`) – Andy Turner Apr 17 '17 at 21:30
  • 1
    @AndyTurner I'm answering **why** OP's code currently gives a `0`. – Elliott Frisch Apr 17 '17 at 21:31
  • Sure, I'm not criticizing your answer, I'm just pointing out that there's no need to store it, especially if you are going to grow the array. – Andy Turner Apr 17 '17 at 21:31
  • Thanks alot for the unbelievably fast solution ! I was under the impression that Employee[size] would automatically change the size attribute of the class, but now that I think of it that's bs of course. – Phreneticus Apr 17 '17 at 21:44
1

The size variable you are calling is the class field. If you take a quick look at your code, the getter is getting the field (which is initialized as zero when created). The size you are using it. The good way of doing it would be to get the size of the array in the getter like this:

public int getSize() {
   return employeeArray.length;
}

This would return the size of the array in the object.

Misys
  • 127
  • 12
Etienne Berube
  • 547
  • 1
  • 7
  • 21