CSci 2101 Lab 3: Arrays.

Due Monday February 15 at 11:59pm

Work in pairs on this lab.

25 points

Problem 1: Random sentence generator (8 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: count negative elements (7 points)

Write and test a method countNegatives that takes an array of integers (i.e. elements of type int) and returns the number of negative elements in the array.

Extra credit: is there anything wrong with this method? (4 points)

Imagine you have just started working in a software development company, and your boss asks you to take a look at some code written by your predecessor at the company. Your boss says that the company software occasionally outputs unexpected results, and the method isOdd may be to blame. Unfortunately, the actual data ran by the client requires security clearance which you don't yet have. Because of that you don't know what data causes problems and whether it is related to the method isOdd. You are asked to test the method and report back on whether the method is correct, and if it's not then to explain what is wrong with it and fix it. Make sure to fix it in a good programming style (if a fix is indeed needed). Make sure to include all your test data in your report.
Below is the code:


public class IsVeryOdd {
    public static void main(String [] args) {
	// your test code goes here
    }

    /** The method takes an integer n and returns true if 
	n is odd and false otherwise
     **/
    public static boolean isOdd(int n) {
	return ( (n % 2) == 1);  
    }
}

How to submit

Submit the java file(s) by e-mail to me. The subject of the message must be 2101 Lab 3. Make sure to CC your group partner.


CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.