Hi There!

I'm Dan Schlegel, an Associate Professor in the Computer Science Department at SUNY Oswego

Programming Challenge 5: Three List Interpreters

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.

  1. Enter the given program in your Java/IntelliJ world.
  2. 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.
  3. 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.

  1. 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.
  2. 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.
  3. Run the program, and interact with it in a manner analogous to the numeric demo.

Problem 3: The WordList program of the arraylistplay package.

  1. 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!
  2. Run the program, and interact with it in a manner analogous to the numeric demo.

The Exit

  1. The assignment is due April 13th.
  2. Please post your work to you work site. You must post it to your web site to get credit.
  3. 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
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

/*
 * 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();
    }
}