Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help in JAVA: This program should input numerator and denominator from a file, create a Fraction object (reducing the fraction if necessary) and then save

Help in JAVA:

This program should input numerator and denominator from a file, create a Fraction object (reducing the fraction if necessary) and then save the fraction to an ArrayList. This list will then be sorted, and output.

Make one method to input, create, and add to the ArrayList. Another method call to sort. And the third method to output the contents of the (sorted) ArrayList.

The input file will consist of an (unknown) quantity of ints representing numerator denominator pairs, which may be negative or zero.

You will need to think about your constructor - you will find it easier if you follow these rules

1. If both numerator and denominator are negative make them both positive

2. if the numerator is positive but the denominator is negative switch both so that the numerator is negative and the denominator positive

3. any other case leave as is.

what I have so far:

public static class Fraction {

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() { return numerator + "/" + denominator; }

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; } public int input(){ File inFile = new File("h7.txt"); Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { } fileInput.nextInt(); } public int sort(int input){ Collections.sort(); } public int output(){ System.out.println(); } } public static void main(String[] args) { } }

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

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

Students also viewed these Databases questions