Q: Implement an equals method for the ADT list that
returns true when the entries in one list equal the entries in a
second list. In particular, add this method to the class AList. The
following is the method header:
public boolean equals (Object other)
public class AList<T>{
private T list[];
private int capacity = 100;
private int numOfEnteries =0;
public AList(){
list = (T[])new Object[capacity + 1];
}
public void add(T element){
numOfEnteries++;
if (numOfEnteries >capacity)
System.out.println ("Exceed limit");
else
list[numOfEnteries] = element;
}
public boolean isFull(){
return numOfEnteries==capacity+1;
}
public boolean isEmpty(){
return numOfEnteries==0;
}
public void add (int position, T element){
if (position <0 || position >capacity+1){
System.out.println("Error");
}
else{
if (position >numOfEnteries){
numOfEnteries++;
list[numOfEnteries] = element;
}
else{
for (int i = numOfEnteries+1 ;i>=position; i--){
list[i]= list[i-1];
}
list[position] = element;
}
}
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AList other = (AList) obj;
if (numOfEnteries !=
other.numOfEnteries)
return false;
for(int i=0; i<numOfEnteries; i++)
{
if(list[i] == null
&& other.list[i] == null) {
continue;
}
if(list[i] == null
|| other.list[i] == null) {
return
false;
}
if(!list[i].equals(other.list[i]))
{
return
false;
}
}
return true;
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.