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...
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.       ...
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.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);...
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 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(); } }
The working code: //import java.util.Hashtable; public class HashingExample1 {    private int[] hashTable = new int[10];...
The working code: //import java.util.Hashtable; public class HashingExample1 {    private int[] hashTable = new int[10];       public int hash(int ele){        return ele % this.hashTable.length;    }    public boolean insert(int ele){        int pos = hash(ele);        System.out.println("Hashing key " + pos);        if(this.hashTable[pos] == 0)            this.hashTable[pos] = ele;        else{            for(int i = pos + 1; i < this.hashTable.length; i++){                if(this.hashTable[i]...
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];            ...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r = new Random(seed); } public int rollD6() { return r.nextInt(6) + 1; } public int score() { int roll1 = rollD6(); int roll2 = rollD6(); return scoreWithNumbers(roll1, roll2); }    // You will need to write a static method named scoreWithNumbers. // scoreWithNumbers returns a score based on the values // of its inputs, as such: // // - If both inputs are 1...