Question

In: Computer Science

/* * File: ListDriver.java * Assignment: Lab03 * Author: * Date: * Course: * Instructor: *...

/*
* File: ListDriver.java
* Assignment: Lab03
* Author:
* Date:
* Course:
* Instructor:
*
* Purpose: Test an implementation of an array-based list ADT.
* To demonstrate the use of "ListArrayBased" the project driver
* class will populate the list, then invoke various operations
* of "ListArrayBased".
*/
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

Solutions

Expert Solution

/////////////////////////////////////////////// ListArrayBased.java


public class ListArrayBased {
   private String [] itrms;// array of string
   private int index;// current index
   private int size;// size of array
  
   // constructor
   public ListArrayBased(int n){
       this.itrms=new String[n];
       this.size=n;
       // initialize
       for(int i=0;i<n;i++){
           this.itrms[i]="";
       }
       this.index=-1;
   }
  
   // set value at position pos
   public void setValue(String str,int pos){
       if(this.index<this.size){
           this.itrms[pos]=str;
           this.index++;
       }      
   }
  
   // add value at position pos
   public void addValue(String str,int pos){
       if(this.index<this.size){
           // increase the positional value
           // a b c
           // n a b c [ a b c increase positioned ]
           for(int i=this.index;i>pos;i--){
               this.itrms[i]=this.itrms[i-1];
           }
           this.itrms[pos]=str;
           this.index++;
       }      
   }
  
   // get value at position pos
   public String getValue(int pos){
       return(this.itrms[pos]);
   }
  
   // remove value at position pos
   public String removeAt(int pos){
       String ret="";
       if(this.index>-1){
           // get element from position
           ret=this.itrms[pos];
          
           // decrease the positional value
           for(int i=pos;i<this.index;i++){
               this.itrms[i]=this.itrms[i+1];
           }
           // decrease elements number
           this.index--;          
       }      
       return ret;
   }
  
   // remove all elements
   public void removeAll(){
       String ret="";
       this.index=-1;
   }
  
   // get current size
   public int getCurSize(){
       return this.index+1;
   }
  
   // if size is empty or not
   public boolean isEmpty(){
       return this.index==-1;
   }
}

////////////////////////////////////////////// ListDriver.java


/*
* File: ListDriver.java
* Assignment: Lab03
* Author:
* Date:
* Course:
* Instructor:
*
* Purpose: Test an implementation of an array-based list ADT.
* To demonstrate the use of "ListArrayBased" the project driver
* class will populate the list, then invoke various operations
* of "ListArrayBased".
*/
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"};
ListArrayBased list=new ListArrayBased (20);
Scanner sc= new Scanner(System.in);

for(;;){
   // show status
   displayStatus(list);
   // show list
   displayList(list);
   // show menu
   displayMenu();
   System.out.print("Enter option : ");
   int option=sc.nextInt();
   // for wrong option
   if(option<1 || option>5){
       System.out.print("Wrong option , please choose correct option");
   }
   else{
       // each option
       if(option==1){
           buildList(list,dataItems);
       }
       if(option==2){
           System.out.print("Enter value : ");
           String str=sc.next();
           addItem(list,str,list.getCurSize());               
       }
       if(option==3){
           System.out.print("Remove at : ");
           int pos=sc.nextInt();
           removeItem(list,pos);
       }
       if(option==4){
           removeAllItems(list);
       }
       if(option==5){
           break;
       }
   }
     
}
  
} // end of main method
  
/*
* Displays the options menu, including the prompt
* for the user
*/
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) done");
     
}
  
/*
* 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 static void displayStatus(ListArrayBased list)
{
   System.out.println("List Empty : "+list.isEmpty());
   System.out.println("Size of List : "+(list.getCurSize()));
}
  
/*
* displayList
*
* Precondition: a reference to a list
*
* Postcondition: list is displayed on std out
*/
public static void displayList(ListArrayBased list)
{
for(int i=0;i<list.getCurSize();i++){
   System.out.println("Element "+(i)+" : "+list.getValue(i));
}
}
  
/*
* 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 static void buildList(ListArrayBased list, String[] items)
{
   int curSize=list.getCurSize();
for(int i=0;i<items.length;i++){
   list.setValue(items[i], i+curSize);
}
}

/*
* 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 static void addItem(ListArrayBased list, String item, int pos)
{
   list.addValue(item, pos);
}
  
/*
* removeItem
*
* Precondition: a reference to a list and
* int pos;
*
* Postcondition: item is removed from the list by its
* position pos.
*/
public static void removeItem(ListArrayBased list, int pos)
{
list.removeAt(pos);
}
  
/*
* removeAllItems
*
* Precondition: a reference to a list
*
* Postcondition: all items currently stored in the list
* are removed
*/
public static void removeAllItems(ListArrayBased list)
{
list.removeAll();
}
  
} // end of class ListArrayBasedDriver

