Question 1. Write a method for the AList class that returns the first position o
ID: 3727095 • Letter: Q
Question
Question 1.
Write a method for the AList class that returns the first position of a given object in the list with the method header below.
public int getPosition(T anObject)
Question 2.
Write a method for the AList class that moves the last item in the list to the beginning of the list.
public void moveToBeginning()
Question 3. Write an equals method for the AList class to override the method inherited from Object. Two lists are logically the same if they contain the same elements in the same order.
Question 4.
Have the AList class implement the Comparable interface.
Write the compareTo method to order lists this way:
first by size: a list that has fewer elements is smaller
second by the first element: if the lists have the same size, then compare the order of the first element on the list
Note, to test your AList class, use these class and method headers:
public class AList<T extends Comparable> implements ListInterface<T>, Comparable<AList<T>>
public int compareTo(AList<T> otherList)
You also need to change the lines in the constructor and toArray methods from using Object to using Comparable:
T[] tempList = (T[]) new Comparable[initialCapacity + 1];
T[] result = (T[]) new Comparable[numberOfEntries]; // Unchecked cast
Examples:
List A: 1, 5, 6, 8, 10
List B: 2, 4, 3, 9, 7
In this example, List A is smaller because the lists are the same size and the first element of Lisa A is smaller than the first element of List B.
List A: 1, 2, 3, 10, 9
List B: 2, 4, 3, 7
In this example, List B is smaller because it has fewer elementss.
List A: 1, 4, 6, 2, 3
List B: 1, 5, 7, 2, 9
In this example, the compareTo method would return 0 because the lists are the same size and their first element is the same. (Note that it's true the lists aren't equal!)
Driver program:
import java.util.Arrays;
public class Homework04Driver {
public static void main(String[] args) {
// setting up an ArrayBag to test
System.out.println("TESTING BAG");
ArrayBag<Integer> numbersBag = new ArrayBag<Integer>();
numbersBag.add(1);
numbersBag.add(2);
numbersBag.add(1);
numbersBag.add(4);
numbersBag.add(3);
System.out.println("The bag contains[1, 2, 1, 4, 3]
"
+
// Q7: getPosition
// Note: this driver assumes you return -1 if an element
is not in the list;
// it would also be valid to throw an exception
System.out.println(" ***Q7***");
System.out.println("Position is 1: " +
produceList.getPosition("banana"));
System.out.println("Position is 3: " +
produceList.getPosition("grape"));
System.out.println("Position is -1: " +
produceList.getPosition("mango"));
//Q8: moveToBeginning
System.out.println(" ***Q8***");
System.out.println("List contains [banana, date, grape,
eggplant, jicama, grape] "
+
Arrays.toString(produceList.toArray()));
produceList.moveToBeginning();
System.out.println("List contains [grape, banana, date,
grape, eggplant, jicama] "
+
Arrays.toString(produceList.toArray()));
produceList.clear();
produceList.moveToBeginning();
System.out.println("List contains [] "
+
Arrays.toString(produceList.toArray()));
// Q9: equals
System.out.println(" ***Q9***");
produceList.add("banana");
produceList.add("potato");
produceList.add("jicama");
produceList.add("grape");
AList<String> foodList = new AList<String>();
foodList.add("banana");
foodList.add("potato");
foodList.add("jicama");
System.out.println("Result is false: " +
produceList.equals(foodList));
foodList.add("grape");
System.out.println("Result is true: " +
produceList.equals(foodList));
foodList.add("squash");
System.out.println("Result is false: " +
produceList.equals(foodList));
foodList.replace(1, "jicama");
foodList.replace(3, "banana");
System.out.println("Result is false: " +
produceList.equals(foodList));
System.out.println("The list contains[banana, potato,
jicama, grape] "
+
Arrays.toString(produceList.toArray()));
System.out.println("The second list contains[jicama,
potato, banana, grape, squash] "
+
Arrays.toString(foodList.toArray()));
/* un-comment this section if doing the extra credit */
/*
System.out.println(" TESTING EXTRA CREDIT");
AList<Integer> numberListA = new AList<Integer>();
numberListA.add(1);
numberListA.add(5);
numberListA.add(6);
numberListA.add(8);
numberListA.add(7);
numberListA.add(9);
AList<Integer> numberListB = new AList<Integer>();
numberListB.add(2);
numberListB.add(4);
numberListB.add(3);
numberListB.add(9);
numberListB.add(7);
System.out.println("List B is smaller (fewer elements),
result should be positive: " + numberListA.compareTo(numberListB));
numberListA.remove(1);
numberListA.remove(1);
System.out.println("List A is smaller (fewer elements),
result should be negative: " + numberListA.compareTo(numberListB));
numberListA.clear();
numberListA.add(1);
numberListA.add(3);
numberListA.add(5);
numberListA.add(7);
numberListA.add(10);
numberListB.clear();
numberListB.add(2);
numberListB.add(4);
numberListB.add(3);
numberListB.add(9);
numberListB.add(7);
System.out.println("List A is smaller (same number of
elements, 1<2), result should be negative: " +
numberListA.compareTo(numberListB));
numberListB.remove(1);
numberListB.add(1, -2);
Explanation / Answer
If you post more than 1 question, as per chegg guidelines I have to solve only 1 question.
Ques 1.
public int getPosition(T anObject)
{
// Assuming Node class defines the node
// head points to the head of the list
Node<T> trav = head;
int pos = 1;
while( trav != null )
{
// getData() returns the value of the node
if( trav.getData() == anObject )
return pos;
// move to the next node
trav = trav.getNext();
pos++;
}
// if node is not found
return -1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.