Chapter 8 Object-Based Programming 441 ment constructor with (Make my own web site)

Chapter 8 Object-Based Programming 441 ment constructor with default values in case no initializers are provided. Provide public methods for each of the following: a) Addition of two Complexnumbers: The real parts are added together and the imaginary parts are added together. b) Subtraction of two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. c) Printing Complex numbers in the form (a,b), where a is the real part and b is the imaginary part. 8.3 Create a class called Rational for performing arithmetic with fractions. Write a driver program to test your class. Use integer variables to represent the private instance variables of the class the numerator and the denominator. Provide a constructor method that enables an object of this class to be initialized when it is declared. The constructor should store the fraction in reduced form (i.e., the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator). Provide a no- argument constructor with default values in case no initializers are provided. Provide public meth ods for each of the following: a) Addition of two Rational numbers. The result of the addition should be stored in reduced form. b) Subtraction of two Rational numbers. The result of the subtraction should be stored in reduced form. c) Multiplication of two Rational numbers. The result of the multiplication should be stored in reduced form. d) Division of two Rational numbers. The result of the division should be stored in reduced form. e) Printing Rational numbers in the form a/b, where a is the numeratorand b is the denominator. f) Printing Rational numbers in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.) 8.4 Modify the Time3 class of Fig. 8.8 to include the tick method that increments the time stored in a Time3 object by one second. Also provide method incrementMinute to increment the minute and method incrementHour to increment the hour. The Time3 object should always remain in a consistent state. Write a driver program that tests the tick method, the increment- Minute 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. c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM). 8.5 Modify the Date class of Fig. 8.13 to perform error-checking on the initializer values for instance variables month, day and year (currently it validates only the month and day). Also, provide a method nextDay to increment the day by one. The Date object should always remain in a consistent state. Write a driver program that tests the nextDay method in a loop that prints the date during each iteration of the loop to illustrate that the nextDay method works correctly. Be sure to test the following cases: a) incrementing into the next month. b) incrementing into the next year. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply