Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java problems Objectives Further understanding of object-oriented programming Problem solving using Java Rational Implement a class to represent rational numbers. Each rational number consists of

java problems

image text in transcribed

Objectives Further understanding of object-oriented programming Problem solving using Java Rational Implement a class to represent rational numbers. Each rational number consists of a numerator and a denominator, both of type int. Since each rational number has its own numerator and denominator, these must be instance variables. Furthermore, good object-oriented programming suggests that the visibility of the variables should be private. Constructors The class Rational has two constructors. One of them has two formal parameters, which provide the initial values for both instance variables. The other constructor has a single parameter, which provides the initial value for the numerator; the denominator is assumed to be 1. getters Implement access methods that return the numerator and denominator of this rational, but no setter methods. An object that has no setter methods, and no other methods for transforming the state of the object, is said to be immutable. Immutable is a great property. Do you see why? Discuss this with your neighbors and TA. plus Implement the instance method plus The method has a single formal parameter, of type Rational. The method returns a new Rational number that is the sum of this number and that of the parameter. plus Implement a class method plus. The method has two formal parameters, both of type Rational. The method returns a new Rational number that is the sum of the two numbers. ged Implement a private class method for calculating the greatest common divisor of two integers, which are the formal parameters of the method. reduce Implement a private instance method called reduce that transforms this number into its reduced form. reduce Make all the necessary changes so that a rational number is always stored in reduced form. equals Implement the instance method public boolean equals (Rational o) that returns true if this fraction and the one designated by o represent the same fraction (content equality). toString Add a method public String toString() that returns a String representation of this fraction with the numerator followed by the symbol "/", followed by the denominator. If the denominator is 1 then the method returns a String consisting of the numerator only. compareTo lmplements the instance method int compare To(Rational o I). It compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object

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

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago