Question: Lab 3 Inheritance and interface Task 1 Define a Rational class based on Number and Comparable A Rational number has a numerator and a denominator

Lab 3

Inheritance and interface

Task 1 Define a Rational class based on Number and Comparable

A Rational number has a numerator and a denominator in the form a/b, where a is the numerator and b the denominator. For example, 1/5, 13/4, and 7/9 are rational numbers. Java provides data types for integers and floating-point numbers, but not for rational numbers. It is helpful to define a Rational class for representing rational numbers. Since rational numbers share many features with integers and floating-point numbers, and Number is the root for numeric wrapper classes, it is appropriate to define Rational as a subclass of Number. Since rational numbers are comparable, the Rational class should also implement the Comparable interface. Figure 1 illustrates the Rational class and its relationship to the Number class and the Comparable interface. Write a program to test your Rational class, such as the additions, subtractions, multiplications, divisions, and comparisons for Rational numbers.

Lab 3 Inheritance and interface Task 1 Define a Rational class based

java.lang.Number java.lang.Comparable 4. Rational - numerator:long -denominator:long + Rarional +Rational(numerator:long,denom inator:long) +getNumerator:long +getDenominatorO:long +add(secondRational:Rational):R ational + subtraction(second Rational:Rati onal):Rational + multiply(secondRational:Ration al):Rational + divide (secondRational:Rational): Rational +toString0:String Figure 1. Rational Diagram Task 2 Abstract class, interface and subclasses Shape + calArea(): double + calPerimeter(): double > Compareable + compareTo(E); int Rectangle - width: double -height: double + calArea(): double + calArea(): double + compareTo(EO): int Figure 2. Rectangle Diagram Define abstract class Shape and subclass Rectangle. The compare To method compares the areas of two rectangles

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!