You are on page 1of 5

CS2010.

03/4
Dr. Zhizhang Shen

Solutions of Some Problems in Chapter 8

Fall 2004

1. A Java declaration for one real number quantity to be called rate, initialized to 5.0 is
float rate = 5.0;
2. A single Java statement that declares two integer quantities is
int orderOne, orderTwo = 0;
3. The declaration could be the following:
public static void main(String[] args){
char choice;
int inventory;
float sales;
choice = ;
inventory = sales = 0;
4. (a)
(b)

System.out.print(Math.PI);
public static final double EVAPORATION_RATE = 6.15;

5. Your program will need four variables, three to store the three quiz grades, and one to store
the average. The declarations would look like the following,
int quiz1,quiz2,quiz3,average;
6. Since the index begins with 0, one would access the eighth number in the array with the
following expression: list[7].
7. It might look like the following:
float[][] box = new float[5][7];
8. Again, since both indices begin with 0, one would access that cell by the following: table[2][2].
9. A three dimensional table would be appropriate for such geometric problems. The declaration
to implement the data structure could be the following:
float point[][][] = float[100][100][100];
11. The output statement could be the following:
System.out.println("inventoryNumber is " + inventoryNumber +
"\nnumberOrdered is " + numberOrdered);
1

12. The output is the following:


Your age is32and your weight is187.
13. The segment could be the following:
import java.text.*;
public static void main(String[] args){
DecimalFormat p = new DecimalFormat("0.0");
System.out.println("The current density is "+ p.format(density) +",
to within one decimal place.");
14. The output is 66.
15. The method coudl be the following:
public static void main(String[] args){
int length, width, area;
length = Console.readInt("Enter the length of the rectangle");
width = Console.readInt("Enter the width of the rectangle");
area = length*width;
System.out.println("The area is "+area);
}
16. (a) The revised condition in the program is
if (taskToDo == C || taskToDo == c)
(b) The rewritten condition to allow an uppercase or lowercase response is
while (more == Y || more == y)
17. The method could be the following:
public static void main(String[] args){
char input;
input = Console.readChar("Please enter a single character.);
if (input == a || input == e || input == i || input == o || input == u)
System.out.println("Congratulations!");
else
System.out.println("you lose, better luck next time");
}
18. The missing line is score++; or
score = score + 1;
19. The output is
2

1
2
3
4
5
6
7
8

20
19
18
17
16
15
14
13

29. The method could be the following:


public int max(int a, int b, int c){
if (a < b && b < c)
return c;
else if (a < b && c < b)
return b;
else
return a;
}
33. The completed program could be the following:
class CheckbookApp{
public static void main(String[] args){
double balance;
int choice;
balance = Console.readDouble("Please enter the initial balance");
while (true){
choice = Console.readInt("Menu: 1. Make deposit\n2. Make withdrawal\n
3. Exit\nPlease enter a selection");
if (choice == 1)
balance = Checkbook.handledeposit(balance);
else if (choice == 2)
balance = Checkbook.handlecheck(balance);
else
break;
}
System.out.println("Your balance is " + balance);
}
}
class Checkbook{
public static double handledeposit(double current){
double returned, deposit;
deposit = Console.readDouble("What is your deposit?");
System.out.println("Your deposit is "+deposit);
3

returned = current + deposit;


System.out.println("Your new balance is "+returned);
return returned;
}
public static double handlecheck(double current){
double returned, check;
check = Console.readDouble("What is your withdrawal?");
System.out.println("Your withdrawal is "+check);
returned = current - check;
System.out.println("Your new balance is "+returned);
return returned;
}
class CheckbookApp{
public static void main(String[] args){
double balance;
int choice;
balance = Console.readDouble("Please enter the initial balance");
while (true){
choice = Console.readInt("Menu: 1. Make deposit\n2. Make withdrawal\n
3. Exit\nPlease enter a selection");
if (choice == 1)
balance = Checkbook.handledeposit(balance);
else if (choice == 2)
balance = Checkbook.handlecheck(balance);
else
break;
}
System.out.println("Your balance is " + balance);
}
}
class Checkbook{
public static double handledeposit(double current){
double returned, deposit;
deposit = Console.readDouble("What is your deposit?");
System.out.println("Your deposit is "+deposit);
returned = current + deposit;
System.out.println("Your new balance is "+returned);
return returned;
}
public static double handlecheck(double current){
double returned, check;
check = Console.readDouble("What is your withdrawal?");
System.out.println("Your withdrawal is "+check);
returned = current - check;
System.out.println("Your new balance is "+returned);
4

return returned;
}

You might also like