Question: JAVA: You will get input from a file called H8.in. Input consists of integers representing the numerator and denominator of fractions. NOTE that some numerators

JAVA:

You will get input from a file called H8.in.

Input consists of integers representing the numerator and denominator of fractions. NOTE that some numerators and some denominators will be negative.

As you input 2 integers create a Fraction object. Output the Fraction. (I want to see that your gcd method is working even for negative numbers). I DO NOT want to see any negative denominators. If both numerator and denominator are negative make both positive, if the denominator is negative but the numerator is positive then flip those signs so that the numerator is negative and the denominator positive. This is so that you do not output 3/-4, but rather -3/4.

DO NOT store these Fractions in any data structure (such as an array or ArrayList). Just keep track of the biggest and the smallest.

At the end of your program output the biggest and the smallest.

You will need to alter your Fraction class to include a compareTo method, and you will need to consider carefully how you know one fraction is less than another.

What I have:

//fraction class public static class Fraction implements Comparable { private int numerator; private int denominator;

public Fraction() { numerator = 0; denominator = 1; }

public Fraction(int n, int d) { int g = gcd(n, d); numerator = n/g; denominator = d/g; }

public int getNumerator() { return numerator; }

public void setNumerator(int n) { int d = denominator; int g = gcd(n, d); numerator = n / g; denominator= d/g; }

public int getDenominator() { return denominator; }

public void setDenominator(int d) { int n = numerator; int g = gcd(n, d); denominator = d / g; numerator= n/g; } //add public Fraction add(Fraction g) { int a = this.numerator; int b = this.denominator; int c = g.numerator; int d = g.denominator; Fraction v = new Fraction(a * d + b * c, b * d); return v; } //subtract public Fraction subtract(Fraction g) { int a = this.numerator; int b = this.denominator; int c = g.numerator; int d = g.denominator; Fraction v = new Fraction(a * d - b * c, b * d); return v; } //multiply public Fraction multiply(Fraction g) { int a = this.numerator; int b = this.denominator; int c = g.numerator; int d = g.denominator; Fraction v = new Fraction(a * c, b * d); return v; } //divide public Fraction divide(Fraction g) { int a = this.numerator; int b = this.denominator; int c = g.numerator; int d = g.denominator; Fraction v = new Fraction(a * d, b * c); return v; }

public String toString() { return numerator + "/" + denominator; } //gcd private int gcd(int int1, int int2) { int i = 0; int smallest=0; if (int2>0){ if (int1 < int2) { smallest=int1; } else{ smallest= int2; } for (i = smallest; i > 0; i--) {

if ((int1 % i == 0) && (int2 % i == 0)) { break; }

} } return i; } //sign public char Sign(){ if(numerator*denominator < 0) {return '-'; } else{ return '+'; } } //switch sign public void switchSign(){ if (denominator < 0){ denominator=-denominator; numerator=-numerator;} } //compareTo public int compareTo(Fraction f){ return f.denominator*this.numerator - f.numerator*this.denominator; } }

public static void main(String[] args) { //input from file File inFile = new File("H8.in"); Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { } int frac=fileInput.nextInt(); //get first frac and make it the biggest; Fraction f1 = new Fraction(); Fraction f2 = f1; //while loop while (fileInput.hasNextInt()){ frac=fileInput.nextInt(); //second frac Fraction f3 = new Fraction(); if (f3.compareTo(f2)>0) f2=f3; } //output big and small System.out.println("The biggest frac is: "+f2); System.out.println("The smallest frac is: "+f1); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!