Java Reference
In-Depth Information
Exercises
8.2 (Based on Section 8.14) Explain the notion of package access in Java. Explain the negative
aspects of package access.
8.3 What happens when a return type, even void , is specified for a constructor?
8.4 (Rectangle Class) Create a class Rectangle with attributes length and width , each of which
defaults to 1. Provide methods that calculate the rectangle's perimeter and area. It has set and get
methods for both length and width . The set methods should verify that length and width are each
floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle .
8.5 (Modifying the Internal Data Representation of a Class) It would be perfectly reasonable
for the Time2 class of Fig. 8.5 to represent the time internally as the number of seconds since mid-
night rather than the three integer values hour , minute and second . Clients could use the same pub-
lic methods and get the same results. Modify the Time2 class of Fig. 8.5 to implement the time as
the number of seconds since midnight and show that no change is visible to the clients of the class.
8.6 (Savings Account Class) Create class SavingsAccount . Use a static variable annualInter-
estRate to store the annual interest rate for all account holders. Each object of the class contains a
private instance variable savingsBalance indicating the amount the saver currently has on deposit.
Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the
savingsBalance by annualInterestRate divided by 12—this interest should be added to savings-
Balance . Provide a static method modifyInterestRate that sets the annualInterestRate to a new
value. Write a program to test class SavingsAccount . Instantiate two savingsAccount objects,
saver1 and saver2 , with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate
to 4%, then calculate the monthly interest for each of 12 months and print the new balances for
both savers. Next, set the annualInterestRate to 5%, calculate the next month's interest and print
the new balances for both savers.
8.7 (Enhancing Class Time2 ) Modify class Time2 of Fig. 8.5 to include a tick method that in-
crements the time stored in a Time2 object by one second. Provide method incrementMinute to in-
crement the minute by one and method incrementHour to increment the hour by one. Write a
program that tests the tick method, the incrementMinute method and the incrementHour method
to ensure that they work correctly. Be sure to test the following cases:
a) incrementing into the next minute,
b) incrementing into the next hour and
c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
8.8 (Enhancing Class Date ) Modify class Date of Fig. 8.7 to perform error checking on the ini-
tializer values for instance variables month , day and year (currently it validates only the month and
day). Provide a method nextDay to increment the day by one. Write a program that tests method
nextDay in a loop that prints the date during each iteration to illustrate that the method works cor-
rectly. Test the following cases:
a) incrementing into the next month and
b) incrementing into the next year.
8.9 Rewrite the code in Fig. 8.14 to use a separate import declaration for each static member
of class Math that's used in the example.
8.10 Write an enum type TrafficLight , whose constants ( RED , GREEN , YELLOW ) take one parame-
ter—the duration of the light. Write a program to test the TrafficLight enum so that it displays the
enum constants and their durations.
8.11 (Complex Numbers) Create a class called Complex for performing arithmetic with complex
numbers. Complex numbers have the form
realPart + imaginaryPart * i
 
Search WWH ::




Custom Search