Question: INTRO JAVA I need help completing two methods below. public class ListArray implements Comparable { private Object[] array; private int numElements; /**Constructors*/ /** * Default

INTRO JAVA I need help completing two methods below. public class ListArray implements Comparable> {

private Object[] array;

private int numElements;

/**Constructors*/

/**

* Default constructor for ListArray

* Creates an empty array of length 10

* Sets numElements to 0

*/

public ListArray() {

array = new Object[10]; numElements = 0;

}

/**

* Constructor for ListArray

* Creates an empty array of length size

* Sets numElements to 0

* @param size the initial size of the

* ListArray

*/

public ListArray(int size) {

array = new Object[size]; numElements = 0;

}

// THERE ARE METHODS BETWEEN HERE // BUT I ONLY Need help with these two methods below as they are not working correctly. I need to follow the Javadoc above each method exactly. /**

* Properly overrides Object equals

* Returns whether two ListArrays and

* the same length and store

* the same data in the same order

*/

@Override public boolean equals(Object o) {

if (this == o) {

return true;

} else if (!(o instanceof ListArray)) {

return false;

} else {

ListArray la = (ListArray) o;

return array.equals(la.array) && numElements == la.numElements;

}

}

/**

* Returns an integer value when comparing

* two ListArrays

* It returns 0 if the two ListArrays are equal

* Otherwise, compares the numElements in each array

* using Integer.compare

* Otherwise, it finds the first differing element

* in the ListArray and returns the integer given

* by calling Integer.compare on the hashCode()

* values of the two differing elements

*/

public int compareTo(ListArray la) {

if (this.equals(la)) {

return 0;

} else if (Integer.compare(this.numElements, la.numElements) < 0) {

return Integer.compare(this.numElements, la.numElements);

} else {

for(int i = 0; i < numElements; i++) {

for (int j = 0; j < la.numElements; j++) {

if (array[i] != la.array[j]) {

return Integer.compare(array[i].hashCode(), la.array[i].hashCode());

}

}

}

}

return -1;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!