Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago