Java Reference
In-Depth Information
PROGRAMMING EXERCISES
1. In Chapter 8, the class Clock was designed to implement the time of day in a
program. Certain applications, in addition to hours, minutes, and seconds, might
requireyoutostorethetimezone.Derivethe class ExtClock from the class
Clock by adding a data member to store the time zone. Add the necessary
methods and constructors to make the class functional. Also, write the definitions
of the methods and the constructors. Finally, write a test program to test your class.
2. In this chapter, the class Date was designed to implement the date in a
program, but the method setDate and the constructor with parameters do
not check whether the date is valid before storing the date in the data members.
Rewrite the definitions of the method setDate and the constructor with
parameters so that the values of month, day, and year are checked before storing
the date into the data members. Add a method isLeapYear to check whether a
year is a leap year. Then, write a test program to test your class.
3. A point in the x-y plane is represented by its x-coordinate and y-coordinate.
Design the class Point that can store and process a point in the x-y plane.
You should then perform operations on a point, such as showing the point,
setting the coordinates of the point, printing the coordinates of the point,
returning the x-coordinate, and returning the y-coordinate. Also, write a test
program to test various operations on a point.
4. Everycirclehasacenterandaradius.Giventheradius,wecandeterminethe
circle's area and circumference. Given the center, we can determine its position in
the x-y plane. The center of a circle is a point in the x-y plane. Design the class
Circle that can store the radius and center of the circle. Because the center is a
point in the x-y plane and you designed the class to capture the properties of a
point in Programming Exercise 3, you must derive the class Circle from the
class Point . You should be able to perform the usual operations on a circle,
such as setting the radius, printing the radius, calculating and printing the area and
circumference, and carrying out the usual operations on the center.
5. Every cylinder has a base and height, where the base is a circle. Design the class
Cylinder that can capture the properties of a cylinder and perform the usual
operations on a cylinder. Derive this class from the class Circle designed in
Programming Exercise 4. Some of the operations that can be performed on a
cylinder are as follows: calculate and print the volume, calculate and print the
surface area, set the height, set the radius of the base, and set the center of the base.
6. Using classes, design an online address book to keep track of the names,
addresses, phone numbers, and birthdays of family members, close friends,
and certain business associates. Your program should be able to handle a
maximum of 500 entries.
a. Define the class Address that can store a street address, city, state, and
zip code. Use the appropriate methods to print and store the address.
Also, use constructors to automatically initialize the data members.
1
0
 
Search WWH ::




Custom Search