8.2. The String Class

 
[Page 255 ( continued )]

Programming Exercises

Pedagogical Note

The exercises in Part 2 achieve three objectives :

  1. Design and draw UML for classes.

  2. Implement classes from the UML.

  3. Use classes to develop applications.

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.


Sections 7.2 “7.11

7.1 ( The Rectangle class ) Design a class named Rectangle to represent a rectangle. The class contains:
  • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height .

  • A string data field named color that specifies the color of a rectangle. Hypothetically, assume that all rectangles have the same color. The default color is white.

  • A no-arg constructor that creates a default rectangle.

  • A constructor that creates a rectangle with the specified width and height .

  • The accessor and mutator methods for all three data fields.

  • A method named getArea() that returns the area of this rectangle.

  • A method named getPerimeter() that returns the perimeter.


[Page 256]

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:
  • Three constants named SLOW , MEDIUM , and FAST with values 1 , 2 , and 3 to denote the fan speed.

  • An int data field named speed that specifies the speed of the fan (default SLOW ).

  • A boolean data field named on that specifies whether the fan is on (default false ).

  • A double data field named radius that specifies the radius of the fan (default 5 ).

  • A string data field named color that specifies the color of the fan (default blue ).

  • A no-arg constructor that creates a default fan.

  • The accessor and mutator methods for all four data fields.

  • A method named toString() that returns a string description for the fan. If the fan is on, the method returns the fan speed, color, and radius in one combined string. If the fan is not on, the method returns fan color and radius along with the string "fan is off" in one combined string.

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:
  • An int data field named id for the account (default ).

  • A double data field named balance for the account (default ).

  • A double data field named annualInterestRate that stores the current interest rate (default ).

  • A Date data field named dateCreated that stores the date when the account was created.

  • A no-arg constructor that creates a default account.

  • The accessor and mutator methods for id , balance , and annualInterestRate .

  • The accessor method for dateCreated .

  • A method named getMonthlyInterestRate() that returns the monthly interest rate.

  • A method named withDraw that withdraws a specified amount from the account.

  • A method named deposit that deposits a specified amount to the account.

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.


[Page 257]
7.4 ( The Stock class ) Design a class named Stock that contains:
  • A string data field named symbol for the stock's symbol.

  • A string data field named name for the stock's name.

  • A double data field named previousClosingPrice that stores the stock price for the previous day.

  • A double data field named currentPrice that stores the stock price for the current time.

  • A constructor that creates a stock with specified symbol and name.

  • The accessor methods for all data fields.

  • The mutator methods for previousClosingPrice and currentPrice .

  • A method named changePercent() that returns the percentage changed from previousClosingPrice to currentPrice .

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:
  • Data fields hour , minute , and second that represents a time.

  • A no-arg constructor that creates a Time object for the current time. (The data fields value will represent the current time)

  • A constructor that constructs a Time object with a specified elapse time since the middle night, January 1, 1970 in milliseconds . (The data fields value will represent this time.)

  • Three get methods for the data fields hour , minute , and second , respectively.

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.

[Page 258]

Sections 7.12 “7.16

7.9 ( The MyInteger class ) Design a class named MyInteger . The class contains:
  • An int data field named value that stores the int value represented by this object.

  • A constructor that creates a MyInteger object for the specified int value.

  • A get method that returns the int value.

  • Methods isEven() , isOdd() , and isPrime() that return true if the value is even, odd, or prime, respectively.

  • Static methods isEven(int) , isOdd(int) , and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.

  • Static methods isEven(MyInteger) , isOdd(MyInteger) , and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.

  • Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.

  • A static method parseInt(int) that converts a string to an int value.

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:
  • Two data fields x and y that represent the coordinates.

  • A no-arg constructor that creates a point ( , ).

  • A constructor that constructs a point with specified coordinates.

  • Two get methods for data fields x and y , respectively.

  • A method named distance that returns the distance from this point to another point of the MyPoint type.

  • A method named distance that returns the distance from this point to another point with specified x and y -coordinates.

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.

[Page 259]
7.14*** ( The Tax class ) Design a class named Tax to contain the following instance data fields:
  • int filingStatus : One of the four tax filing statuses: ” single filer , 1 ” married filing jointly, 2 ” married filing separately, and 3 ” head of household. Use the public static constants SINGLE_FILER ( ), MARRIED_JOINTLY ( 1 ), MARRIED_SEPARATELY ( 2 ), HEAD_OF_HOUSEHOLD ( 3 ) to represent the status.

  • int[][] brackets : Stores the tax brackets for each filing status (see Listing 6.11, ComputeTax.java).

  • double[] rates : Stores tax rates for each bracket (see Listing 6.11).

  • double taxableIncome : Stores the taxable income.

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

[Page 260]
Tax rate Single filers Married filing jointly or qualifying widow(er) Married filing separately Head of household
15% Up to $27,050 Up to $45,200 Up to $22,600 Up to $36,250
27.5% $27,051 “$65,550 $45,201 “$109,250 $22,601 “$54,625 $36,251 “$93,650
30.5% $65,551 “$136,750 $109,251 “$166,500 $54,626 “$83,250 $93,651 “$151,650
35.5% $136,751 “$297,350 $166,501 “$297,350 $83,251 “$148,675 $151,651 “$297,350
39.1% $297,351 or more $297,351 or more $148,676 or more $297,351 or more

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net