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

Homework 8 Generalized Arithmetic Objects, with Exceptions Description: So far,

ID: 3682621 • Letter: H

Question

Homework 8

Generalized Arithmetic Objects, with Exceptions

Description:

So far, in working with numbers we have worked with ints and doubles, where we have learned that doubles are inherently inexact. 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.

Objective:

Download the abstract class Num interface Num

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).

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

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

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

Remember that your source code MUST have appropriate headers and MUST contain appropriate comments.

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.val