JAVA You will get input from a file called H8in Input consis
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 <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;
 }
 //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);
 }
 }
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public 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() {
int n = numerator;
int d = denominator;
if(d < 0){
n = -n; // converting n as negative
d = -d; // converting d as positive
}
if(d == 1)
return Integer.toString(n);
return n + \"/\" + d;
}
private int gcd(int int1, int int2) {
int i = 0;
int smallest=0;
// if any of two numbers are negative, make it positive
if(int1 < 0)
int1 = -int1;
if(int2 < 0)
int2 = -int2;
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 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 in opening in input file\");
System.exit(0);
}
ArrayList <Fraction> myList=new ArrayList<Fraction>();
while(fileInput.hasNext()){
int num=fileInput.nextInt();
int denom = fileInput.nextInt();
// creating fraction Object
Fraction f = new Fraction(num, denom);
// adding f in list
myList.add(f);
}
int i = 0;
while(i < myList.size()){
System.out.println(myList.get(i));
i++;
}
}
}
/*
Sample output:
4/5
6/5
1/2
3/2
45/34
3/1
9/20
43/12
13/9
1/1
*/
############ H8.in ########
4 -5
6 5
1 2
-9 -6
45 34
12 4
9 20
43 12
-78 54
10 10








