📄 solveeven.txt
字号:
Solutions to Even Numbered Test Your Understanding Exercises
Copyright 1998 Arthur Gittleman
Chapter 1
4. 15 3950 5600 1234
6. An interpreter
8. Simple, Object-Oriented, and Familiar
Robust and Secure
Architecture Neutral and Portable
High Performance
Interpreted, Threaded, and Dynamic
10. The byte code will run on any machine that has a Java interpreter (virtual machine).
14. One possibility is:
Get the supplies ready.
Vacuum interior.
Rinse the car.
Mix the cleaning solution with water.
Loop until all surfaces clean.
Dip sponge in cleaning mixture.
Clean part of car surface.
Rinse suds off car.
Loop until car dry.
Dry part of car surface.
Wring out the towel.
Take special care with windows.
Polish trim.
Clean hubcabs and tires.
18. One possibility is:
The agent asks the customer to select a car.
The customer asks the agent to provide a mid-size convertible.
The agent asks the customer to specify a rental period.
The customer tells the agent that the rental is for one week.
The agent checks the computer system for a mid-size convertible.
The computer checks the inventory for a mid-size convertible.
The inventory shows a mid-size convertible is available.
The computer system checks the reservations for a conflict.
The reservations indicates no conflict.
The database tells the agent that such a car is available.
The agent requests a license and credit card from the customer.
The customer provides a license and a credit card.
The agent updates the system.
The system updates the inventory.
The agent provides the car to the customer.
Agent
Accept request for car.
Accept result of inquiry to system.
Customer
Provide rental type.
Provide license and credit card.
System
Accept request for rental.
Accept update.
Inventory
Accept request for car availability.
Accept update.
Reservation List
Accept request for conflict check.
Chapter 2
4. We assign number2 avalue in the next line of the program.
8. System.out.println("Number2 is now " + number2);
10. a. 5 b. 3 c. 4 d. -4 e. -305 f. 15 g. -1 h. -1
12. a. 4 b. -19 c. 4 d. 9 e. -2 f. 0 g. 8 h. -3 i. 1
14. y = -3
a = 84
x = -4
w = -1
b = -6
16. j and k are 5 and 4
j and k are 4 and 3
result and x are 10 and 8
result and y are 10 and 7
result and a are 7 and 4
result and b are -1 and -3
18. a. -44 b. 72 c. -20 d. -224 e. 136
22. The result is 102
24.
import iopack.Io;
public class Increment {
public static void main(String [ ] args) {
int x, y, a, b, j, k, result;
x = Io.readInt("Enter x");
y = Io.readInt("Enter y");
a = Io.readInt("Enter a");
b = Io.readInt("Enter b");
j = Io.readInt("Enter j");
k = Io.readInt("Enter k");
j++; ++k;
System.out.println("j and k are " + j + " and " + k);
j--; --k;
System.out.println("j and k are " + j + " and " + k);
result = 3 + x++;
System.out.println("result and x are " + result + " and " + x);
result = 3 + ++y;
System.out.println("result and y are " + result + " and " + y);
result = 2 + a--;
System.out.println("result and a are " + result + " and " + a);
result = 2 + --b;
System.out.println("result and b are " + result + " and " + b);
}
}
26. Declare a local variable inside a method. Refer to a local variable inside the method
in which it is declared. In Example 2.6, x, y, z, and result are local variables.
28. private int accountNumber;
30. null
32. The constructor BankAccount()
34. a. myAccount.deposit(3550); b. myAccount.getBalance(); c. myAccount.deposit(99900);
36. Method deposit(int) not found in class UseBankAccount (or something similar)
Chapter 3
2. a. true b. true c. false
4. a. true b. true c. false d. true
6. The >= sybol must not have any space between the two characters.
8. a. Use braces to make a block { z=7; x=5; }
b. Use parentheses (y>5)
c. Change ( to { and ) to }. Omit the final semicolon.
14. a. 8.93454 E2 b. 3.45722 E-4 c. 9.8761234 E7 d. 9.0909 E-2
16. a. 2.5 b. 2 c. 2 d. 2.4
18. Yes
20. No
22. a. 2 b. the loop never terminates c. 4
24. a. total=0; count=0; quantity=4;
0<4 score=95; total=95; count=1;
1<4 score=46; total=141; count=2;
2<4 score=68; total=209; count=3;
3<4 score=79; total=288; count=4;
The sum of the 4 scores is 288.
28. Follow the pseudocode in the text for finding the max, but change 'max' to 'min'
everywhere, and change '>' to '<'.
30. Find the maximum of a sequence of nonnegative numbers entered by the user.
Get the quantity of numbers.
Set max to zero.
Compare each number to the max, updating the max if necessary.
Output the result.
Get the quantity of numbers.
Set max to zero.
Set count of numbers processed to zero.
while (count < quantity)
Process next number.
Increment count.
Output the result.
Get the quantity of numbers.
Set max to zero.
Set count of numbers processed to zero.
while (count < quantity)
Get the next number.
If (number > max)
Set max to number.
Increment count.
Output the result.
32. Count the negative numbers in a sequence of numbers.
Get the quantity of numbers.
Set the amount of negative numbers to zero.
Set the count of numbers processed to zero.
Process each number.
Output the result.
Get the quantity of numbers.
Set the amount of negative numbers to zero.
Set the count of numbers processed to zero.
while(count < quantity)
Get the next number;
If (number < 0) Increment amount.
Increment count.
Output the result.
34. Replace the readInt statement with
int high = Io.readInt("Enter the number of squares to sum (<= 1860)");
while(high > 1860)
high = Io.readInt("Enter the number of squares to sum (<= 1860)");
36. public void withdraw(){
if(balance >= 4000)
balance -= 4000;
else
System.out.println("Insufficient funds");
}
Chapter 4
2. a. x= 3, y= 5 b. x = 2 y=2 c. x=12 y=12 d. x=12 y=12
4. a. 2 b. 7 c. 7 d. 12
6. a. 5 b. 5 c. 20 d. 10
8. if (amount >= 100 && amount < 499)
System.out.println("Thanks Supporter");
else if (amount >= 500 && amount <= 999)
System.out.println("Thanks Patron");
else if (amount >= 1000) System.out.println("Thanks Benefactor");
else System.out.println("Please contribute to out charity");
10. a. 8 b. 7 c. 6 d. 7
12. a. 6 b. 7 c. 8 d. 7
14. a. 9 b. 9 c. 9 d. 9 e. 9 f. 9 g. 9
16. a. 5 b. 6 c. 7 d. 5 e. 8 f. 5 g. 5
18. for(int i = 7; i<=12; i++) System.out.println(i);
20. for(int i=4; i<=20; i+=2) System.out.println(i);
22. 86
24. -70
26. 8
28. 20
32. a. Descartes said, "I think
therefore
I am"
b. set path=c:\java\bin
c. 1234678
d. 'i' before 'e' except after 'c'
34. No. Variables of type long require more space than those of type int. Arithmetic
operations on type long values take longer than operations on type int.
36. a. Math.pow(4,7); b. Math.sqrt(117.45); c. Math.max(32.1*33.9, 1000.2);
d. Math.min(Math.sqrt(32.0),5.613)); e. Math.abs(1089.9);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -