Objective: 1. Download the interface Num ( https://cse.sc.edu/~pade/csce145/assi
ID: 3682505 • Letter: O
Question
Objective:
1. Download the interface Num (https://cse.sc.edu/~pade/csce145/assign/HW08/Num.java)
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 numer : The numerator
----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):
----getNumer
----getDenom
4. Finally, write a class InvalidDenomException 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 test your code, you may use the test class provided here: TestNumbers (https://cse.sc.edu/~pade/csce145/assign/HW08/TestNumbers.java)
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.
Example:
This is example output given the provided test program
Finally:
Submit Int.java, Rational.java, and InvalidDenomException.java to the dropbox
Explanation / Answer
import java.util.InputMismatchException; import java.util.Scanner; public class TestNumbers { public static void main (String[] args) { Scanner console = new Scanner(System.in); int numer; int denom; String input = ""; while ( !input.equals("exit") ) { System.out.print("Enter a rational number as two "); System.out.print("integers: "); try { numer = console.nextInt(); denom = console.nextInt(); } catch( InputMismatchException e ) { input = console.nextLine(); continue; } Rational rational; try { rational = new Rational(new Int(numer),new Int(denom)); } catch ( InvalidDenomException e ) { System.out.println(e.getMessage()); System.out.println(); continue; } System.out.print("Enter an integer: "); Int integer; try { integer = new Int(console.nextInt()); } catch ( InputMismatchException e ) { input = console.nextLine(); continue; } System.out.println("a = " + rational); System.out.println("b = " + integer); System.out.println(integer + " < " + rational + " = " + integer.lessThan(rational)); System.out.println(); } } } class Int implements Num{ int val; public Int(int val) { this.val = val; } public int getVal() { return val; } @Override public int compare(Num that) { return this.val-(int)that.getApproxVal(); } @Override public double getApproxVal() { return this.val; } @Override public boolean lessThan(Num that) { return false; } @Override public String toString() { return val+""; } // @Override public boolean lessThan(Rational that) { int d = (int)(that.numer.val/that.denom.val); return this.valRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.