Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java class \"LIST\" that outputs: //main: public class AssignmentThree {

ID: 3797181 • Letter: W

Question

Write a java class "LIST" that outputs:

//main:

public class AssignmentThree
{
   public static void main(String[] args)
   {
       List myList = new List(15);
      
       // Cause List Empty Message
       myList.removeFront();
       myList.removeRear();
       myList.removeItem("a");
      
       // Cause Not found message
       myList.addToFront("x");
       myList.removeItem("y");
       myList.removeItem("x");
       myList.addAfterItem("x", "z");
       myList.addBeforeItem("x", "z");
          
       // Normal behavior
       myList.addToFront("not.");
       myList.addToFront("or");
       myList.addToRear("is");
       myList.addToRear("try.");
       myList.addAfterItem("is", "no");
       myList.addBeforeItem("is", "There");
       myList.addToFront("Do");
       myList.addAfterItem("or", "do");
      
       myList.print("Original list");
       myList.printSorted("Sorted Original List");
          
       sop(" Front is " + myList.getFront());
       sop("Rear is " + myList.getRear());
       sop("Count is " + myList.askCount());
       sop("Is There present? " + myList.isPresent("There"));
       sop("Is Dog present? " + myList.isPresent("Dog"));
  
       myList.addToFront("junk");
       myList.addToRear("morejunk");
       myList.addAfterItem("or", "moremorejunk");
      
       myList.print("List with junk");
       sop("Count is " + myList.askCount());
      
       myList.removeFront();
       myList.removeRear();
       myList.removeItem("moremorejunk");
       myList.print("List with junk removed");
       sop("Count is " + myList.askCount());
       sop("");
      
       // Cause List Full message
       for(int ii = 0; ii < 10; ++ii)
       {
           myList.addToFront(DUMMY);
       }
      
       myList.addToRear(DUMMY);
       myList.addBeforeItem("no", DUMMY);
       myList.addAfterItem("There", DUMMY);
      
       myList.print("After filling List");
       sop("Count is " + myList.askCount());
      
       while(myList.isPresent(DUMMY)) myList.removeItem(DUMMY);
      
       myList.print("After removing " + DUMMY );
       sop("Count is " + myList.askCount());
   }
  
   private static void sop(String s)
   {
       System.out.println(s);
   }
      
   private static final String DUMMY = "dummy";
}

//Class List:

public class List {
   public List(int size)
   {
      
   }
   public void addToFront(java.lang.String item)
   {
      
   }
   public void addToRear(java.lang.String item)
   {
      
   }
   public void addBeforeItem(java.lang.String beforeItem,
java.lang.String item)
   {
      
   }
   public void addAfterItem(java.lang.String afterItem,
java.lang.String item)
   {
      
   }
   public java.lang.String getFront()
   {
      
   }
   public java.lang.String getRear()
   {
      
   }
   public boolean isPresent(java.lang.String item)
   {
      
   }
   public int askCount()
   {
      
   }
   public void removeFront()
   {
      
   }
   public void removeRear()
   {
      
   }
   public void removeItem(java.lang.String item)
   {
      
   }
   public void print(java.lang.String title)
   {
      
   }
   public void printSorted(java.lang.String title)
   {
      
   }
}

Explanation / Answer

The List is the base interface for all list types, and the ArrayList and LinkedList classes are two common List’s implementations.

Besides ArrayList and LinkedList, Vector class is a legacy collection and later was retrofitted to implement the Listinterface. Vector is thread-safe, but ArrayList and LinkedList are not. The following class diagram depicts the inheritance tree of the List collections.

It’s a good practice to declare a list instance with a generic type parameter, for example:

1

2

3

4

List<Object> listAnything = new ArrayList<Object>();

List<String> listWords = new ArrayList<String>();

List<Integer> listNumbers = new ArrayList<Integer>();

List<String> linkedWords = new LinkedList<String>();

Since Java 7, we can remove the type parameter on the right side as follows:

1

2

List<Integer> listNumbers = new ArrayList<>();

List<String> linkedWords = new LinkedList<>();

The compiler is able to infer the actual type parameter from the declaration on the left side.

When creating a new ArrayList using the empty constructor, the list is constructed with an initial capacity of ten. If you are sure how many elements will be added to the list, it’s recommended to specify a capacity which is large enough. Let’s say, if we know that a list contains around 1000 elements, declare the list as follows:

1

List<Integer> listNumbers = new ArrayList<>(1000);

It’s also possible to construct a list that takes elements from an existing collection, for example:

1

2

3

List<Integer> listNumberOne;  // existing collection

List<Integer> listNumberTwo = new ArrayList<>(listNumberOne);

The listNumberTwo constructed with copies of all elements from the listNumberOne.The methods add(Object), add(index, Object) and addAll() are used to add elements to the list. It requires to add elements of the same type (or sub type) as the type parameter declared by the list. For example:

1

2

3

4

5

6

7

8

9

List<String> listStrings = new ArrayList<String>();

// OK to add Strings:

listStrings.add("One");

listStrings.add("Two");

listStrings.add("Three");

// But this will cause compile error

listStrings.add(123);

Adding elements of sub types of the declared type:

1

2

3

4

5

6

List<Number> linkedNumbers = new LinkedList<>();

linkedNumbers.add(new Integer(123));

linkedNumbers.add(new Float(3.1415));

linkedNumbers.add(new Double(299.988));

linkedNumbers.add(new Long(67000));

We can insert an element into the list at a specified indexThe simplest way to sort out elements in a list is using the Collections.sort() static method which sorts the specified list into ascending order, based on the natural ordering of its elements. Here’s an example:

1

2

3

4

5

6

7

8

9

10

11

12

List<String> listStrings = new ArrayList<String>();

listStrings.add("D");

listStrings.add("C");

listStrings.add("E");

listStrings.add("A");

listStrings.add("B");

System.out.println("listStrings before sorting: " + listStrings);

Collections.sort(listStrings);

System.out.println("listStrings after sorting: " + listStrings);

Output:

1

2

listStrings before sorting: [D, C, E, A, B]

listStrings after sorting: [A, B, C, D, E]

1

2

3

4

List<Object> listAnything = new ArrayList<Object>();

List<String> listWords = new ArrayList<String>();

List<Integer> listNumbers = new ArrayList<Integer>();

List<String> linkedWords = new LinkedList<String>();

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote