Your task is to write three separate Java programs for this assignment, all pertaining to the storage and manipulation of lists. The first two will feature arrays. The last one will feature an ArrayList object.
Why do it?
By engaging in this programming challenge you will have an opportunity to do some array processing and some ArrayList processing, you will engage in working by analogy, and you will gain additional experience writing interpreters.
The Entrance
Review the given program, the NumberList program of the arrayplay package. Read and understand it!
Problem 1: The NumberList program of the arrayplay package.
- Enter the given program in your Java/IntelliJ world.
- Create a data file of numbers called NumberSet.txt containing between 10 and 15 integers of your own choosing. Place the file in a directory called public_html/data inside your home directory.
- Run the program, and interact with it in much the same manner as I did in the example demo.
Problem 2: The WordList program of the arrayplay package.
- By analogy with the given program, write a program called WordList to store and manipulate words, character strings, in an array. Do so within the arrayplay package.
- Create a data file of words called WordSet.txt containing between 10 and 15 words of your own choosing. Place the file in a directory called public_html/data inside your home directory.
- Run the program, and interact with it in a manner analogous to the numeric demo.
Problem 3: The WordList program of the arraylistplay package.
- By analogy with the WordList program that you just wrote in the arrayplay package, write a program called WordList in the arraylistplay package which features an ArrayList object rather than the array object. In doing so, be thinking about whether there is an easier way to do a task, such as adding at the beginning, with ArrayLists!
- Run the program, and interact with it in a manner analogous to the numeric demo.
The Exit
- The assignment is due November 15th.
- Please post your work to you work site.
- Think for a little while about this assignment and your engagement with it. What did you learn that is conceptually significant? What did you learn that is technologically useful? What is your most salient thought about the assignment and your engagement with it?
Featured Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
>>> help HELP - display a menu of commands DISPLAY - display the list of numbers PRINT - print a number (FIRST;LAST;nth) SWAP - exchange two elements (nth;mth) ADD - add a number to the list (FIRST;LAST) EXIT - terminate execution of the program >>> display 11 22 33 44 55 66 77 88 99 9 8 7 6 5 4 3 2 1 >>> print first 11 >>> print last 1 >>> print 3 33 >>> print 18 1 >>> swap 5 7 >>> display 11 22 33 44 77 66 55 88 99 9 8 7 6 5 4 3 2 1 >>> swap 1 18 >>> display 1 22 33 44 77 66 55 88 99 9 8 7 6 5 4 3 2 11 >>> help HELP - display a menu of commands DISPLAY - display the list of numbers PRINT - print a number (FIRST;LAST;nth) SWAP - exchange two elements (nth;mth) ADD - add a number to the list (FIRST;LAST) EXIT - terminate execution of the program >>> add first 100 >>> add last 200 >>> shownumbers ### Unrecognizable command: shownumbers >>> display 100 1 22 33 44 77 66 55 88 99 9 8 7 6 5 4 3 2 11 200 >>> exit |
Featured Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
/* * Program featuring an array to store and interactively manipulate a list of numbers. */ package arrayplay; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class NumberList { // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS private static final int LIMIT = 1000; private static int[] numbers = new int[LIMIT]; private static int numberOfNumbers = 0; private static Scanner commandReader = new Scanner(System.in); public static void main(String[] args) { try { // ESTABLISH THE ARRAY OF NUMBERS readNumbers(); // CHECK THE DATA // System.out.println("nThe original list of numbers ..."); // displayNumbers(); // ENTER THE INTERPRETER interpreter(); } catch (FileNotFoundException ex) { System.out.println("The file was not found. Please think again."); System.exit(-1); } } // Assuming that the data file will be found in the public_html/data // subdirectory of the user’s home directory. private static Scanner establishScanner(String fn) throws FileNotFoundException { String separator = File.separator; String homeDirectory = System.getProperty("user.home"); String path = homeDirectory + separator + "public_html" + separator + "data" + separator; String fullFileName = path + fn; return new Scanner(new File(fullFileName)); } private static void readNumbers() throws FileNotFoundException { Scanner scanner = establishScanner("NumberSet.txt"); while (scanner.hasNext()) { numbers[numberOfNumbers] = scanner.nextInt(); numberOfNumbers = numberOfNumbers + 1; } } private static void displayNumbers() { for (int x = 0; x < numberOfNumbers; x = x + 1) { System.out.println(numbers[x]); } } private static void interpreter() { System.out.print(">>> "); String command = commandReader.next(); if (command.equalsIgnoreCase("DISPLAY")) { interpreterDisplayCommand(); } else if (command.equalsIgnoreCase("PRINT")) { interpretPrintCommand(); } else if (command.equalsIgnoreCase("SWAP")) { interpretSwapCommand(); } else if (command.equalsIgnoreCase("ADD")) { interpretAddCommand(); } else if (command.equalsIgnoreCase("HELP")) { interpretHelpCommand(); } else if (command.equalsIgnoreCase("EXIT")) { System.exit(0); } else { System.out.println("### Unrecognizable command: " + command); } interpreter(); } private static void interpreterDisplayCommand() { displayNumbers(); } private static void interpretPrintCommand() { String operand = commandReader.next(); if (operand.equalsIgnoreCase("FIRST")) { System.out.println(numbers[0]); } else if (operand.equalsIgnoreCase("LAST")) { System.out.println(numbers[numberOfNumbers - 1]); } else { int index = Integer.valueOf(operand); System.out.println(numbers[index - 1]); } } private static void interpretHelpCommand() { System.out.println("HELP - display a menu of commands"); System.out.println("DISPLAY - display the list of numbers"); System.out.println("PRINT - print a number (FIRST;LAST;nth)"); System.out.println("SWAP - exchange two elements (nth;mth)"); System.out.println("ADD - add a number to the list (FIRST;LAST)"); System.out.println("EXIT - terminate execution of the program"); } private static void interpretSwapCommand() { int position1 = commandReader.nextInt(); int position2 = commandReader.nextInt(); int temp = numbers[position1 - 1]; numbers[position1 - 1] = numbers[position2 - 1]; numbers[position2 - 1] = temp; } private static void interpretAddCommand() { String position = commandReader.next(); if (position.equalsIgnoreCase("LAST")) { addLast(); } else if (position.equalsIgnoreCase("FIRST")) { addFirst(); } else { System.out.println("### invalid operand for add command"); } numberOfNumbers = numberOfNumbers + 1; } private static void addLast() { numbers[numberOfNumbers] = commandReader.nextInt(); } private static void addFirst() { for (int x = numberOfNumbers; x > 0; x = x - 1) { numbers[x] = numbers[x - 1]; } numbers[0] = commandReader.nextInt(); } } |