CSci 1211 Problem set 6. Due Friday, Feb. 27th at 5pm

Problem 1 (6 points)

Write a program that reverses the elements of an array of integers. For instance, if the array initially is [1, 2, 3, 4, 5], then in the end of the program it becomes [5, 4, 3, 2, 1]. Note that this is different from (and somewhat harder than) printing the array backwards.

You don't need the user's input in the program, just set the array in the beginning.

The example of swapping two elements of an array should be helpful.

Make sure to test your program on arrays of different sizes.

Problem 2 (6 points)

Write a program that is given the following: (again, assume that all the values are set, not entered by the user).

The program will output the number of elements of the array within the given precision of x (including x+precision and x-precision). If there are no elements within this precision, then the message "No elements found" should be displayed (instead of just "0 elements found"). If there 1 such element, the message should be "1 element found", not "1 elements found".
Extra credit (2 points): make your program print out the correct singular/plural forms of the noun for all numbers. I.e. it should print the singular form for 21, 31, etc., but the plural form for 11, 111, etc.

Problem 3 (8 points)

Change the random rectangles with random colors program (problem 2 of Problem set 4) so that it doesn't use the switch statement. Instead, create an array of colors, randomly generate a number between 0 and length-1 (where "length" is the length of the array), and then use the random number as the index to get the color.

Also, instead of repeating your code for generating a rectangle 3 times, use a loop to create 3 (or more) random rectangles.

Important: please submit your HTML file with your solution.

Problem 4 (8 points)

Write a program to read a sequence of strings from the user and store them in a vector in alphabetical order. More specifically, the vector starts off as empty. When the first string is read, it is added to the vector. When the second string is read, it is compared to the first one and inserted before or after it, depending on which of the two strings is first alphabetically. The process continues until the user enters a specified keyword (such as "bye").

In the end of the program print out the strings as they are stored in the vector.

Problem 4 (10 points)

For the following program draw the following pictures of the state of the program (the values of all variables and all elements of the array): As before, it's OK to test the program, but try to figure out the answer on paper first.

Here is the program:


public class PS6 {

    public static void main (String [] args) {
	StringBuffer [] sb = new StringBuffer[5];
	int i, j;

	for (i = 0, j = sb.length - 1; i <= j; ++i, --j) {
	    sb[i] = new StringBuffer();
	    sb[j] = sb[i];
	    sb[i].append(j);
	    sb[j].append(i);
	}

	StringBuffer sb1;
	sb1 = sb[i].reverse();
	sb[i] = sb[j].reverse(); // draw the picture after this statement
	sb[j] = sb1.reverse();

	for (i = 0; i < sb.length; ++i) {
	    System.out.println(sb[i]);
	}
    }

    
}

This page is a part of the course CSci 1211 at UMM.