Your task is to write three separate Java programs for this assignment, all pertaining to the storage, manipulation, and use 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 ArrayStats program of the arrayplay package. Read and understand it!
Problem 1: The ArrayStats 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 we did in the example demo.
- Add the following pieces of functionality to your program, working by analogy with what is there and being sure to update the help command as necessary:
Problem 2: The DoubleArrayStats program of the arrayplay package.
- By analogy with the ArrayStats program, write a program called DoubleArrayStats to store and manipulate doubles (instead of ints) in an array. Do so within the arrayplay package.
- Create a data file of doubles called DoubleSet.txt containing between 10 and 15 doubles 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 integer demo.
Problem 3: The DoubleArrayListStats program of the arraylistplay package.
- By analogy with the DoubleArrayStats program that you just wrote in the arrayplay package, write a program called DoubleArrayListStats 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 with ArrayLists! There are likely some unnecessary pieces of functionality in the program given the translation to ArrayLists!
- Run the program, and interact with it in a manner analogous to the array-based demo.
The Exit
- The assignment is due Tuesday, November 14th.
- Please post your work to you work site. You must post it to your web site to get credit.
- 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
>>> help HELP - display a menu of commands DISPLAY - display the list of numbers SORT - sort the numbers ADD - add a number to the end of the list MIN - print the minimum value, requires a sorted list EXIT - terminate execution of the program >>> display 10 12 4 3 6 12 18 24 14 10 9 37 23 18 12 23 >>> sort >>> display 3 4 6 9 10 10 12 12 12 14 18 18 23 23 24 37 >>> min 3 >>> add 1 >>> display 3 4 6 9 10 10 12 12 12 14 18 18 23 23 24 37 1 >>> min The list isn't sorted, please sort first. >>> sort >>> min 1 >>> exit
Featured Program
/*
* Program featuring an array to store, manipulate, and compute stats about a list of numbers.
*/
package arrayplay;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ArrayStats {
// Initially, we will assume we're only reading 10 numbers.
private static int[] numbers = new int[10];
private static int numberOfNumbers = 0;
private static boolean sorted = false;
public static void main(String[] args) {
try {
readNumbers();
//displayNumbers(); // Uncomment this to check that your numbers are reading right!
interpreter(new Scanner(System.in));
} catch (FileNotFoundException e) {
System.out.println("The file was not found. Please think again.");
System.exit(-1);
}
}
private static void interpreter(Scanner commandReader) {
System.out.print(">>> ");
String command = commandReader.next();
if (command.equalsIgnoreCase("DISPLAY")) {
interpreterDisplayCommand();
} else if (command.equalsIgnoreCase("SORT")) {
interpretSortCommand();
} else if (command.equalsIgnoreCase("ADD")) {
interpretAddCommand(commandReader);
} else if (command.equalsIgnoreCase("MIN")) {
interpretMinCommand();
} else if (command.equalsIgnoreCase("HELP")) {
interpretHelpCommand();
} else if (command.equalsIgnoreCase("EXIT")) {
System.exit(0);
} else {
System.out.println("### Unrecognizable command: " + command);
}
interpreter(commandReader);
}
private static void interpreterDisplayCommand() {
displayNumbers();
}
private static void interpretAddCommand(Scanner commandReader) {
int value = 0;
try {
value = commandReader.nextInt();
} catch(InputMismatchException e) {
System.out.println("You didn't enter an integer value to add.");
return;
}
ensureCapacity();
numbers[numberOfNumbers] = value;
numberOfNumbers = numberOfNumbers + 1;
sorted = false;
}
private static void interpretSortCommand() {
bubbleSort();
}
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("SORT - sort the numbers");
System.out.println("ADD - add a number to the end of the list");
System.out.println("MIN - print the minimum value, requires a sorted list");
System.out.println("EXIT - terminate execution of the program");
}
private static void interpretMinCommand() {
if (numberOfNumbers == 0) {
System.out.println("There are no numbers, so there is no minimum.");
return;
}
if (!sorted) {
System.out.println("The list isn't sorted, please sort first.");
return;
}
System.out.println(numbers[0]);
}
private static void displayNumbers() {
for (int x = 0; x < numberOfNumbers; x = x + 1) {
System.out.println(numbers[x]);
}
}
// 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 fileName) throws FileNotFoundException {
String separator = File.separator;
String homeDirectory = System.getProperty("user.home");
String path = homeDirectory + separator + "public_html" + separator + "data" + separator;
String fullFileName = path + fileName;
return new Scanner(new File(fullFileName));
}
private static void readNumbers() throws FileNotFoundException {
Scanner scanner = establishScanner("NumberSet.txt");
while (scanner.hasNext()) {
ensureCapacity();
numbers[numberOfNumbers] = scanner.nextInt();
numberOfNumbers = numberOfNumbers + 1;
}
}
// Ensures the numbers array has room for a next element.
// If not, it makes a new array twice the size,
// and copies over the old array to the new one.
private static void ensureCapacity(){
if (numberOfNumbers == numbers.length){
int[] newNumbers = new int[numbers.length * 2];
for (int i = 0; i < numbers.length; i = i + 1){
newNumbers[i] = numbers[i];
}
numbers = newNumbers;
}
}
private static void swap(int i, int j){
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
private static void bubbleSort(){
for(int i = 0; i < numberOfNumbers; i = i + 1){
for(int j = 1; j < (numberOfNumbers - i); j = j + 1){
if(numbers[j-1] > numbers[j]){
swap(j, j-1);
}
}
}
sorted = true;
}
}