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.
Card a = new Card(2, Card.HEARTS); Card b = new Card(2, Card.CLUBS); Card c = new Card(2, Card.DIAMONDS); System.out.println(a.makesPair(b)); // should output false (different color) System.out.println(a.makesPair(c)); // should output true System.out.println(a.makesPair(a)); // should output false (same suit)
public boolean makesPair(Card other)
{
if (rank == other.rank && isRed() == other.isRed() && suit != other.suit)
return true;
else
return false;
}
Card d = new Card(3, Card.HEARTS); System.out.println(a.makesPair(d)); // false -- different rank
Card[] hand = new Card(5); hand[0] = new Card(3, Card.CLUBS); hand[1] = new Card(4, Card.CLUBS); hand[2] = new Card(Card.JACK, Card.HEARTS); hand[3] = new Card(Card.QUEEN, Card.CLUBS); hand[4] = new Card(9, Card.CLUBS); System.out.println(Card.isFlush(hand)); // should output false hand[2] = new Card(Card.JACK, Card.CLUBS); System.out.println(Card.isFlush(hand)); // should output true
public static boolean isFlush(Card[] hand)
{
int c = 1;
while (c < hand.length && hand[c].suit == hand[0].suit)
c++;
if (c >= hand.length)
return true;
else
return false;
}
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.
public static int productOddCubes(int n)
{
int product = 1;
for (int term = 0; term < n; term++)
product *= Math.pow(2 * term + 1, 3);
return product;
}
public static int findOutOfOrder(String s)
{
int loc = 0;
while (loc < s.length() - 1 && s.charAt(loc) < s.charAt(loc + 1))
loc++;
if (loc == s.length() - 1)
return -1;
else
return loc;
}
public static int shortestLength(String[] sarr)
{
int min = Integer.MAX_VALUE;
for (int i = 0; i < sarr.length; i++)
if (sarr[i].length() < min)
min = sarr[i].length();
return min;
}
public static String shortestString(String[] sarr)
{
int min = Integer.MAX_VALUE;
String shortest = null;
for (int i = 0; i < sarr.length; i++)
if (sarr[i].length() < min)
{
min = sarr[i].length();
shortest = sarr[i];
}
return shortest;
}
public static int findSpace(String[] sarr)
{
int loc = 0;
while (loc < sarr.length && sarr[loc].indexOf(' ') == -1)
loc++;
if (loc == sarr.length)
return -1;
else
return loc;
}
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);