// Fill-in a list of numbers until it is complely full, then // remove the items from the list one by one counting them to determine // the length of the list, and store the removed items in a text file. import java.util.Scanner; import java.io.*; interface MyListInterface { public void fill (int item) throws MyListFullException; public int remove () throws MyListEmptyException; public int size(); public boolean empty(); public boolean full(); public int lastNumber () throws MyListEmptyException; } class MyListEmptyException extends Exception { public MyListEmptyException (String message) { System.out.println (message); } } class MyListFullException extends Exception { public MyListFullException (String message) { System.out.println (message); } } class MyListClass implements MyListInterface { final int MAXSIZE = 10 ; int size; int[] list; private int current = -1; public MyListClass () { size = MAXSIZE; list = new int[size]; } public boolean empty () { return (current == -1); } public boolean full () { return (current == size - 1); } public void fill (int number) throws MyListFullException { if (size() == size) throw new MyListFullException ("The list is completely filled up."); current++; list[current] = number; } public int remove () throws MyListEmptyException { if (empty()) throw new MyListEmptyException ("The list is completely empty."); int i = list[current]; current--; return i; } public int lastNumber () throws MyListEmptyException { if (empty()) throw new MyListEmptyException ("The list is empty."); int i = remove(); try { fill(i); } catch (MyListFullException e) { System.out.println ("The stack is full."); } return i; } public int size () { return current + 1; } } class Summary1 { public static void main (String[] args) throws IOException { Scanner scan = new Scanner (System.in); PrintWriter outputFile = new PrintWriter(new FileWriter("summary1.txt")); MyListClass example = new MyListClass(); System.out.print ("Enter next integer: "); //System.out.flash(); int i = scan.nextInt(); /* catching two exceptions, MyListFullException and MyListEmptyException */ try { for (int j = 0; j < 100; j++) { example.fill(i); System.out.println (example.lastNumber() + " added to the list"); System.out.print ("Enter next integer: "); // System.out.flash(); i = scan.nextInt(); } } catch (MyListFullException e) { System.out.println ("The list is full -- no more numbers can be added to it."); } catch (MyListEmptyException e) { System.out.println ("The list is empty."); } // Determine the lenght of the list, example using the recursive // method, computeLength. int mylength = computeLength(example.list, example.size); System.out.println ("The length of the list is " + mylength); try { System.out.println ("The last element added to the list was " + example.lastNumber()); for (int j = 0; j < 100; j++) { System.out.println (example.lastNumber() + " removed from the list"); outputFile.println (example.remove() + " removed from the list"); } } catch (MyListEmptyException e) { System.out.println ("The list is empty."); } outputFile.close(); } static int computeLength (int[] l, int s) { if (s == 0) return 0; else return (1 + computeLength (l, s - 1)); } }