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.
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(): doubleStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started