Question

import java.util.Scanner; public class ListDriver {    /*    * main    *    * An...

import java.util.Scanner;

public class ListDriver {

   /*
   * main
   *
   * An array based list is populated with data that is stored
   * in a string array. User input is retrieved from that std in.
   * A text based menu is displayed that contains a number of options.
   * The user is prompted to choose one of the available options:
   * (1) Build List, (2) Add item, (3) Remove item, (4) Remove all items,
   * or (5) done. The switch statement manages calling the
   * appropriate method based on the option chosen by the user, and
   * prompts the user for further input as required Program terminates
   * if user chooses option (5). If the user chooses an option that is
   * not in the menu, a message telling the user to choose an appropriate
   * option is written to std out, followed by the options menu.
   */
   public static void main(String[] args)
   {
       String[] dataItems = {"milk","eggs","butter","apples","bread","chicken"};

       // TO DO: add code here
              
   } // end of main method
  
   /*
   * Displays the options menu, including the prompt
   * for the user
   */
   public void displayMenu()
   {
       // TODO: add code here
   }
  
   /*
   * displayStatus
   *
   * displays information about the state of
   * the list
   *
   * Preconditions: a reference to a list
   *
   * Postconditions:
   * "List empty: B" where B is either TRUE or FALSE
   * and "Size of list: n" where n is the size of
   * the list is written to std out.
   */
   public void displayStatus(ListArrayBased list)
   {
       // TO DO: add code here
   }
  
   /*
   * displayList
   *
   * Precondition: a reference to a list
   *
   * Postcondition: list is displayed on std out
   */
   public void displayList(ListArrayBased list)
   {
       // TO DO: add code here
   }
  
   /*
   * buildList
   *
   * Precondition: a reference to a list and an string array
   * of items to be address to the list
   *
   * Postcondition: items stored the string array are added
   * to the list.
   */
   public void buildList(ListArrayBased list, String[] items)
   {
       // TO DO: add code here
   }

   /*
   * addItem
   *
   * Precondition: a reference to a list, a String
   * representing a grocery item, and the integer
   * pos is the position in the list
   *
   * Postcondition: an item is added to the list at
   * position pos.
   */
   public void addItem(ListArrayBased list, String item, int pos)
   {          
       // TO DO: add code here      
   }
  
   /*
   * removeItem
   *
   * Precondition: a reference to a list and
   * int pos;
   *
   * Postcondition: item is removed from the list by its
   * position pos.
   */
   public void removeItem(ListArrayBased list, int pos)
   {      
       // TO DO: add code here      
   }
  
   /*
   * removeAllItems
   *
   * Precondition: a reference to a list
   *
   * Postcondition: all items currently stored in the list
   * are removed
   */
   public void removeAllItems(ListArrayBased list)
   {
       // TO DO: add code here
   }
      
} // end of class ListArrayBasedDriver

Homework Answers

Answer #1

Implementation in JAVA:

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class ListDriver {
  
     
   static Scanner s= new Scanner(System.in);
  
   static ListArrayBased<String> list= new ListArrayBased<>(20);
  
  
   public static void main(String[] args) {
      
       String[] dataItems = {"milk","eggs","butter","apples","bread","chicken"};
         
       while(true) {
             
           displayMenu();
           int selection=s.nextInt();
             
           switch(selection) {
           case 1:
               buildList(list,dataItems);
                 
               list.print();
              System.out.println();
              break;
                
              case 2:
                  System.out.print("Enter the item you want to add : ");
                  String item=s.next();
                  System.out.print("Enter the index on which you want to add item : ");
                  int pos=s.nextInt();
                  addItem(list,item,pos);
                 
                   list.print();
                  System.out.println();
                  break;
                    
              case 3:
                  System.out.println("Enter the index on which you want to add item : ");
                  int posi=s.nextInt();
                  removeItem(list,posi);
             
                   list.print();
                  System.out.println();
                  break;
                    
              case 4:
                  removeAllItems(list);
                   list.print();
                  System.out.println();
                  break;
                        
              case 5:
                  list.print();
                  System.out.println();
                  break;
              case 6:
                  System.out.println("Done");
                  return;
                    
                  default:
                      System.out.println("Invalid choice");
                       System.out.println("Please enter the valid choice");
                           System.out.println();
                       displayMenu();
                        
           }
             
       }
      

   }
  
   public static void displayMenu()
   {
  
       System.out.println("1 : Build List ");
       System.out.println("2 : Add Item ");
       System.out.println("3 : Remove Item ");
       System.out.println("4 : Remove All Items ");
       System.out.println("5 : View List");
       System.out.println("6 : Done");
       System.out.print("Choice: ");
       System.out.println();
      
      
      
   }
     
  
public static void displayStatus(ListArrayBased list)
   {
     
   }
     
   public static void displayList(ListArrayBased list)
   {
   list.print();
   }
     
   public static void buildList(ListArrayBased list, String[] items)
   {
   for(int i=0;i<items.length;i++) {
       list.add(items[i]);
   }
   }
     
   public static void addItem(ListArrayBased list, String item, int pos)
   {
       list.add(item, pos);
   }
     
   public static void removeItem(ListArrayBased list, int pos)
   {
   list.remove(pos);
   }
     
   public static void removeAllItems(ListArrayBased list)
   {
       list.clear();
     
   }
     
         
}