///////////////////////////////////////////


Related Solutions

Course assignment is: Choose a hypothesis, test your hypothesis with an analysis of the data file,...
Course assignment is: Choose a hypothesis, test your hypothesis with an analysis of the data file, and discuss the results by including both descriptive and inferential statistics. My hypothesis addresses risk-taking in married vs single Firefighters - H₀ = Null Hypothesis: the amount of risk that married firefighters take is = to the amount of risk that single firefighters take. H₁ = Alternate Hypothesis: The amount of risk that single firefighters take is > the amount of risk that married...
If you are the instructor of a course, how would you structure the course and its...
If you are the instructor of a course, how would you structure the course and its content to incorporate the anagogical approach in a college class. Instead of traditional lectures in class, what would you suggest as learning activities that would be challenging but effective in acquiring knowledge of skills in the course?
An instructor for a test preparation course claims that the course will improve the test scores...
An instructor for a test preparation course claims that the course will improve the test scores of students. The table shows the critical reading scores for 14 students the first two times they took the test. Before taking the test for the second​ time, the students took the​ instructor's course to try to improve their critical reading test scores. At a​ = 0.01, is there enough evidence to support the​ instructor's claim? Complete parts​ (a) through​ (f). number of students:14....
C++ Start with your Date class in the Date.cpp file (from Date01B assignment or whatever) Name...
C++ Start with your Date class in the Date.cpp file (from Date01B assignment or whatever) Name the new Date file Date03.cpp Add the following constructors to the date class create a constructor that takes 3 integers in the order month, day, year assigns the month, day, year parameters to the corresponding data items. Use the ‘setter’ to assign the values Add a line cout << “in constructor with 3 ints\n” in the constructor a 'default' constructor - no arguments Add...
The instructor of an English course is interested in knowing if there is a difference in...
The instructor of an English course is interested in knowing if there is a difference in the mean grades between the Peterborough and Oshawa classes. Summary data for these classes are as follows: Ptbo Class Osh Class Mean 73.00 67.00 N   101 61 SD 10.50. 11.00 Is there a significant difference in the average grades of these two classes? Compute the power of the findings, explaining what it means (7 grades) What is the effect size? Interpret the effect size
Assignment #1: Descriptive Statistics Data Analysis Plan Identifying Information Student (Full Name): Class: Instructor: Date: Scenario:...
Assignment #1: Descriptive Statistics Data Analysis Plan Identifying Information Student (Full Name): Class: Instructor: Date: Scenario: Please write a few lines describing your scenario and the four variables (in addition to income) you have selected. Use Table 1 to report the variables selected for this assignment. Note: The information for the required variable, “Income,” has already been completed and can be used as a guide for completing information on the remaining variables. Table 1. Variables Selected for the Analysis Variable...
Assignment 2 (worth 10% of the final course grade - due date July 23, 2019) Prof....
Assignment 2 (worth 10% of the final course grade - due date July 23, 2019) Prof. Sherif Saad Purpose The purpose of this assignment is to help you to practice working with C String, arrays, and pointers Learning Outcomes ● Develop skills with pointers ● Learn how to traverse C String using pointers ● Passing Strings to functions ● Manipulate C String ● Develop algorithm design skills Problem Overview You will implement a basic string manipulation library called xstring.h. This...
the instructor of an english course is interested if there is a difference in mean grade...
the instructor of an english course is interested if there is a difference in mean grade on the tests between 2 classes Mean 73.00 67.00 N 101 61 SD 10.50 11.00 Is there a significant difference in the average grades of these two classes? Compute the power of the findings, explaining what it means (7 grades) What is the effect size? Interpret the effect size show all work
Instructor:         Williams Course:               Contract Law Project:               Con
Instructor:         Williams Course:               Contract Law Project:               Contract Project/Spring 2018 Facts Kobe owns a consulting business, Kobe’s Basketball, Inc., but he wants to purchase and operate a restaurant. He meets a businessman name LeBron who owns a restaurant called King’s Pizza, LLC. He has two restaurants locations, one in Houston and one in Cleveland. LeBron wans to sell the one in Houston, so he can move back to Cleveland to operate his Cleveland restaurant full time. LeBron’s restaurant,...
This assignment marks the last part of the course project. In this section, you are to again through the use and application of credible sources author an original discussion post defining
  This assignment marks the last part of the course project. In this section, you are to again through the use and application of credible sources author an original discussion post defining, discussing and applying the HR topic posed. Your discussion board submission address the following topic points: Define and discuss the importance of the performance evaluation process using credible sources to support your findings. Using real examples from businesses or HR-related sources found on the internet identify and discuss...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT