Loyola College in Maryland

CS 201 - Computer Science I
Spring 2009


Loyola College > Department of Computer Science > Dr. James Glenn > CS 201 > Homework > Final Exam Practice Problems

Solutions are now available.

Problem 0: Review old quizzes, homework assignments, and labs.

Problem 1: Review the following vocabulary and, where appropriate, the corresponding Java syntax.

Problem 2: To draw an n-sided polygon of radius r centered at (x, y), we need to know the coordinates of each corner. If corners are numbered clockwise starting with corner 0 in the 3 o'clock position, then the coordinates of corner i are (x + r * cos(i * 360 / n), y + r * sin(i * 360 / n)) wbere angles are measured in degrees. Write a Java statement that, given x, y, r, n, and i, draws the ith side of the polygon (that is, the line from corner i to corner i + 1). Keep in mind that Java's trig functions expect angles to measured in radians and that the Graphics methods take ints as arguments.

Problem 3: Encode each of the following using the 8-bit two's complement system.

Problem 4: Encode each of the following using the floating point scheme described in Brookshear, Ch. 1.

Problem 5: Write each of the following as

Problem 6: Write a machine language program for the machine described in Brookshear, App. C that computes 12x + 8y. Assume that x has been stored in memory cell F0 and y has been stored in cell F1 before the program is executed. Store the result in cell F2.

Problem 7: Recall that the Card class has two instance variables rank and suit, both ints.

Problem 8: Format the following code properly. Figure out which case is unreachable. Are there any values of x and y for which two messages would be printed?

if (x > 10 && y < 5)
if (x > 5 && y < 0)
System.out.println("A");
if (x > 0 && y > 0)
if (x == 0)
System.out.println("B");
else
System.out.println("C");

Problem 9: The following method attempts to compute the sum 1! + 2! + ... + n!. It does not work correctly.

    public static int factorialSum(int n)
    {
	int sum = 0;
	int factorial = 0;
	
	for (int i = 1; i <= n; i++)
	    {
		// compute i!

		for (int j = 1; j <= i; j++)
		    factorial *= j;

		// factorial should now hold i!; add it to the sum

		sum += factorial;
	    }

	return sum;
    }

Problem 10: For each of the following sequences of statements, write loops that are equivalent. Each answer should have only one line that calls the drawLine method.

This draws 5 concentric circles.
g.drawOval(0, 0, 100, 100);
g.drawOval(10, 10, 80, 80);
g.drawOval(20, 20, 60, 60);
g.drawOval(30, 30, 40, 40);
g.drawOval(40, 40, 20, 20);
5 concentric circles centered @ (50,50) w/ radii 20, ..., 100
This draws 3 triangles (polygons!) of radius 20 centered at (20, 20), (60, 20), and (100, 20).
int sides = 3;
double sideAngle = 2 * Math.PI / sides;
double startAngle = Math.PI / 2;

g.drawLine(20 + (int)(20 * Math.cos(startAngle)),
	   20 + (int)(20 * Math.sin(startAngle)),
	   20 + (int)(20 * Math.cos(startAngle + sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + sideAngle)));
g.drawLine(20 + (int)(20 * Math.cos(startAngle + sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + sideAngle)),
	   20 + (int)(20 * Math.cos(startAngle + 2 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 2 * sideAngle)));
g.drawLine(20 + (int)(20 * Math.cos(startAngle + 2 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 2 * sideAngle)),
	   20 + (int)(20 * Math.cos(startAngle + 3 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 3 * sideAngle)));

g.drawLine(60 + (int)(20 * Math.cos(startAngle)),
	   20 + (int)(20 * Math.sin(startAngle)),
	   60 + (int)(20 * Math.cos(startAngle + sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + sideAngle)));
g.drawLine(60 + (int)(20 * Math.cos(startAngle + sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + sideAngle)),
	   60 + (int)(20 * Math.cos(startAngle + 2 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 2 * sideAngle)));
g.drawLine(60 + (int)(20 * Math.cos(startAngle + 2 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 2 * sideAngle)),
	   60 + (int)(20 * Math.cos(startAngle + 3 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 3 * sideAngle)));

g.drawLine(100 + (int)(20 * Math.cos(startAngle)),
	   20 + (int)(20 * Math.sin(startAngle)),
	   100 + (int)(20 * Math.cos(startAngle + sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + sideAngle))));
g.drawLine(100 + (int)(20 * Math.cos(startAngle + sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + sideAngle)),
	   100 + (int)(20 * Math.cos(startAngle + 2 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 2 * sideAngle)));
g.drawLine(100 + (int)(20 * Math.cos(startAngle + 2 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 2 * sideAngle)),
	   100 + (int)(20 * Math.cos(startAngle + 3 * sideAngle)),
	   20 + (int)(20 * Math.sin(startAngle + 3 * sideAngle)));
3 triangles
You are on your own for this picture. triangle to octagon, side-by-side

Problem 11: Write methods to do each of the following.

Problem 12: Write a class called Song with fields for the title and the artist. Include constructors, accessors, mutators, an equals method and a toString method.

Problem 13: Write a class called Quadratic to represent quadratic functions. The class should have three fields for the three coefficients of the function. Include a zero-argument constructor, a constructor that allows all three coefficients to be set, a method double getCoefficient(int which) to return a coefficient, and a method double evaluate(int x) to return the value of the function for a particular value of x.

Problem 14: Write a code fragment that creates a 10 element array of ints, initializes all elements of that array to random integers from 1 to 99, and then displays the sum of all the entries and the index of the element that has the highest value.

Problem 15: Write a method that takes two arrays of doubles as arguments and returns a new array that is the same size as the ones passed in and that contains elements initialized to the sum of the corresponding elements in the arguments. For example, if the two arrays passed in as arguments are [1 2 3 4 5] and [10 20 30 40 50] then the array returned should contain [11 22 33 44 55]. You may assume that the two arrays passed in have the same number of elements.

Problem 16: Write a method that takes an array of int as arguments and modifies that array so that every element is set equal to the largest value that was originally in the array. For example, if the array originally contains [1 6 3 8 3 2] then the method should change it to [8 8 8 8 8 8].


Valid HTML 4.01 Transitional