Question: Help me fix the code (JAVA) DIRECTIONS: Directed Lab Work Rational The skeleton of the Rational class already exists and is in Rational.java. Test code
Help me fix the code (JAVA)
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.
-----------------------------------------------------------------------------------------------------------------------------------------------------
public class Rational { private int num; private int denom; public Rational() { this.num = 1; this.denom = 1; } public Rational(final int n, final int d) { if (d == 0) { throw new ZeroDenominatorException("0 in denominator"); } this.num = n; this.denom = d; this.normalize(); } public int getNumerator() { return this.num; } public int getDenominator() { return this.denom; } public Rational negate() { return new Rational(-this.num, this.denom); } public Rational invert() { return new Rational(this.denom, this.num); } public Rational add(final Rational other) { final int n = this.num * other.denom + other.num * this.denom; final int d = this.denom * other.denom; return new Rational(n, d); } public Rational subtract(final Rational other) { return this.add(other.negate()); } public Rational multiply(final Rational other) { final int n = this.num * other.num; final int d = this.denom * other.denom; System.out.println(d); return new Rational(n, d); } public Rational divide(final Rational other) { return this.multiply(other.invert()); } private void normalize() { if (this.denom < 0) { this.denom = -this.denom; this.num = -this.num; } int a = this.num; final int b = this.denom; if (a < 0) { a = -a; } final int g = this.gcd(a, b); this.num /= g; this.denom /= g; } private int gcd(final int a, final int b) { int result = 0; if (a < b) { result = this.gcd(b, a); } else if (b == 0) { result = a; } else { final int remainder = a % b; result = this.gcd(b, remainder); } return result; } } -----------------------------------------------------------------------------------------------------------------------------------------------------
VERY IMPORTANT CODE BELOW!!!!
https://codeshare.io/G8Lw6D <---- RationalTest.java
https://codeshare.io/5OopYj <------ ZeroDenominatorException
https://codeshare.io/5QjQNm <------ CounterInitializationException
https://codeshare.io/2W7PBy <------ Counter.Java
^ Those code are already done you need these 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 Passes Trying 42/30 Passes Passes Trying 6/-3 Passes Passes Trying -6/-3 Passes Passes Trying -6/3 Passes Passes Trying 0/3 Passes Passes Constructor tests finished
TESTING the negate method Negate 1/2 Passes Passes Passes Passes Negate -2/3 Passes Passes Passes Passes Negate tests finished
TESTING the invert method Invert 1/2 Passes Passes Passes Passes Invert -2/3 Passes Passes Passes Passes Invert 0/5 Passes Invert tests finished
TESTING the add and subtract methods Adding 1/2 and 1/2 Passes Passes Passes Passes Adding 4/7 and 3/5 Passes Passes Passes Passes Passes Passes Adding 1/2 and 1/6 Passes Passes Subtracting 1/2 and 1/2 Passes Passes Passes Passes Subtracting 4/7 and 3/5 Passes Passes Passes Passes Passes Passes Subtracting 1/2 and 1/6 Passes Passes Add/Subtract tests finished
TESTING the multiply and divide methods Multiply 1/2 and 1/2 4 Passes Passes Passes Passes Multiply 5/7 and 3/5 35 Passes Passes Passes Passes Passes Passes Multiply 1/2 and 0/1 2 Passes **** Fails - denominator not 2 Dividing 1/2 by 1/2 2 Passes Passes Passes Passes Dividing 4/7 by 3/28 21 Passes Passes Passes Passes Passes Passes Dividing 1/2 by 1/6 2 Passes Passes Dividing 1/2 by 0/1 Passes Multiply/Divide tests finished
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
