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

Write a program in code of java for the following information. Maximum: 1 / 2 Mi

ID: 3657454 • Letter: W

Question

Write a program in code of java for the following information.


Maximum: 1 / 2
Minimum: 3 / 8
Average: 17 / 40

Use the scanner class for input.

extending the program to find the minimum, the maximum and the average of N rational numbers, where N is specified by the user (N>3).

Explanation / Answer

import java.util.Scanner; class AddRationals { public static void main (String[] args) { String div; int a, b, c, d, e, f; Scanner scan = new Scanner(System.in); System.out.print("Type a rational number (a / b): "); a = scan.nextInt(); div = scan.next("/"); b = scan.nextInt(); System.out.print("Type another rational number (c / d): "); c = scan.nextInt(); div = scan.next("/"); d = scan.nextInt(); System.out.print("Type another rational number (e / f): "); e = scan.nextInt(); div = scan.next("/"); f = scan.nextInt(); Rational p = new Rational(a,b); // p points to a new instance of Rational Rational q = new Rational(c,d); // q points to a new instance of Rational Rational s = new Rational(e,f); // s points to a new instance of Rational Rational r = p.sum(q); // no need of new here, sum returns a new Ratonal object Rational t = r.sum(s); //add rationals to find final sum of all three rationals System.out.println(p + " + " + q + " = " + r); r.reduce(); System.out.println("Reduced sum is: " + r); if (decimal(p)>=decimal(q)&&decimal(p)>=decimal(s)) {Rational max = p;} // if (q>=p&&q>=s) maximum=q; // if (s> System.out.println("Maximum: " + max); System.out.println("Minimum: "); //Rational two = new Rational(2,1);//divide by 3,1 or 3/1 Rational ave = r.divide(new Rational(3,1));//three System.out.println("The average is " + ave); } } class Rational { private int numerator; private int denominator; public Rational (int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } public Rational sum (Rational r) { int num, den; den = denominator * r.getDenominator(); num = numerator * r.getDenominator() + r.getNumerator() * denominator; return new Rational (num, den); } public Rational divide (Rational r) { int num, den; num = numerator*r.getDenominator(); den = denominator*r.getNumerator(); return new Rational (num, den); } public double Decimal (Rational r) { double decimal; decimal = numerator/denominator; return decimal; //a*d>=b*c } public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } public String toString () { return numerator + "/" + denominator; } public void reduce() { int factor; factor = gcd(numerator, denominator); numerator = numerator / factor; denominator = denominator / factor; } private int gcd (int x, int y) { int t; while (y>0) { t = x % y; x = y; y = t; } return x; } }

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