# Question: please do it in java thank you directions directed lab...

###### Question details

**Please do it in JAVA!!! Thank you.**

**DIRECTIONS:**

Directed Lab Work Rational The skeleton of the Rational class already exists and is in Rational.java. Test code has been created and is in RationalTest.java. You will complete the methods for the Rational class.

Step 1. If you have not done so, look at the interface documentation in Rational.html. Look at the skeleton in Rational.java. All of the methods exist, but do not yet do anything. Compile the classes ZeroDenominatorException, Rational, and RationalTest. Run the main method in RationalTest. Checkpoint: If all has gone well, you should see test results. Don't worry for now about whether the test cases indicate pass or fail. Don't worry about the null pointer exception. All we want to see is that the Rational class has the correct protocol. Now you will complete the heart of the Rational class, its constructors, and basic accessor methods.

Step 2. Create the private data fields that will hold the state of a Rational object.

Step 3. Complete the default constructor. It should create the rational number 1.

Step 4. Complete the private method normalize. It should put the rational number in a normal form where the numerator and denominator share no common factors. Also, guarantee that only the numerator is negative. The gcd (greatest common divisor) method may be of use to you.

Step 5. Complete the alternate constructor. It should throw a new ZeroDenominatorException if needed. Don't forget to normalize.

Step 6. Complete the method getNumerator().

Step 7. Complete the method getDenominator(). Checkpoint: At this point there is enough to test. Your code should compile and pass all the tests in testConstructor(). If it fails any tests, debug and retest. The next two methods chosen for implementation are simple methods that construct a new rational number from an existing rational object.

Step 8. Complete the method negate(). Note that this method should not change the rational number it is invoked on, but instead return a new rational object. Don't forget to change the return statement. Currently it returns null, which means after executing the line of code Rational r2 = r1.negate(); the variable r2 will have the value null. If any methods are invoked on null (e.g., r2.getNumerator()) a null pointer exception will occur.

Checkpoint: Your code should compile and pass all the tests up to and including testNegate(). If it fails any tests, debug and retest. If you get null pointer exception before the test indicates it is finished with the negate testing, check what you are returning.

Step 9. Complete the method reciprocal(). Checkpoint: Your code should compile and pass all the tests through testInvert(). If it fails any tests, debug and retest. The next two methods chosen for implementation are closely related and will be tested together.

Step 10. Complete the method add(other). Lab Manual for Data Structures and Abstractions with Java ™ 9

Step 11. Complete the method subtract(other). There are a couple of ways that you can implement subtraction. One way is to use a formula similar to the one used for addition. Another way is to negate the second argument and then add. Either technique will work. Checkpoint: Your code should compile and pass all the tests through testAddSubtract(). If it fails any tests, debug and retest. Again the next two methods are closely related and will be implemented together.

Step 12. Complete the method multiply(other).

Step 13. Complete the method divide(other). Final checkpoint: Your code should compile and pass all the tests.

**---------RATIONAL SKELETON CODE:------Fill it in. Also
there are 2 other class that goes with rational to run it (They are
already done)! Just need to fill out rational and test if it
passes- Thank you**

/**

* A class that represents a rational number.

*

* **@author** Charles Hoot

* **@version** 4.0

*/

**public** **class** Rational

