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

Please help in JAVA: You are to write a program that gets ints from a file. Thes

ID: 3787418 • Letter: P

Question

Please help in JAVA: You are to write a program that gets ints from a file. These are numerators and denominators, and you are to create Fractions. The input file will contain 20 ints, 2 ints per line, the numerator and denominator of one Fraction. Put these Fractions into an ArrayList. Output the ArrayList. You will need to use a while loop for input, and for output you will need to use the size of the ArrayList.

For example, if I make the following statement in main Fraction f=new Fraction (20, 25); the constructor should call gcd(20, 25), which returns 5. The constructor then makes the numerator 4 (=20/5) and the denominator 5 (=25/5). Yes, this is integer division, but we don't worry about it because the greatest common divisor by definition divides both integers with no remainder.

This 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 static void main(String[] args) {
File inFile = new File("H5.in");
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
}
  
ArrayList <Fraction> myList=new ArrayList<Fraction>();

while(fileInput.hasNext()){
int x=fileInput.nextInt();
myList.add(x);
}
System.out.println(myList.size());
}
}
  

  

Explanation / Answer

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

       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 static void main(String[] args) {

       File inFile = new File("H5.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

*/

############## H5.in ########

4 5
6 5
1 2
9 6
45 34
12 4
9 20
43 12
78 54
10 10

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