In: Computer Science
/*
* 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
/////////////////////////////////////////////// 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
///////////////////////////////////////////