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