Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago