Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

We need to start (again) writing modular code . 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 sofar:

import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;

public class H7 { public static class Fractions implements Comparable { //Attributes private int numerator; private int denominator; //Constuctors public Fractions() { numerator = 0; denominator=1; } public Fractions(int n, int d) { int g=gcd(n,d); numerator = n / g; denominator = d / g; if((n<=0&&d<=0)||(d<0&&n>=0)){ numerator = -n; denominator = -d; } } //Setters and gettrs public int getNumerator() { return numerator; }

public void setNumerator(int n) { this.numerator=n; }

public int getDenominator() { return denominator; }

public void setDenominator(int d) { this.denominator=d; } //toString public String toString() { return numerator + "/" + denominator; } //Calculates greatest common factor private int gcd(int int1, int int2) { if ((int1 % int2) == 0) { return int2; } else{ return gcd(int2, int1 % int2); } } public int compareTo(Fractions f) { if (f.numerator

return 1;

else if (f.numerator==this.numerator && f.denominator==f.denominator)

return 0;

else

return -1; } } public static void main(String[] args) { File inFile = new File("H7.txt"); Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { } //create array ArrayList myF = new ArrayList(); int n, d; //Loop to get fraction input and adding it to the array while(fileInput.hasNext()){ n = fileInput.nextInt(); d = fileInput.nextInt(); //Creating Fraction Fractions f = new Fractions(n, d); //Adding Fraction to list myF.add(f); } Collections.sort(myF); for (int i=0;i

Input File:

6 9 10 100 -2 -1 -1 2 8 9

Output:

1/10 1/-2 2/3 2/1 8/9

Having trouble sorting them least to greatest and if the numerator is positive but the denominator is negative switch both so that the numerator is negative and the denominator positive

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

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions