Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So far, in working with numbers we have worked with ints and doubles, where we h

ID: 3682392 • Letter: S

Question

So far, in working with numbers we have worked with ints and doubles, where we have learned that doubles are inherently inexace. Since we know a rational number can be represented as a pair of integers, our goal is to create an object Rational that we can treat with the same 'trust' as int but allows us more precision. 1. Download the abstract class Num interface Num 2. Write a class Int that extends Num. This class should have a single instance variable. It should also override definitions for the following methods: getApproxVal Compare toString In addition, you should have a constructor and a method getVal that returns the exact value of the Int as a Java primitive (int). 3. Write a java class Rational that has two instance variables: Int number : The number: The numberator Int denom: The denominator This class should override definitions for the same 3 methods as above. In addition it should have a constructor that takes two ints as well as the following methods (to replace getVal in Int); getnumber get Denom 4. Finally, write a class InvalidDemomException that extends RuntimeException. Modify the constructor in Rational to throw an InvalidDenomException if anyone tries to set the denominator to zero. You do not need a main method for this assignment. If you? Wish to lest your code, you may use the test class xovided here: Test Numbers As a final note, make sure that in all your Compare methods you Compare by the actual values of the numbers, not by the values returned by getApproxVal. This is example output given the provided test program Enter a rational number as two integers: 15 7 Enter an integer: 2 a = 15/7 b = 2 15/7

Explanation / Answer

import java.io.*;

class rational1

{

public rational1(){}

public long gcd(long a,long b)

{

if(b==0)

return a;

else

return gcd(b,a%b);

}

}

public class rational

{

public static void main(String args[])throws IOException

{

rational1 r=new rational1();

long a,b,x,y;

integer z;

String str;

DataInputStream in= new DataInputStream(System.in);

System.out.println("Enter rational number rat1");

str=in.readLine();

a=Integer.parseInt(str);

System.out.println("Enter the rational number rat2");

str=in.readLine();

b=Integer.parseInt(str);

long l=r.gcd(a,b);

System.out.println();

System.out.println("The Rational number is:"+l);

x=a/l;

y=b/l;

System.out.println();

System.out.println("The resultant value is: "+x+"/"+y);

System.out.println("Ente rthe integer");

str=in.readLine();

z=Integer.parseInt(str);

if ( l < z)

  System.out.println("Rational Number is true");

else

  System.out.println("Rational Number is false");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote