CSci 2101 Problem set 3.

Due Tuesday June 1 at 11:59pm

You may work individually or in pairs.

30 points

Problem 1: Random sentence generator (10 points).

Write a program that has an array of articles, an array of adjectives, an array of nouns, and an array of verbs and constructs random sentences based on the scheme
article + adjective + noun + verb
For instance, the program may randomly select an article 'My", an adjective "old", a noun "dog", and a verb "is walking", to make a sentence "My old dog is walking".
Make sure to have at a different number of elements in different arrays (for instance, you would probably have fewer articles than nouns).
Write methods as needed; avoid code repetition (if you repeat roughly the same code, make it into a method).

Problem 2: tabulating results of a two-dice roll (10 points).

Write a program that simulates 500 rolls of two six-sided dice. Create an array of integers to count how many times each sum occurs. For instance, if you get 3 on the first die and 5 on the second one, then the result is 8, and the array element that counts the number of 8s needs to be incremented. At the end print out how many times each sum occurred. In comments write your conclusions about probabilities of different sums.

Problem 3: making a list of words (10 points).

Write a program that reads a text and creates an alphabetically ordered ArrayList of all words appearing in the text. Duplicated words should appear in the ArrayList only once. Print out the resulting ArrayList at the end. Feel free to use any methods of the ArrayList.

Use the method compareToIgnoreCase to compare two strings alphabetically (ignoring the upper/lower case differences). The method is called on one string and takes another one as a parameter. It returns a negative number if the string it is called on is before teh parameter string in the alphabetical order, a positive number if it is after, and 0 if the two strings are the same (ignoring the upper/lower case). For instance:


String str = "tiger";

int diff = str.compareToIgnoreCase("bear");  // diff > 0
diff = str.compareToIgnoreCase("Zebra");  // diff < 0; upper case doesn't matter
diff = str.compareToIngnoreCase("Tiger"); // diff = 0; the strings are the same

The following program ReadIn.java has the starting code for reading text and instructions on how to feed a text file to a java program on the command line.


import java.util.Scanner;


public class ReadIn {

    /**
     * The program illustrates reading from the standard input until
     * the end of input. The program prints the input data one string at 
     * a time. Strings are separated by spaces, tabs, and new lines.  
     * The program prints "Done" when the end of input is reached.
     *
     * Usage:
     * On UNIX machines: Ctrl-D signals the end of input.
     * On Windows: Ctrl-C signals the end of input.
     * One of the above should work on a Mac (try Ctrl-D first).
     *
     * Redirecting input from a file (on UNIX and Windows, should work on Mac):
     * Assume that the input is in a file input.txt, run the program as
     * java ReadIn < input.txt
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("Say something. Press Ctrl-D (Ctrl-C on Windows) when done");

        while (in.hasNext()) {
            System.out.println(in.next());
        }
        System.out.println("Done");
    }

}

How to submit

Submit the java file(s) with your testing code by e-mail to me. The subject of the message must be 2101 Problem set 3. Make sure to CC your group partner(s) if any.


CSci 2101 course web site.