Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 2: Programming Project No. 6 (Page 373-374): Part One: Define a class for rational numbers. A rational number is a number that can be

image text in transcribed
Problem 2: Programming Project No. 6 (Page 373-374): Part One: Define a class for rational numbers. A rational number is a number that can be represented as the quotient o two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so forth, we mean the everyday meaning of the fraction, not the integer division this expression would produce in a Java program.) Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Your class should have two instance variables of type int. Call the class Rational. Include a constructor with two arguments that can be used to set the instance variables of an object to any values. Also include a constructor that has only a single parameter of type int call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1 Also include a no-argument constructor that initializes an object to 0 (that is, to 0/1). Note that the numerator, the denominator, or both may contain a minus sign. Define methods for addition, subtraction, multiplication, and division of objects of your class Rational. These methods hould be static methods that each have two parameters o type Rational and return a value of type Rational. For example, Rational.add(rl, r2) will return the result of adding the two rational numbers (two objects of the class Rational, rl and r2). Define accessor and mutator methods as well as the methods equals and toString. You should include a method to normalize the sign of the rational number so that the denominator is positive and the numerator is either positive or negative. For example, after normalization, 4 would be represented the same as -4/8. Also write a test program to test your class Hints: Two rational numbers a/b and c/d are equal if a d equals c b Part Two: Add a second version of the methods for addition, subtraction, multiplication, and division. These methods hould have the same names as the static version but should use a calling object and a single argument. For example, this version of the add method (for addition) has a calling object and one argument. So rl.add(r2) returns the result of adding the rationals rl and r2. Note that your class should have al these methods; for example, there should be two methods named add

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

More Books

Students also viewed these Databases questions

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago

Question

b. Will new members be welcomed?

Answered: 1 week ago