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

need help writing this java program this should be output due us. 28.2016 Ration

ID: 3777295 • Letter: N

Question

need help writing this java program


this should be output


due us. 28.2016 Rational Numbers with write program to oes yovr elace Ust integer represent the private instance variables of the class. the nameralar and the denominaiau. Pnwide a col thoterables an object of dis class to be nsinuelor initialized when it's akrlared The ore the is equivalent to and would be stored in the object as I in mumerator the denominador. Provide a and 2 in constructor with default values in case no initializers are provided. Provide Public methods that perform each of the ay Add two Racional numbers: The result of the addition should be stored in by subtract two Rational numbers The result of the subtraction should be c) Multiply two Rational numbers. result ot the mul calion should be stored in reduced form dy vide two Rational numbers The result oree division should be stored e) Return a Sering representation of a Rational number in the form ab a is the numerator and b is the dennm Return a string represenlation of a Rational number in a floating point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right ofthe decimal point.)

Explanation / Answer

import java.text.NumberFormat; import java.util.Scanner; import java.util.concurrent.TimeUnit; public class Rational { private static Rational zero = new Rational(0, 1); private int mNumerator; private int mDenominator; public Rational(int numerator, int denominator) { mNumerator = numerator; mDenominator = denominator; } public Rational plus(Rational b) { Rational a = this; // special cases if (a.compareTo(zero) == 0) return b; if (b.compareTo(zero) == 0) return a; // Find gcd of numerators and denominators int f = gcd(a.getNumerator(), b.getNumerator()); int g = gcd(a.getDenominator(), b.getDenominator()); // add cross-product terms for numerator Rational s = new Rational((a.getNumerator() / f) * (b.getDenominator() / g) + (b.getNumerator() / f) * (a.getDenominator() / g), lcm(a.getDenominator(), b.getDenominator())); // multiply back in s.setNumerator(s.getNumerator() * f); return s; } public Rational minus(Rational b) { Rational a = this; return a.plus(b.negate()); } private Rational negate() { return new Rational(-getNumerator(), getDenominator()); } public Rational times(Rational b) { Rational a = this; // reduce p1/q2 and p2/q1, then multiply, where a = p1/q1 and b = p2/q2 Rational c = new Rational(a.getNumerator(), b.getDenominator()); Rational d = new Rational(b.getNumerator(), a.getDenominator()); return new Rational(c.getNumerator() * d.getNumerator(), c.getDenominator() * d.getDenominator()); } public Rational reciprocal() { return new Rational(getDenominator(), getNumerator()); } public Rational divides(Rational b) { Rational a = this; return a.times(b.reciprocal()); } public static void main(String args[]) { long startTime = System.currentTimeMillis(); System.out.print("Enter numerator 1: "); int numerator1 = getInput(); System.out.print("Enter denominator 1: "); int denominator1 = getInput(); System.out.print("Enter numerator 2: "); int numerator2 = getInput(); System.out.print("Enter denominator 2: "); int denominator2 = getInput(); System.out.print("Enter precision: "); int precision = getInput(); Rational x = new Rational(numerator1, denominator1); Rational y = new Rational(numerator2, denominator2); while (true) { printInstructions(); System.out.print("Choice: "); Rational result = null; switch (getInput()) { case 1: result = x.plus(y); System.out.println("a + b = " + result + " = " + result.getNoWithPrecision(precision)); break; case 2: result = x.minus(y); System.out.println("a - b = " + result + " = " + result.getNoWithPrecision(precision)); break; case 3: result = x.times(y); System.out.println("a * b = " + result + " = " + result.getNoWithPrecision(precision)); break; case 4: result = x.divides(y); System.out.println("a / b = " + result + " = " + result.getNoWithPrecision(precision)); break; case 5: long totalTime = System.currentTimeMillis() - startTime; System.out.println("BUILD SUCCESSFULL (total time: " + getPrettyPrintTime(totalTime) + ")"); return; default: break; } } } public String toString() { if (getDenominator() == 0) { if (getNumerator() == 0) return "NOT DEFINED"; else return "INFINITY"; } if (getDenominator() == 1) return getNumerator() + ""; else return getNumerator() + "/" + getDenominator(); } private static String getPrettyPrintTime(long millis) { return String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(millis), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)) ); } private String getNoWithPrecision(int precision) { if (getDenominator() == 0) { if (getNumerator() == 0) return "NOT DEFINED"; else return "INFINITY"; } NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(precision); nf.setMinimumFractionDigits(precision); String str = nf.format((double) getNumerator() / (double) getDenominator()); return str; } private int compareTo(Rational b) { Rational a = this; int lhs = a.getNumerator() * b.getDenominator(); int rhs = a.getDenominator() * b.getNumerator(); if (lhs rhs) return +1; return 0; } private int getNumerator() { return mNumerator; } private int getDenominator() { return mDenominator; } private void setNumerator(int numerator) { mNumerator = numerator; } private void setDenominator(int denominator) { mDenominator = denominator; } private int gcd(int m, int n) { if (m < 0) m = -m; if (n < 0) n = -n; if (0 == n) return m; else return gcd(n, m % n); } private int lcm(int m, int n) { if (m < 0) m = -m; if (n < 0) n = -n; return m * (n / gcd(m, n)); // parentheses important to avoid overflow } private static int getInput() { Scanner scanner = new Scanner(System.in); return scanner.nextInt(); } private static void printInstructions() { System.out.println("1. Add"); System.out.println("2. Subtract"); System.out.println("3. Multiply"); System.out.println("4. Divide"); System.out.println("5. Exit"); } }