Question

In: Computer Science

Create the following java program with class list that outputs: //output List Empty List Empty List...

Create the following java program with class list that outputs:

//output

List Empty
List Empty
List Empty
Item not found
Item not found
Item not found

Original list
Do or do not. There is no try. 

Sorted Original List
Do There do is no not. or try. 

Front is Do
Rear is try.
Count is 8
Is There present? true
Is Dog present? false

List with junk
junk Do or moremorejunk do not. There is no try. morejunk 
Count is 11

List with junk removed
Do or do not. There is no try. 
Count is 8

List Full
List Full
List Full
List Full
List Full
List Full

After filling List
dummy dummy dummy dummy dummy dummy dummy Do or do not. There is no try. 
Count is 15

After removing dummy
Do or do not. There is no try. 
Count is 8

// 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("\nFront 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";
}

Solutions

Expert Solution

Following is MyList implementation for your output as:

Java Code:

import java.util.Comparator;

public class MyList {

   private Node start;
   private Node end;
   private int size;
   private int capacity;

   public MyList(int capacity) {
       start = null;
       end = null;
       size = 0;
       this.capacity = capacity;
   }

   /* Function to check if list is empty */
   public boolean isEmpty() {
       return start == null;
   }

   public void removeFront() {
       if (isEmpty()) {
           System.out.println("List Empty");
           return;
       } else {
           start = start.next;
           size--;
           return;
       }
   }

   public void removeRear() {
       if (size == 0) {
           System.out.println("List Empty");
           return;
       }
       if (start.next == null) {
           start=null;
           end=null;
           size=0;
       }
       Node node = start.next;
       while (node.next != null) {
           node = node.next;
       }
       node.next = null;
       end = node;
       size--;
   }

   public void removeItem(String key) {
      
       if (size == 0) {
           System.out.println("List Empty");
           return;
       }
       if (start.next == null) {
           if (start.key.equals(key)) {
               start=null;
               end=null;
               size=0;
           } else {
               System.out.println("");
           }
           return;
       }
       Node node = start;
       Node nodeNext = node.next;
       while (nodeNext.next != null) {
           if (nodeNext.key.equals(key)) {
               node.next=nodeNext.next;
           } else {
               node = node.next;
               nodeNext=nodeNext.next;
           }
       }
       if (nodeNext.key.equals(key)) {
           node.next=null;
           end = node;
       }
       size--;
   }

   /**
   * addToFront
   * @param ele
   */
   public void addToFront(String ele) {
       if (size == capacity) {
           System.out.println("List Full");
           return;
       } else {
           Node node = new Node(ele, null);
           size++;
           if (start == null) {
               start = node;
               end = start;
           } else {
               node.next = start;
               start = node;
           }
       }
   }

   /**
   * addAfterItem
   * @param key1
   * @param key2
   */
   public void addAfterItem(String key1, String key2) {
       Node tmp = start;
Node refNode = null;
/**
* Traverse till given element
*/
while(true){
if(tmp == null){
break;
}
if(tmp.key.equals(key1)){
//found the key1 node, add after add k2 node
refNode = tmp;
break;
}
tmp = tmp.next;
}
if(refNode != null){
//add element after the target node
Node newNode = new Node(key2,tmp.next);
if(tmp == end){
end = newNode;
}
tmp.next=newNode;
size++;

} else {
System.out.println("");
}

   }
  
   /**
   * addBeforeItem
   * @param key1
   * @param key2
   */
   public void addBeforeItem(String key1, String key2) {
       Node current = start;
   //check here
   Node prev = null;
   if (start != null) {
   while (current != null) {
   if (current.key.equals(key1)) {
   Node newNode = new Node(key2,current);
   //check here
   if (prev != null) {
   prev.next = newNode;
   size++;
   }
   return;
   }
   //check here
   prev = current;
   current = current.next;
   }
   }

   }

   public void addToRear(String ele) {
       if (size == capacity) {
           System.out.println("List Full");
       } else {
           Node node = new Node(ele, null);
           size++;
           if (start == null) {
               start = node;
               end = start;
           } else {
               end.next = (node);
               end = node;
           }
       }
   }

   public String getFront() throws Exception {
       if (isEmpty()) {
           System.out.println("List Empty");
           return null;
       } else {
           return start.key;
       }
   }

   public String getRear() {
       if (isEmpty()) {
           System.out.println("List Empty");
           return null;
       } else {
           return end.key;
       }
   }

   public int askCount() {
       return size;
   }

   public boolean isPresent(String key) {
       if (size == 0) {
           System.out.println("List Empty");
           return false;
       }
       if (start.next == null) {
           System.out.println();
           if (start.key.equals(key)) {
               return true;
           } else {
               return false;
           }
       }
       Node node = start;
       node = start.next;
       while (node.next != null) {
           if (node.key.equals(key)) {
               return true;
           } else {
               node = node.next;
           }
       }
       if (node.key.equals(key)) {
           return true;
       } else {
           return false;
       }
   }

   public void print(String msg) {
       System.out.println(msg);
       if (size == 0) {
           System.out.println("List Empty");
           return;
       }
       if (start.next == null) {
           System.out.println(start.key);
           return;
       }
       Node node = start;
       System.out.print(start.key + " ");
       node = start.next;
       while (node.next != null) {

           System.out.print(node.key + "");
           node = node.next;
       }
       System.out.println(node.key + "\n");
   }

   public void printSorted(String string) {
      
   }

   /**
   * Node as nested class
   */
   private static class Node implements Comparator<String> {

       /*-----------------------*
       * Variable Declarations *
       *-----------------------*/
       private String key; // The data in the node
       private Node next; // The next node

       /**
       * Constructs a new TreeNode
       *
       * @param key
       */
       private Node(String key) {
           this(key, null);
       }

       /**
       * Constructs a new TreeNode
       *
       * @param key
       * @param next
       */
       private Node(String key, Node next) {
           this.key = key;
           this.next = next;
       }

       @Override
       public int compare(String key1, String key2) {
           if (key1 == key2) {
       return 0;
       }
       if (key1 == null) {
       return -1;
       }
       if (key2 == null) {
       return 1;
       }
       return key1.compareTo(key2);
       }

   }

}

public class AssignmentThree {
   public static void main(String[] args) throws Exception {
       MyList myList = new MyList(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("\nFront 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";
}

Output:

List Empty
List Empty
List Empty


Original list
Do ordonot.Thereisnotry.


Front is Do
Rear is try.
Count is 8
Is There present? true
Is Dog present? false
List with junk
junk Doormoremorejunkdonot.Thereisnotry.morejunk

Count is 11

If you have any query please comment. Thanks!


Related Solutions

Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...
Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort()...
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. Do not use recursion to implement either of these operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT