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

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...
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...
**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...
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 Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT