Here is the code with comments to solve the question. Please do rate the answer if it helped.
Thank you very much.
Fraction.java
class Fraction
{
private int numerator; //where store the fraction datas
private int denominator;
//data members to store the reduced form of the fraction
//For ex: 6/3 can be stored in reduced form as 2/1.
//How to calculate the reduced form? We need the GCD of the 2 numbers and divide both
numerator and denominator
// by that GCD to give the reduced form
// for ex: 12/8 the GCD for 12 and 8 is 4 . so the numerator in reduced form is 12/4=3
//and denominator of reduced form is 8/4 = 2. So the reduced form of the fracton is 3/2
//GCD stands for greatest common divisor i.e the largest integer less than or equal to the 2
numbers
//which divides the numbers such that their remainder is 0
private int reducedNumerator;
private int reducedDenominator;
public Fraction(){ //default constructure
this(0,1);
}
public Fraction(int num, int denom){ //initialize the fraction
numerator = num;
denominator = denom;
computeReducedForm();
}
private void computeReducedForm()
{
//calculate the reduced form of the fraction
int gcd=GCD(); //compute gcd
reducedNumerator=numerator /gcd;
reducedDenominator=denominator/gcd;
}
//compares the reduced forms of the 2 fractions and returns true if they are equal and false
otherwise
public boolean equals(Fraction other){
return (reducedNumerator==other.reducedNumerator &&
reducedDenominator==other.reducedDenominator); //both numerator and denominator
should match
}
public void setNumerator(int num){
numerator=num;
computeReducedForm();
}
public void setDenominator(int denom){
denominator=denom;
computeReducedForm();
}
public int getNumerator(){
return numerator;
}
public int getDenominator(){
return denominator;
}
public String toString()
{
return reducedNumerator+\"/\"+reducedDenominator;
}
private int GCD()
{
int gcd=1;
for(int i=1;i<=numerator && i<=denominator;i++)
{
if(numerator % i ==0 && denominator % i==0) //should divide both with 0 remainder
gcd=i;
}
return gcd;
}
}
FractionCounter.java
public class FractionCounter
{
private Fraction theFraction;
private int counter;
public FractionCounter(Fraction frac) {
theFraction=frac;
counter=1;
}
public Fraction getFraction()
{
return theFraction;
}
public void incrementCount()
{
counter++;
}
public int getCount()
{
return counter;
}
public boolean compareFraction(Fraction other)
{
return theFraction.equals(other);
}
public String toString()
{
return theFraction+\" has a count of \"+counter;
}
}
Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner scanner;
try {
scanner = new Scanner(new File(\"fractions.txt\"));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
return;
}
String line,nums[];
int numerator,denominator;
Fraction fraction;
ArrayList countersList=new ArrayList(); //a list o.
Here is the code with comments to solve the question. Please do rate.pdf
1. Here is the code with comments to solve the question. Please do rate the answer if it helped.
Thank you very much.
Fraction.java
class Fraction
{
private int numerator; //where store the fraction datas
private int denominator;
//data members to store the reduced form of the fraction
//For ex: 6/3 can be stored in reduced form as 2/1.
//How to calculate the reduced form? We need the GCD of the 2 numbers and divide both
numerator and denominator
// by that GCD to give the reduced form
// for ex: 12/8 the GCD for 12 and 8 is 4 . so the numerator in reduced form is 12/4=3
//and denominator of reduced form is 8/4 = 2. So the reduced form of the fracton is 3/2
//GCD stands for greatest common divisor i.e the largest integer less than or equal to the 2
numbers
//which divides the numbers such that their remainder is 0
private int reducedNumerator;
private int reducedDenominator;
public Fraction(){ //default constructure
this(0,1);
}
public Fraction(int num, int denom){ //initialize the fraction
numerator = num;
denominator = denom;
computeReducedForm();
}
private void computeReducedForm()
{
2. //calculate the reduced form of the fraction
int gcd=GCD(); //compute gcd
reducedNumerator=numerator /gcd;
reducedDenominator=denominator/gcd;
}
//compares the reduced forms of the 2 fractions and returns true if they are equal and false
otherwise
public boolean equals(Fraction other){
return (reducedNumerator==other.reducedNumerator &&
reducedDenominator==other.reducedDenominator); //both numerator and denominator
should match
}
public void setNumerator(int num){
numerator=num;
computeReducedForm();
}
public void setDenominator(int denom){
denominator=denom;
computeReducedForm();
}
public int getNumerator(){
return numerator;
}
public int getDenominator(){
return denominator;
}
public String toString()
{
return reducedNumerator+"/"+reducedDenominator;
}
private int GCD()
3. {
int gcd=1;
for(int i=1;i<=numerator && i<=denominator;i++)
{
if(numerator % i ==0 && denominator % i==0) //should divide both with 0 remainder
gcd=i;
}
return gcd;
}
}
FractionCounter.java
public class FractionCounter
{
private Fraction theFraction;
private int counter;
public FractionCounter(Fraction frac) {
theFraction=frac;
counter=1;
}
public Fraction getFraction()
{
return theFraction;
}
public void incrementCount()
{
counter++;
}
public int getCount()
{
return counter;
}
4. public boolean compareFraction(Fraction other)
{
return theFraction.equals(other);
}
public String toString()
{
return theFraction+" has a count of "+counter;
}
}
Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner scanner;
try {
scanner = new Scanner(new File("fractions.txt"));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
return;
}
String line,nums[];
int numerator,denominator;
Fraction fraction;
ArrayList countersList=new ArrayList(); //a list of counters for fractions
FractionCounter counter;
boolean found;
while(scanner.hasNext())
{
line=scanner.nextLine().trim();
if(line.equals("")) continue; //skip over empty lines
5. nums=line.split("/"); //break into tokens usnig / as separator
numerator=Integer.parseInt(nums[0]);
denominator=Integer.parseInt(nums[1]);
fraction=new Fraction(numerator, denominator);
found=false;
//check the list if any counter already there, if yes then increment otherwise add a new
counter
for(int i=0;i
Solution
Here is the code with comments to solve the question. Please do rate the answer if it helped.
Thank you very much.
Fraction.java
class Fraction
{
private int numerator; //where store the fraction datas
private int denominator;
//data members to store the reduced form of the fraction
//For ex: 6/3 can be stored in reduced form as 2/1.
//How to calculate the reduced form? We need the GCD of the 2 numbers and divide both
numerator and denominator
// by that GCD to give the reduced form
// for ex: 12/8 the GCD for 12 and 8 is 4 . so the numerator in reduced form is 12/4=3
//and denominator of reduced form is 8/4 = 2. So the reduced form of the fracton is 3/2
//GCD stands for greatest common divisor i.e the largest integer less than or equal to the 2
numbers
//which divides the numbers such that their remainder is 0
private int reducedNumerator;
private int reducedDenominator;
public Fraction(){ //default constructure
this(0,1);
6. }
public Fraction(int num, int denom){ //initialize the fraction
numerator = num;
denominator = denom;
computeReducedForm();
}
private void computeReducedForm()
{
//calculate the reduced form of the fraction
int gcd=GCD(); //compute gcd
reducedNumerator=numerator /gcd;
reducedDenominator=denominator/gcd;
}
//compares the reduced forms of the 2 fractions and returns true if they are equal and false
otherwise
public boolean equals(Fraction other){
return (reducedNumerator==other.reducedNumerator &&
reducedDenominator==other.reducedDenominator); //both numerator and denominator
should match
}
public void setNumerator(int num){
numerator=num;
computeReducedForm();
}
public void setDenominator(int denom){
denominator=denom;
computeReducedForm();
}
public int getNumerator(){
return numerator;
7. }
public int getDenominator(){
return denominator;
}
public String toString()
{
return reducedNumerator+"/"+reducedDenominator;
}
private int GCD()
{
int gcd=1;
for(int i=1;i<=numerator && i<=denominator;i++)
{
if(numerator % i ==0 && denominator % i==0) //should divide both with 0 remainder
gcd=i;
}
return gcd;
}
}
FractionCounter.java
public class FractionCounter
{
private Fraction theFraction;
private int counter;
public FractionCounter(Fraction frac) {
theFraction=frac;
counter=1;
}
public Fraction getFraction()
{
return theFraction;
}
8. public void incrementCount()
{
counter++;
}
public int getCount()
{
return counter;
}
public boolean compareFraction(Fraction other)
{
return theFraction.equals(other);
}
public String toString()
{
return theFraction+" has a count of "+counter;
}
}
Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner scanner;
try {
scanner = new Scanner(new File("fractions.txt"));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
return;
}
9. String line,nums[];
int numerator,denominator;
Fraction fraction;
ArrayList countersList=new ArrayList(); //a list of counters for fractions
FractionCounter counter;
boolean found;
while(scanner.hasNext())
{
line=scanner.nextLine().trim();
if(line.equals("")) continue; //skip over empty lines
nums=line.split("/"); //break into tokens usnig / as separator
numerator=Integer.parseInt(nums[0]);
denominator=Integer.parseInt(nums[1]);
fraction=new Fraction(numerator, denominator);
found=false;
//check the list if any counter already there, if yes then increment otherwise add a new
counter
for(int i=0;i