Problem 1: Give a brief definition of each of the following.
Problem 2:
Suppose we have the rank of a card in the rank variable and the suit
in the suit variable. Assume that both variables are ints
and that there are constants such as RANK_KING and SUIT_CLUBS
declared. Write a code fragment that determines the value the card
would have in the game of Hearts and saves that value in the variable
pointValue (also an int). pointValue should be 1
if the card is a heart, 13 if the card is the queen of spades,
-10 if the card is the jack of diamonds, and 0 otherwise.
int pointValue;
if (suit == SUIT_HEARTS)
{
pointValue = 1;
}
else if (suit == SUIT_SPADES && rank == RANK_QUEEN)
{
pointValue = 13;
}
else if (suit == SUIT_DIAMONDS && rank == RANK_JACK)
{
pointValue = -10;
}
else
{
pointValue = 0;
}
Problem 3: The following code fragment is intended to return the area, in hectares, of a rectangular plot of land given the coordinates (in meters) of either pair of opposite corners of the plot. As written, it does not accomplish its task. (Note: there are 10000 square meters in a hectare.)
Scanner scan = new Scanner(System.in);
System.out.println("Enter coordinates of opposite corners:");
int x1 = scan.nextInt();
int y1 = scan.nextInt();
int x2 = scan.nextInt();
int y2 = scan.nextInt();
final int SQ_METERS_PER_HECTARE = 10000;
int width = x2 - x1;
int height = y2 - y1;
if (width < 0)
{
width = -width;
}
else if (height < 0)
{
height = -height;
}
System.out.println(width / 10000 * height; (double)width * height / SQ_METERS_PER_HECTARE);
}
Problem 4: The following code fragment was purposefully formatted poorly and has errors. Assume that x and y were properly declared and initialized.
int z;
z = 2;
if (x == 1 || y > 0)
if (y < x)
{
if (x > 0)
z = 1;
}
else
z = 0;
System.out.println(z);
Problem 5: Rewrite the following code fragment using a switch statement. Assume that lines has been properly declared and initialized.
int score;
if (lines == 1)
{
score = 100;
}
else if (lines == 2)
{
score = 300;
}
else if (lines == 3)
{
score = 700;
}
else if (lines == 4)
{
score = 1500;
}
else
{
score = 1500 + 1000 * (lines - 4);
}
int score;
switch (lines)
{
case 1:
score = 100;
break;
case 2:
score = 300;
break;
case 3:
score = 700;
break;
case 4:
score = 1500;
break;
default:
score = 1500 + 1000 * (lines - 4); // formula would work for case 4 too
break;
}
Problem 6: Consider the following code fragment, which purportedly computes the sum of the first n odd integers. Assume that n has been properly declared and initialized.
// compute the sum of the first n odd integers
int sum = 0;
int newSum = 0;
for (int i = 0; i <= n; i++)
{
newSum = (i + 2) + sum;
newSum += i;
}
System.out.println(newSum);
For each of the following values of n, give the value displayed
by the code fragment.
(Remember, the code does not do what the comment claims it does.)
Problem 7: Consider the following code fragment, which purportedly computes the number of digits in the decimal representation of n. Assume that n was properly declared and initialized.
// compute the number of digits in n
int digits = 0;
do
{
digits++;
} while (n % 10 > 0);
System.out.println(digits);
When answering the following questions, keep in mind that the code
does not do what the comment claims it does.
Problem 8: For each of the following sequences of statements, write loops that are equivalent. Each answer should have only one line that invokes the drawLine method.
g.drawLine(0, 0, 100, 100); g.drawLine(20, 0, 80, 100); g.drawLine(40, 0, 60, 100); g.drawLine(60, 0, 40, 100); g.drawLine(80, 0, 20, 100); g.drawLine(100, 0, 0, 100); |
![]() |
for (int l = 0; l < 6; l++)
{
g.drawLine(l * 20, 0, 100 - l * 20, 100);
}
g.drawLine(0, 0, 20, 0); g.drawLine(0, 10, 20, 10); g.drawLine(0, 20, 20, 20); g.drawLine(40, 0, 60, 0); g.drawLine(40, 10, 60, 10); g.drawLine(40, 20, 60, 20); g.drawLine(0, 40, 20, 40); g.drawLine(0, 50, 20, 50); g.drawLine(0, 60, 20, 60); g.drawLine(40, 40, 60, 40); g.drawLine(40, 50, 60, 50); g.drawLine(40, 60, 60, 60); |
![]() |
for (int col = 0; col < 2; col++)
{
for (int row = 0; row < 2; row++)
{
for (int line = 0; line < 3; line++)
{
g.drawLine(col * 40, row * 40 + line * 10, col * 40 + 20, row * 40 + line * 10);
}
}
}
Problem 9: Write code fragments to do each of the following.
int sum = 0;
for (int i = 1; i <= n; i++)
sum += Math.pow(i, 3);
System.out.println(sum);
int sum = 0;
// here i is 1, 3, 5, ...
// to get n terms, the bound must be i <= 2n-1
for (int i = 1; i <= 2 * n - 1; i += 2)
sum += Math.pow(i, 3);
// alternate solution: have i = 1, 2, 3, ..., n
// when i is 1 we want to add in 1^3
// when i is 2 we want to add in 3^3
// when i is 3 we want to add in 5^3
// in general the term to add in is (2i-1)^3
/*
for (int i = 0; i < n; i++)
sum += Math.pow(2 * i - 1, 3);
*/
System.out.println(sum);
// when doing a sum we start our accumulator at 0 (the
// identity for +) and in the loop add the accumulator
// and the next term
// when doing a product we start our accumulator at 1
// (the identity for *) and in the loop multiply the
// accumulator and the next term
// for a max we will start our accumulator at infinity
// (the indentity for mub) and in the loop compute the
// min of the accumulator and the next term
double min = Double.POSITIVE_INFINITY;
int iForMin = 0;
for (int i = 1; i <= 100; i++)
{
if (Math.tan(i) < min)
{
min = Math.min(min, Math.tan(i));
iForMin = i;
}
}
System.out.println(iForMin);
// start at the first character
int loc = 0;
// keep going until we're out of characters or we
// find a lower case letter (or, in other words,
// keep going while we're not out of characters and
// we're not on a lower case letter
while (loc < s.length() && !(s.charAt(loc) >= 'a' && s.charAt(loc) <= 'z'))
{
loc++;
}
// figure out why we stopped
int result;
if (loc == s.length())
{
result = -1; // not found
}
else
{
result = loc; // found at loc
}
System.out.println(result);
// start at the first character
int loc = 0;
// keep going until we're out of characters or we
// find the first of two straight upper case letters
// (or, in other words, keep going while we're not out
// of characters and we're not at the first of two upper
// case letters
// use s.length() - 1 instead of s.length() since the last
// character can't be the first of two consecutive uppers
while (loc < s.length() - 1 && !(s.charAt(loc) >= 'A' && s.charAt(loc) <= 'Z' && s.charAt(loc + 1) >= 'A' && s.charAt(loc + 1) <= 'Z'))
{
loc++;
}
// figure out why we stopped
int result;
if (loc == s.length() - 1)
{
result = -1; // not found
}
else
{
result = loc; // found at loc
}
System.out.println(result);