10.10. Review Questions

 
[Page 338 ( continued )]

Programming Exercises

Sections 9.2 “9.4

9.1 ( The Triangle class ) Design a class named Triangle that extends GeometricObject . The class contains:
  • Three double data fields named side1 , side2 , and side3 with default values 1.0 to denote three sides of the triangle.

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

  • A constructor that creates a rectangle with the specified side1 , side2 , and side3 .

  • The accessor methods for all three data fields.

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

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

  • A method named toString() that returns a string description for the triangle.

For the formula to compute the area of a triangle, see Exercise 5.19. The toString() method is implemented as follows :

   return   "Triangle: side1 = "   + side1 +   " side2 = "   + side2 +   " side3 = "   + side3; 

Draw the UML diagram that involves the classes Triangle and GeometricObject . Implement the class. Write a test program that creates a Triangle object with sides 1 , 1.5 , 1 , sets color yellow and filled true , and displays the area, perimeter, color, and whether filled or not.

Sections 9.5 “9.9

9.2 ( The Person , Student , Employee , Faculty , and Staff classes ) Design a class named Person and its two subclasses named Student and Employee . Make Faculty and Staff subclasses of Employee . A person has a name , address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date- hired . Define a class named MyDate that contains the fields year , month , and day . A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person's name.
[Page 339]

Draw the UML diagram for the classes. Implement the classes. Write a test program that creates a Person , Student , Employee , Faculty , and Staff , and invokes their toString() methods.

9.3 ( Subclasses of Account ) In Exercise 7.3, the Account class was created to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw. Create two subclasses for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.

Draw the UML diagram for the classes. Implement the classes. Write a test program that creates objects of Account , SavingsAccount , and CheckingAccount , and invokes their toString() methods.

9.4 ( Implementing MyStack using inheritance ) In Listing 9.8, MyStack is implemented using composition. Create a new stack class that extends MyArrayList .

Draw the UML diagram for the classes. Implement MyStack .

9.5 ( The Course class ) Rewrite the Course class in Listing 7.11. Use an ArrayList to replace an array to store students.
9.6 ( Using ArrayList ) Write a program that creates an ArrayList , adds a Loan object, a Date object, a string, a JFrame object, and a Circle object to the list, and uses a loop to display all the elements in the list by invoking the object's toString() method.
[Page 340]
 


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