Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help in JAVA: You will get input from a file called H8.in. Input consists

ID: 3790309 • Letter: P

Question

Please help in 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 so 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.

Thanks!

Explanation / Answer

package sample;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class lib {   

public static void main(String args[]) {

FileInputStream fis = null;
BufferedReader reader = null;
Fraction large=null;
Fraction small=null;
  
try {
fis = new FileInputStream("C:/sample.txt");
reader = new BufferedReader(new InputStreamReader(fis));
  
String line = reader.readLine();
while(line != null){
System.out.println(line);
String parts[]=line.split(" ");
int num=Integer.parseInt(parts[0]);
int deno=Integer.parseInt(parts[1]);
if(num<0&&deno<0)
{
   num=-(num);
   deno=-(deno);
}
if(deno<0&&num>0){
   num=-(num);
}
Fraction f=new Fraction(num,deno);
if(large.compareTo(f)==1)
large =f;
   if(small.compareTo(f)==-1)
   small=f;
line = reader.readLine();
}   
  
} catch (Exception ex) {
System.out.println();
}
finally {
try {
reader.close();
fis.close();
} catch (IOException ex) {
Logger.getLogger(lib.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
class Fraction
{
private int num;
private int deno;
public int getNum() {
   return num;
}
public Fraction(int num, int deno) {
   super();
   this.num = num;
   this.deno = deno;
}
public void setNum(int num) {
   this.num = num;
}
public int getDeno() {
   return deno;
}
public void setDeno(int deno) {
   this.deno = deno;
}
public int compareTo(Fraction f){
   if((f.getNum()/f.getDeno())>(this.getNum()/this.getDeno()))return 1;
   else if((f.getNum()/f.getDeno())<(this.getNum()/this.getDeno()))return -1;
   else return 0;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote