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
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
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started