File - Munoz AHISD

advertisement
PreAPCS Exposure Java Exercises 06.01- 04 Date:
Name: KEY
Period:
1.
What kind of methods did you use in chapters 4 and 5?
Class methods
2.
An _______ stores more than data, because data by itself lacks all _______.
object
functionality
3.
What does the word class imply?
The word class implies a collection of some category
4.
List 7 Math class methods.
abs, pow, sqrt, round, floor, ceil, min, max
5.
List 2 Math class data members.
PI and E
6.
Methods of the Math class are called what type of methods?
Class Methods
7.
If Bank is a class and checkingDeposit is an object method of the Bank class, then the statement
Bank.checkingDeposit(1000); is NOT a proper statement. Rewrite this statement properly to deposit
$1000 in your checking account.
Answers will vary: john.checkingDeposit(1000);
8.
To use an object method, an _______ must first be created.
object
Questions 9-12 refer to the following examples from the
GridWorld Case Study. In each example, you need to list
how many classes and how many objects are being shown.
9.
Number of classes: 1
Number of objects: 8
Exposure Java 2012, PreAPCS Edition
Exercises 06.01-04
Page 1
04-14-12
10.
Number of classes: 5
Number of objects: 5
11.
Number of classes: 2
Number of objects: 7
12.
Number of classes: 5
Number of objects: 41
Exposure Java 2012, PreAPCS Edition
Exercises 06.01-04
Page 2
04-14-12
13.
What was the primary motivation of introducing graphics programming back in Chapter IV?
to enhance your understanding of method calls and parameter passing
Refer to program Java0601.java for question 14.
14.
What is wrong with this program?
It tries to call object methods like class methods.
Refer to program Java0602.java for questions 15 through 17.
15.
How does this program fix the problem of previous program?
Separate objects are created for each Bank customer. These objects are used to call the object methods.
16.
In the statement Bank tom; what is Bank and what is tom?
Bank is the class and tom is the object.
17.
What does the statement tom = new Bank(); do?
It allocates space in memory and constructs the new object tom.
18.
For right now, the author wants you to think of constructing an object as a combination of what 2
things?
allocating space for all the object data and properly initializing each data member of the object
19.
Objects are usually created with some initial value or values. In the case of the Bank class objects are
created with a $0.00 balance. What special methods make this possible?
constructors
20.
Why would a class have more than one constructor?
This allows different objects to be created in different situations.
Refer to program Java0603.java for questions 21 through 23.
21.
Write 2 lines of Java code that will create a Bank object for yourself and initialize both your checking
and your savings account to $1 million.
Bank myAccount = new Bank(1000000,1000000);
22.
Write 1 line of Java code that will make a $250,000 deposit in your checking account.
myAccount.checkingDeposit(250000);
23.
Write 1 line of Java code that will make a $500,000 deposit in your savings account.
myAccount.savingsDeposit(500000);
24.
Rewrite these two statements as one:
String name = “John”;
String name; name = “John”;
25.
Rewrite these two statements as one:
Bank john = new Bank( );
Bank john; john = new Bank();
Exposure Java 2012, PreAPCS Edition
Exercises 06.01-04
Page 3
04-14-12
Refer to program Java0604.java for questions 26 and 27.
26.
Write 1 line of Java code that will make a $150,000 withdrawal from your checking account.
myAccount.checkingWithdrawal(150000);
27.
Write 1 line of Java code that will make a $400,000 withdrawal from your savings account.
myAccount.savingsWithdrawal(400000);
28.
Which class lets you control the output appearance of numbers?
DecimalFormat
Refer to program Java0605.java for questions 29 and 30.
29.
Write 1 line of Java code that will create a DecimalFormat object that will be used to display numbers
with up to 3 digits.
DecimalFormat output = new DecimalFormat("000");
30.
Write 1 line of Java code that will use the object you created in the previous question to display the
number “23” as “023”.
System.out.println(output.format(23));
Refer to program Java0606.java for question 31.
31.
If the following line of Java code was added to the program, what would be its output?
System.out.println(output.format(1234567890));
1,234,567,890
Refer to program Java0607.java for questions 32 through 34.
32.
Does the DecimalFormat object always need to be called output?
No
33.
If the following line of Java code was added to the program, what would be its output?
System.out.println(money.format(Math.PI));
$3.14
34.
If the following line of Java code was added to the program, what would be its output?
System.out.println(money.format(345.678));
$345.68
Refer to program Java0608.java for questions 35.
35.
Look at the output of this statement: System.out.println(output4.format(Math.PI));
The output is 3.1416. If we look at the next output, it is 3.1415 9. Why is the 4th digit a 5 in one line and
a 6 in the next?
When DecimalFormat rounds Math.PI to 4 decimal places, the 5 rounds to a 6 since the next digit is a 9
Exposure Java 2012, PreAPCS Edition
Exercises 06.01-04
Page 4
04-14-12
Download