Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

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;

}

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;

}

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;

}

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;

}

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() {

int n = numerator;

int d = denominator;

if(d < 0){

n = -n;

d = -d;

}

if(d == 1)

return Integer.toString(n);

return n + "/" + d;

}

private int gcd(int a, int b) {

int i = 0;

int c=0;

if(a < 0)

a = -a;

if(b < 0)

b = b;

if (b>0){

if (a

c=a;

}

else{

c= b;

}

for (i =c; i > 0; i--) {

if ((a % i == 0) && (b % i == 0)) {

break;

}

}

}

return i;

}

public int compareTo(Fraction f){

return f.denominator*this.numerator - f.numerator*this.denominator;

public static void main(String[] args) {

File inFile = new File("H8.in");

Scanner fileInput = null;

try {

fileInput = new Scanner(inFile);

} catch (FileNotFoundException ex) {

System.out.println("Error");

}

ArrayList A_L=new ArrayList()

while(fileInput.hasNext()){

int num=fileInput.nextInt();

int denom = fileInput.nextInt();

// create the fraction Object

Fraction f = new Fraction(num, denom);

A_L.add(f);

}

int i = 0;

while(i < A_L.size()){

System.out.println(A_L.get(i));

i++;

}

}

}

Please Help

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Address an envelope properly.

Answered: 1 week ago

Question

Discuss guidelines for ethical business communication.

Answered: 1 week ago