Pedagogical Note
The exercises in Part 2 achieve three objectives :
Solutions for the UML diagrams for the even-numbered exercises can be downloaded from the Student Web Site and all others can be downloaded from the Instructor Web Site. |
7.1 | ( The Rectangle class ) Design a class named Rectangle to represent a rectangle. The class contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates two Rectangle objects. Assign width 4 and height 40 to the first object and width 3.5 and height 35.9 to the second object. Assign color red to all Rectangle objects. Display the properties of both objects and find their areas and perimeters . |
7.2 | ( The Fan class ) Design a class named Fan to represent a fan. The class contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates two Fan objects. Assign maximum speed, radius 10 , color yellow , and turn it on to the first object. Assign medium speed, radius 5 , color blue , and turn it off to the second object. Display the objects by invoking their toString method. |
7.3 | ( The Account class ) Design a class named Account that contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates an Account object with an account ID of 1122 , a balance of 20000 , and an annual interest rate of 4.5% . Use the withdraw method to withdraw $2500 , use the deposit method to deposit $3000 , and print the balance, the monthly interest, and the date when this account was created. |
| |
7.4 | ( The Stock class ) Design a class named Stock that contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates a Stock object with the stock symbol SUNW, the name Sun Microsystems Inc, and the previous closing price of 100 . Set a new current price to 90 and display the price-change percentage. |
7.5* | ( Using the GregorianCalendar class ) Java API has the GregorianCalendar class in the java.util package that can be used to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the methods get(GregorianCalendar.YEAR) , get(GregorianCalendar.MONTH) , and get(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day. Write a program to test this class to display the current year, month, and day. |
7.6** | ( Displaying calendars ) Rewrite the PrintCalendar class in §5.11, "Method Abstraction and Stepwise Refinement," to display calendars in a message dialog box. Since the output is generated from several static methods in the class, you may define a static String variable output for storing the output and display it in a message dialog box. |
7.7* | ( The Time class ) Design a class named Time . The class contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates two Time objects (using new Time() and new Time(555550000) ) and display their hour, minute, and second. |
Hint
The No-arg constructor uses the current time. The current time can be obtained using System.currentTimeMillis() , as shown in Listing 2.8, ShowCurrentTime.java. The other constructor sets the hour, minute, and second for the specified elapse time. For example, if the elapse time is 555550000 milliseconds, the hour is 10 , the minute is 19 , and the second is 10 . |
7.8* | ( The Course class ) The array size is fixed in Listing 7.11. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it. |
7.9 | ( The MyInteger class ) Design a class named MyInteger . The class contains:
Draw the UML diagram for the class. Implement the class. Write a client program that tests all methods in the class. | ||||||||||||||||||||||||||||||
7.10 | ( Modifying the Loan class ) Rewrite the Loan class to add two static methods for computing monthly payment and total payment, as follows : public static double monthlyPayment( double annualInterestRate, int numOfYears, double loanAmount) public static double totalPayment( double annualInterestRate, int numOfYears, double loanAmount) Write a client program to test these two methods. | ||||||||||||||||||||||||||||||
7.11 | ( The MyPoint class ) Design a class named MyPoint to represent a point with x and y -coordinates. The class contains:
Draw the UML diagram for the class. Implement the class. Write a test program that creates two points ( , ) and ( 10 , 30.5 ) and displays the distance between them. | ||||||||||||||||||||||||||||||
7.12* | ( Displaying the prime factors ) Write a program that receives a positive integer and displays all its smallest factors in decreasing order. For example, if the integer is 120 , the smallest factors are displayed as 5 , 3 , 2 , 2 , 2 . Use the StackOfIntegers class to store the factors (e.g., 2 , 2 , 2 , 3 , 5 ) and retrieve and display them in reverse order. | ||||||||||||||||||||||||||||||
7.13** | ( Displaying the prime numbers ) Write a program that displays all the prime numbers less than 120 in decreasing order. Use the StackOfIntegers class to store the prime numbers (e.g., 2 , 3 , 5 , ) and retrieve and display them in reverse order. | ||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
7.14*** | ( The Tax class ) Design a class named Tax to contain the following instance data fields:
Provide the get and set methods for each data field and the getTax() method that returns the tax. Also provide a no-arg constructor and the constructor Tax(filingStatus, brackets, rates, taxableIncome) . Draw the UML diagram for the class. Implement the class. Write a test program that uses the Tax class to print the 2001 and 2002 tax tables for taxable income from $50,000 to $60,000 with intervals of $1,000 for all four statuses. The tax rates for the year 2002 were given in Table 3.7. The tax rates for 2001 are shown in Table 7.1. Table 7.1. 2001 United States Federal Personal Tax Rates
|