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
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:-)
Get Answers For Free
Most questions answered within 1 hours.