NumberList.java
1    package arrayplay;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.util.Scanner;
6    public class NumberList {
7    
8    
9    
10   
11   
12   
13           /* 
14            * Program featuring an array to store and interactively manipulate a list of numbers. 
15            */
16   
17           // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
18           private static final int LIMIT = 1000;
19           private static int[] numbers = new int[LIMIT];
20           private static int numberOfNumbers = 0;
21           private static Scanner commandReader = new Scanner(System.in);
22   
23           public static void main(String[] args) {
24               try {
25                   // ESTABLISH THE ARRAY OF NUMBERS
26                   readNumbers();
27                   // CHECK THE DATA
28                    System.out.println("nThe original list of numbers ...");
29                    displayNumbers();
30                   // ENTER THE INTERPRETER
31                   interpreter();
32               } catch (FileNotFoundException ex) {
33                   System.out.println("The file was not found. Please think again.");
34                   System.exit(-1);
35               }
36           }
37   
38           // Assuming that the data file will be found in the public_html/data
39           // subdirectory of the user’s home directory.
40           private static Scanner establishScanner(String fn) throws FileNotFoundException {
41               String separator = File.separator;
42               String homeDirectory = System.getProperty("user.home");
43               String path = homeDirectory + separator + "public_html" + separator + "data" + separator;
44               String fullFileName = path + fn;
45               return new Scanner(new File(fullFileName));
46           }
47   
48           private static void readNumbers() throws FileNotFoundException {
49               Scanner scanner = establishScanner("NumberSet.txt");
50               while (scanner.hasNext()) {
51                   numbers[numberOfNumbers] = scanner.nextInt();
52                   numberOfNumbers = numberOfNumbers + 1;
53               }
54           }
55   
56           private static void displayNumbers() {
57               for (int x = 0; x < numberOfNumbers; x = x + 1) {
58                   System.out.println(numbers[x]);
59               }
60           }
61   
62           private static void interpreter() {
63               System.out.print(">>> ");
64               String command = commandReader.next();
65               if (command.equalsIgnoreCase("DISPLAY")) {
66                   interpreterDisplayCommand();
67               } else if (command.equalsIgnoreCase("PRINT")) {
68                   interpretPrintCommand();
69               } else if (command.equalsIgnoreCase("SWAP")) {
70                   interpretSwapCommand();
71               } else if (command.equalsIgnoreCase("ADD")) {
72                   interpretAddCommand();
73               } else if (command.equalsIgnoreCase("HELP")) {
74                   interpretHelpCommand();
75               } else if (command.equalsIgnoreCase("EXIT")) {
76                   System.exit(0);
77               } else {
78                   System.out.println("### Unrecognizable command: " + command);
79               }
80               interpreter();
81           }
82   
83           private static void interpreterDisplayCommand() {
84               displayNumbers();
85           }
86   
87           private static void interpretPrintCommand() {
88               String operand = commandReader.next();
89               if (operand.equalsIgnoreCase("FIRST")) {
90                   System.out.println(numbers[0]);
91               } else if (operand.equalsIgnoreCase("LAST")) {
92                   System.out.println(numbers[numberOfNumbers - 1]);
93               } else {
94                   int index = Integer.valueOf(operand);
95                   System.out.println(numbers[index - 1]);
96               }
97           }
98   
99           private static void interpretHelpCommand() {
100              System.out.println("HELP - display a menu of commands");
101              System.out.println("DISPLAY - display the list of numbers");
102              System.out.println("PRINT - print a number (FIRST;LAST;nth)");
103              System.out.println("SWAP - exchange two elements (nth;mth)");
104              System.out.println("ADD - add a number to the list (FIRST;LAST)");
105              System.out.println("EXIT - terminate execution of the program");
106          }
107  
108          private static void interpretSwapCommand() {
109              int position1 = commandReader.nextInt();
110              int position2 = commandReader.nextInt();
111              int temp = numbers[position1 - 1];
112              numbers[position1 - 1] = numbers[position2 - 1];
113              numbers[position2 - 1] = temp;
114          }
115  
116          private static void interpretAddCommand() {
117              String position = commandReader.next();
118              if (position.equalsIgnoreCase("LAST")) {
119                  addLast();
120              } else if (position.equalsIgnoreCase("FIRST")) {
121                  addFirst();
122              } else {
123                  System.out.println("### invalid operand for add command");
124              }
125              numberOfNumbers = numberOfNumbers + 1;
126          }
127  
128          private static void addLast() {
129              numbers[numberOfNumbers] = commandReader.nextInt();
130          }
131  
132          private static void addFirst() {
133              for (int x = numberOfNumbers; x > 0; x = x - 1) {
134                  numbers[x] = numbers[x - 1];
135              }
136              numbers[0] = commandReader.nextInt();
137          }
138      }
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150