{

// PUT PRIVATE DATA FIELDS HERE

/**

* The default constructor for objects of class Rational. Creates the rational number 1.

*/

**public** Rational()

{

// ADD CODE TO THE CONSTRUCTOR

}

/**

* The alternate constructor for objects of class Rational. Creates a rational number equivalent to n/d.

* **@param** n The numerator of the
rational number.

* **@param** d The denominator of the
rational number.

*/

**public** Rational(**int** n,
**int** d)

{

// ADD CODE TO THE ALTERNATE CONSTRUCTOR

}

/**

* Get the value of the Numerator

*

* **@return** the value of the
numerator

*/

**public** **int** getNumerator()

{

// CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** 0;

}

/**

* Get the value of the Denominator

*

* **@return** the value of the
denominator

*/

**public** **int**
getDenominator()

{

// CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** 0;

}

/**

* Negate a rational number r

*

* **@return** a new rational number
that is negation of this number -r

*/

**public** Rational negate()

{

// CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** **null**;

}

/**

* Invert a rational number r

*

* **@return** a new rational number
that is 1/r.

*/

**public** Rational invert()

{

// CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** **null**;

}

/**

* Add two rational numbers

*

* **@param** other the second argument
of the add

* **@return** a new rational number
that is the sum of this and the other rational

*/

**public** Rational add(Rational other)

{

// ADD NEW CODE AND CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** **null**;

}

/**

* Subtract a rational number t from this one r

*

* **@param** other the second argument
of subtract

* **@return** a new rational number
that is r-t

*/

**public** Rational subtract(Rational other)

{

// CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** **null**;

}

/**

* Multiply two rational numbers

*

* **@param** other the second argument
of multiply

* **@return** a new rational number
that is the sum of this object and the other rational.

*/

**public** Rational multiply(Rational other)

{

// ADD NEW CODE AND CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** **null**;

}

/**

* Divide this rational number r by another one t

*

* **@param** other the second argument
of divide

* **@return** a new rational number
that is r/t

*/

**public** Rational divide(Rational other)

{

// CHANGE THE RETURN TO SOMETHING APPROPRIATE

**return** **null**;

}

/**

* Put the rational number in normal form where the numerator

* and the denominator share no common factors. Guarantee that only the numerator

* is negative.

*

*/

**private** **void** normalize()

{

// ADD CODE TO NORMALIZE THE RATIONAL NUMBER

}

/**

* Recursively compute the greatest common divisor of two positive integers

*

* **@param** a the first argument of
gcd

* **@param** b the second argument of
gcd

* **@return** the gcd of the two
arguments

*/

**private** **int**
gcd(**int** a, **int** b)

{

**int** result = 0;

**if**(a

result = gcd(b,a);

**else** **if**(b==0)

result = a;

**else**

{

**int** remainder = a % b;

result = gcd(b, remainder);

}

**return** result;

}

}

**VERY IMPORTANT CODE BELOW!!!!**

https://codeshare.io/G8Lw6D <---- RationalTest.java

https://codeshare.io/5OopYj <------ ZeroDenominatorException

**^ Those** **cod****e are
already** **done yo****u need these two
classes to run rational**

**sample output----- all of it has to pass**

TESTING the constructor, getNumerator, getDenominator Trying default constructor Passes Passes Constructing 2/5 Passes Passes Passes Trying 2/0 **** Fails - no exception thrown Trying 42/30 **** Fails - numerator not 7 **** Fails - denominator not 5 Trying 6/-3 **** Fails - numerator not -2 **** Fails - denominator not 1 Trying -6/-3 **** Fails - numerator not 2 **** Fails - denominator not 1 Trying -6/3 **** Fails - numerator not -2 **** Fails - denominator not 1 Trying 0/3 Passes **** Fails - denominator not 1 TESTING the negate method Negate 1/2 Passes Passes Passes Passes Negate -2/3 Passes Passes Passes Passes TESTING the invert method Invert 1/2 Passes Passes Passes Passes Invert -2/3 Passes Passes **** Fails - numerator not -3 **** Fails - denominator not 2 Invert 0/5 **** Fails - did not throw zero denominator exception TESTING the add and subtract methods Adding 1/2 and 1/2 Passes Passes **** Fails - numerator not 1 **** Fails - denominator not 1 Adding 4/7 and 3/5 Passes Passes Passes Passes Passes Passes Adding 1/2 and 1/6 **** Fails - numerator not 2 **** Fails - denominator not 3 Subtracting 1/2 and 1/2 Passes Passes Passes **** Fails - denominator not 1 Subtracting 4/7 and 3/5 Passes Passes Passes Passes Passes Passes Subtracting 1/2 and 1/6 **** Fails - numerator not 1 **** Fails - denominator not 3 TESTING the multiply and divide methods Multiply 1/2 and 1/2 Passes Passes Passes Passes Multiply 5/7 and 3/5 Passes Passes Passes Passes **** Fails - numerator not 3 **** Fails - denominator not 7 Multiply 1/2 and 0/1 Passes **** Fails - denominator not 1 Dividing 1/2 by 1/2 Passes Passes **** Fails - numerator not 1 **** Fails - denominator not 1 Dividing 4/7 by 3/28 Passes Passes Passes Passes **** Fails - numerator not 16 **** Fails - denominator not 3 Dividing 1/2 by 1/6 **** Fails - numerator not 3 **** Fails - denominator not 1 Dividing 1/2 by 0/1 **** Fails - did not throw zero denominator exception