// class ListArrayBased

class ListArrayBased<E> {
  
   private E listArray[]; // Array holding list elements
   private static final int DEFAULT_SIZE = 10; // Default size
   private int maxSize; // Maximum size of list
   private int listSize; // Current # of list items
   // Position of current element
  
   ListArrayBased(int size) {
       maxSize = size;
       listSize = 0;
       listArray = (E[])new Object[size]; // Create listArray
       }
  
  
   ListArrayBased() {
       this(DEFAULT_SIZE);
       }
  
  
   public void clear() // Reinitialize the list
   { listSize = 0;
  
   } // Simply reinitialize values
  
   public int size() {
       return listSize;
   }
  
  
   public void add(E it,int pos) {
       if (listSize >= maxSize && pos>=listSize) {
           throw new NoSuchElementException();
       }
          
          
      
       ArrayList<E> list= new ArrayList<E>();
      
       for(int i=0;i<listSize;i++) {
           list.add(listArray[i]);
       }
      
       int j=0;
       for(int i=0;i<=listSize;i++) {
          
           if(i==pos-1) {
               listArray[j]=it;
           }
           else {
               listArray[i]=list.get(j);
               j++;
           }
          
       }
      
       listSize++;   
         
       }
  
   public boolean add(E it) {
       if (listSize >= maxSize) return false;
       // to make room
       listArray[listSize] = it;
       listSize++; // Increment list size
       return true;
       }
  
  
   public E remove() throws NoSuchElementException {
       if (0==listSize) // No current element
       throw new NoSuchElementException();
      
       E it = listArray[listSize-1]; // Copy the element
         
       listSize--; // Decrement size
       return it;
       }
  
  
   public E remove(int pos) throws NoSuchElementException {
       if ((pos<0) || (pos>=listSize-1)) // No current element
       throw new NoSuchElementException();
      
      
       if ( pos>=listSize) {
           throw new NoSuchElementException();
       }
ArrayList<E> list= new ArrayList<E>();
      
       for(int i=0;i<listSize;i++) {
           list.add(listArray[i]);
       }
      
       E it=listArray[pos];
       int j=0;
for(int i=0;i<listSize;i++) {
          
           if(i==pos-1) {
               continue;
           }
           else {
               listArray[j]=list.get(i);
               j++;
           }
          
       }
      
         
       listSize--; // Decrement size
       return it;
       }
  
   public E get(int pos) throws NoSuchElementException {
       if ((pos < 0) || (pos >= listSize)) // No current element
       throw new NoSuchElementException();
       return listArray[pos];
  
  
  
   }
  
   public void print() {
       System.out.print(" List is : [ ");
      
       for(int i=0;i<listSize;i++) {
           System.out.print(listArray[i]+", ");
       }
         
       System.out.println("]");
   }
  
  
}

SAMPLE OUTPUT:

If you have any doubt regarding this question please ask me in comments

// THANK YOU:-)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {       ...
import java.util.Scanner; public class AroundTheClock {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        int departureTime = input.nextInt();        int travelTime = input.nextInt();        int arrivalTime;            departureTime =12;            departureTime = 0;            arrivalTime = (int) (departureTime + travelTime);            (arrivalTime = (arrivalTime >=12));            if (arrivalTime = arrivalTime %12)            {            System.out.println(arrivalTime);...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...