CSCI A201/A597

Lab Notes 13

Spring 2000


Types of problems likely to appear on the practical next week.

Sample Problem 1.

Write a program to Allow the user to specify the size of the array in the beginning and to restart the process over and over again if (s)he so desires. The user interface should be through a drawing window with buttons like in assignment 8 part 10.

Sample Problem 2.
Write a program that defines, allocates and initializes a two dimensional array of integers. Then find the maximum among the array elements. Display the array as a grid of gray squares where for each cell in the grid the intensity of the color is the ratio between the number in the cell and the max value in the array. Check out 4.4.1. in your text for color functions.

Sample Problem 3.
Implement a game similar to the packing bin assignment with the following differences: assume a circular bin and provide rectangles instead of circles for the user to place them. When you place the rectangle check only if it falls inside the circular bin, and if it does not send it back to the starting location otherwise place it.

Sample Problem 4.
Write a method that computes pi by throwing n darts. Each dart is randomly thrown at a two hundred by two hundred pixel square. It is a hit if it is within 100 pixels of the center. An approximation of pi is determined by dividing the number of hits by four times the number of darts.

Sample Problem 5.
Implement the Tetris-like game in which random rectangles are used rather than random sized circles.

Sample Problem 6.
Write a program that generates a certain number of random sized rectangles, places them on the screen and allows the user to move them around with the mouse, one at a time.

Sample Problem 7.
Write a procedure reverse that reverses the sequence of elements in an array. For example if reverse is called with an array containing:
1 4 9 16 9 7 4 9 11
then the array is changed to
11 9 4 7 9 16 9 4 1

Sample Problem 8.
Write a function
int[] append(int[] a, int[] b)
that appends one array after another. For example, if a is
1 4 9 16
and b is
9 7 4 9 11
then append returns the array
1 4 9 16 9 7 4 9 11

Sample Problem 9.
Write a predicate function
boolean equals(int[] a, int[] b)
that checks whether two arrays have the same elements in the same order.


Sample Problem 10.
Write a procedure
void barChart(double[] data)
that displays a bar chart of the values in data. You may assume that all values in data are positive. Hint: you must figure out the maximum of the values in data. Set the coordinate system so that the x-range equals the number of bars and the y-range goes from 0 to maximum.

Sample Problem 11.
Write a loop that fills an array v with ten random numbers between 1 and 100 and then reports the maximum and the minimum in the array. Write Java code for a loop that simultaneously computes the maximum and the minimum of an array.

Sample Problem 12.
Write a function
double scalarProduct(double[] a, double[] b)
that computes the scalar product of two mathematical vectors (represented as arrays). The scalar product is
a0b0 + a1b1 + ...an-1bn-1

Sample Problem 13.
Write a function that computes the alternating sum of all elements in an array. For example, if alternatingSum is called with an array containing
1 4 9 16 9 7 4 9 11
then it computes
1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11 = -2

Sample Problem 14.
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 x 3 grid as in

The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, accept mouse clicks into empty squares, change the players after every successful move, and pronounce the winner.


Sample Problem 15.
Write a program that computes the mean (average) and standard deviation of a set of values located in an array. The average of a data set
is equal to the sum of the elements divided by how many they are, and the standard deviation is the square root of the sum of the squared differences between the individual values and their average divided by their number minus 1:


Other Sample Problems.


These problems will be refined in the next few days and will be the starting point in producing the actual problems that you will get on the practical. If you know how to solve these problems you will probably do well on the exam. We will make sure that the problems that can appear on the exam will be all of the same difficulty. Distribution of problems will be through QuizSite in the exact same way it was done for the first practical exam.


Last updated Apr 13, 2000 by Adrian German.