Loyola College in Maryland

CS 201 - Computer Science I
Fall 2003


Loyola College > Department of Computer Science > CS 201 > Homework > Solutions to Final Exam Practice Problems

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

Problem 1: 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.

g.drawLine(x + (int)(r * Math.cos(Math.toRadians(i * 360.0 / n))),
           y + (int)(r * Math.sin(Math.toRadians(i * 360.0 / n))),
           x + (int)(r * Math.cos(Math.toRadians((i + 1) * 360.0 / n))),
           y + (int)(r * Math.sin(Math.toRadians((i + 1) * 360.0 / n))));

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


Problem 3: 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");
// Note that there are two separate ifs but no values lead to two messages
// displayed since it is impossible for (x > 5 && y < 0) and (x > 0 && y > 0)
// to be true at the same time.

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 4: The following method attempts to compute the sum 1! + 2! + ... + n!. It does not work correctly.

Bold is for part (b)
Italics are for part (c)

public static int factorialSum(int n)
{
    int sum = 0;
    int factorial = 0;
    int factorial = 1;	

    for (int i = 1; i <= n; i++)
    {
        // compute i!

        int factorial = 1;
        factorial *= 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 5: 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);

for (int r = 0; r < 5; r++)
    g.drawOval(r * 10, r * 10, 100 - 20 * r, 100 - 20* r);
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)));
for (int x = 20; x <= 100; x += 40)
   for (int side = 0; side < sides; side++)
       g.drawLine(x + (int)(20 * Math.cos(startAngle + side * sideAngle)),
                  20 + (int)(20 * Math.sin(startAngle + side * sideAngle)),
                  x + (int)(20 * Math.cos(startAngle + (side + 1) * sideAngle)),
                  20 + (int)(20 * Math.sin(startAngle + (side + 1) * sideAngle)));
You are on your own for this picture.
for (sides = 3; sides < 9; sides++)
{
    sideAngle = 2 * Math.PI / sides;
    for (int side = 0; side < sides; side++)
        g.drawLine(sides * 40 - 100 + (int)(20 * Math.cos(startAngle + side * sideAngle)),
                   20 + (int)(20 * Math.sin(startAngle + side * sideAngle)),
                   sides * 40 - 100 + (int)(20 * Math.cos(startAngle + (side + 1) * sideAngle)),
                   20 + (int)(20 * Math.sin(startAngle + (side + 1) * sideAngle)));
}

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


Problem 7: Write a code fragment that creates a 10 row, 5 column array of ints, initializes all elements of that array to random integers from 1 to 9, and then displays the sum of all the entries and the index of the row that has the highest total.

// declare and create the array

int[][] arr = new int[10][5];

// initialize array to random numbers

for (int r = 0; r < arr.length; r++)
    for (int c = 0; c < arr[r].length; c++)
        arr[r][c] = (int)(Math.random() * 9) + 1;

int maxTotal = Integer.MIN_VALUE; // the highest total for a row
int maxRow = -1;                  // the row with the highest total
int total = 0;

for (int r = 0; r < arr.length; r++)
{
    // get the total for the current row

    int rowTotal = 0;
    for (int c = 0; c < arr[r].length; c++)
        rowTotal += arr[r][c];

    // add current row's total into the grand total

    total += rowTotal;

    // check if the current row had a higher total than any other so far

    if (rowTotal > maxTotal)
    {
        maxTotal = rowTotal;
        maxRow = r;
    }
}

System.out.println("TOTAL = " + total + ", MAX ROW = " + maxRow);