SlideShare a Scribd company logo
1 of 9
Download to read offline
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 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
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);
}
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 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

More Related Content

Similar to Here is the code with comments to solve the question. Please do rate.pdf

Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfsutharbharat59
 
Ejemplos Programas Descompilados
Ejemplos Programas DescompiladosEjemplos Programas Descompilados
Ejemplos Programas DescompiladosLuis Viteri
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,pptAllNewTeach
 
Write a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdfWrite a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdfleventhalbrad49439
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cppgourav kottawar
 
Cmis 212 module 2 assignment
Cmis 212 module 2 assignmentCmis 212 module 2 assignment
Cmis 212 module 2 assignmentzjkdg986
 
Cmis 212 module 2 assignment
Cmis 212 module 2 assignmentCmis 212 module 2 assignment
Cmis 212 module 2 assignmentsdfgsdg36
 
Cmis 212 module 2 assignment
Cmis 212 module 2 assignmentCmis 212 module 2 assignment
Cmis 212 module 2 assignmentfdjfjfy4498
 
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdfexxonzone
 
Java Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdfJava Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdfaroramobiles1
 
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfravikapoorindia
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...
ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...
ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...ScyllaDB
 
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfCSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfmarketing413921
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfAggarwalelectronic18
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003R696
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsAbed Bukhari
 

Similar to Here is the code with comments to solve the question. Please do rate.pdf (20)

Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
 
Ejemplos Programas Descompilados
Ejemplos Programas DescompiladosEjemplos Programas Descompilados
Ejemplos Programas Descompilados
 
Functions
FunctionsFunctions
Functions
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
Write a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdfWrite a program that works with fractions. You are first to implemen.pdf
Write a program that works with fractions. You are first to implemen.pdf
 
classes & objects in cpp
classes & objects in cppclasses & objects in cpp
classes & objects in cpp
 
Cmis 212 module 2 assignment
Cmis 212 module 2 assignmentCmis 212 module 2 assignment
Cmis 212 module 2 assignment
 
Cmis 212 module 2 assignment
Cmis 212 module 2 assignmentCmis 212 module 2 assignment
Cmis 212 module 2 assignment
 
Cmis 212 module 2 assignment
Cmis 212 module 2 assignmentCmis 212 module 2 assignment
Cmis 212 module 2 assignment
 
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf#ifndef RATIONAL_H    if this compiler macro is not defined #def.pdf
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
 
Java Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdfJava Programpublic class Fraction {   instance variablesin.pdf
Java Programpublic class Fraction {   instance variablesin.pdf
 
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...
ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...
ceph::errorator<> throw/catch-free, compile time-checked exceptions for seast...
 
C#, What Is Next?
C#, What Is Next?C#, What Is Next?
C#, What Is Next?
 
Lecture17
Lecture17Lecture17
Lecture17
 
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfCSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
 

More from angelfragranc

C ) common in nature, but commonly used psychological measures rare.pdf
 C ) common in nature, but commonly used psychological measures rare.pdf C ) common in nature, but commonly used psychological measures rare.pdf
C ) common in nature, but commonly used psychological measures rare.pdfangelfragranc
 
Question evidence 1. Comparison of DNA sequences among single-ce.pdf
    Question evidence   1. Comparison of DNA sequences among single-ce.pdf    Question evidence   1. Comparison of DNA sequences among single-ce.pdf
Question evidence 1. Comparison of DNA sequences among single-ce.pdfangelfragranc
 
The purpose of alchol (ethanol) is to dissolve io.pdf
                     The purpose of alchol (ethanol) is to dissolve io.pdf                     The purpose of alchol (ethanol) is to dissolve io.pdf
The purpose of alchol (ethanol) is to dissolve io.pdfangelfragranc
 
The components may not separate properly, because.pdf
                     The components may not separate properly, because.pdf                     The components may not separate properly, because.pdf
The components may not separate properly, because.pdfangelfragranc
 
The answer would be 5 because Dehydration in a .pdf
                     The answer would be 5 because  Dehydration in a .pdf                     The answer would be 5 because  Dehydration in a .pdf
The answer would be 5 because Dehydration in a .pdfangelfragranc
 
Nucleophile is any negative ion or any neutral mo.pdf
                     Nucleophile is any negative ion or any neutral mo.pdf                     Nucleophile is any negative ion or any neutral mo.pdf
Nucleophile is any negative ion or any neutral mo.pdfangelfragranc
 
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdf
                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdfangelfragranc
 
it is a salt formed by KOH and HCl it is a neutra.pdf
                     it is a salt formed by KOH and HCl it is a neutra.pdf                     it is a salt formed by KOH and HCl it is a neutra.pdf
it is a salt formed by KOH and HCl it is a neutra.pdfangelfragranc
 
Cultural competence refers to an ability to inter.pdf
                     Cultural competence refers to an ability to inter.pdf                     Cultural competence refers to an ability to inter.pdf
Cultural competence refers to an ability to inter.pdfangelfragranc
 
Conformers can also be named as conformational is.pdf
                     Conformers can also be named as conformational is.pdf                     Conformers can also be named as conformational is.pdf
Conformers can also be named as conformational is.pdfangelfragranc
 
Cl S Se Solution Cl .pdf
                     Cl  S  Se  Solution                     Cl .pdf                     Cl  S  Se  Solution                     Cl .pdf
Cl S Se Solution Cl .pdfangelfragranc
 
benzene sulphonic acid - SO3H on benzene ring .pdf
                     benzene sulphonic acid  - SO3H on benzene ring   .pdf                     benzene sulphonic acid  - SO3H on benzene ring   .pdf
benzene sulphonic acid - SO3H on benzene ring .pdfangelfragranc
 
The HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdfThe HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdfangelfragranc
 
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdfThe genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdfangelfragranc
 
ans D because NO2 has higher priority and should.pdf
                     ans D because NO2 has higher priority and should.pdf                     ans D because NO2 has higher priority and should.pdf
ans D because NO2 has higher priority and should.pdfangelfragranc
 
The objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdfThe objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdfangelfragranc
 
A. He has a smaller radius than H because He has .pdf
                     A. He has a smaller radius than H because He has .pdf                     A. He has a smaller radius than H because He has .pdf
A. He has a smaller radius than H because He has .pdfangelfragranc
 
standard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdfstandard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdfangelfragranc
 
A transducer is a device, usually electrical, ele.pdf
                     A transducer is a device, usually electrical, ele.pdf                     A transducer is a device, usually electrical, ele.pdf
A transducer is a device, usually electrical, ele.pdfangelfragranc
 

More from angelfragranc (20)

C ) common in nature, but commonly used psychological measures rare.pdf
 C ) common in nature, but commonly used psychological measures rare.pdf C ) common in nature, but commonly used psychological measures rare.pdf
C ) common in nature, but commonly used psychological measures rare.pdf
 
Question evidence 1. Comparison of DNA sequences among single-ce.pdf
    Question evidence   1. Comparison of DNA sequences among single-ce.pdf    Question evidence   1. Comparison of DNA sequences among single-ce.pdf
Question evidence 1. Comparison of DNA sequences among single-ce.pdf
 
The purpose of alchol (ethanol) is to dissolve io.pdf
                     The purpose of alchol (ethanol) is to dissolve io.pdf                     The purpose of alchol (ethanol) is to dissolve io.pdf
The purpose of alchol (ethanol) is to dissolve io.pdf
 
The components may not separate properly, because.pdf
                     The components may not separate properly, because.pdf                     The components may not separate properly, because.pdf
The components may not separate properly, because.pdf
 
The answer would be 5 because Dehydration in a .pdf
                     The answer would be 5 because  Dehydration in a .pdf                     The answer would be 5 because  Dehydration in a .pdf
The answer would be 5 because Dehydration in a .pdf
 
SO3 Sol.pdf
                     SO3                                       Sol.pdf                     SO3                                       Sol.pdf
SO3 Sol.pdf
 
Nucleophile is any negative ion or any neutral mo.pdf
                     Nucleophile is any negative ion or any neutral mo.pdf                     Nucleophile is any negative ion or any neutral mo.pdf
Nucleophile is any negative ion or any neutral mo.pdf
 
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdf
                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf                     Moles of acid (HNO2) = Vol  conc = 0.52 = 1 mol.pdf
Moles of acid (HNO2) = Vol conc = 0.52 = 1 mol.pdf
 
it is a salt formed by KOH and HCl it is a neutra.pdf
                     it is a salt formed by KOH and HCl it is a neutra.pdf                     it is a salt formed by KOH and HCl it is a neutra.pdf
it is a salt formed by KOH and HCl it is a neutra.pdf
 
Cultural competence refers to an ability to inter.pdf
                     Cultural competence refers to an ability to inter.pdf                     Cultural competence refers to an ability to inter.pdf
Cultural competence refers to an ability to inter.pdf
 
Conformers can also be named as conformational is.pdf
                     Conformers can also be named as conformational is.pdf                     Conformers can also be named as conformational is.pdf
Conformers can also be named as conformational is.pdf
 
Cl S Se Solution Cl .pdf
                     Cl  S  Se  Solution                     Cl .pdf                     Cl  S  Se  Solution                     Cl .pdf
Cl S Se Solution Cl .pdf
 
benzene sulphonic acid - SO3H on benzene ring .pdf
                     benzene sulphonic acid  - SO3H on benzene ring   .pdf                     benzene sulphonic acid  - SO3H on benzene ring   .pdf
benzene sulphonic acid - SO3H on benzene ring .pdf
 
The HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdfThe HTML was developed by Tim Berners Lee, to create electronic docu.pdf
The HTML was developed by Tim Berners Lee, to create electronic docu.pdf
 
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdfThe genotype of happy skipping smurf 2 – Hs  hSSolutionThe ge.pdf
The genotype of happy skipping smurf 2 – Hs hSSolutionThe ge.pdf
 
ans D because NO2 has higher priority and should.pdf
                     ans D because NO2 has higher priority and should.pdf                     ans D because NO2 has higher priority and should.pdf
ans D because NO2 has higher priority and should.pdf
 
The objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdfThe objective of the above code is to define a phonebook entry in ja.pdf
The objective of the above code is to define a phonebook entry in ja.pdf
 
A. He has a smaller radius than H because He has .pdf
                     A. He has a smaller radius than H because He has .pdf                     A. He has a smaller radius than H because He has .pdf
A. He has a smaller radius than H because He has .pdf
 
standard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdfstandard deviation = 0Solutionstandard deviation = 0.pdf
standard deviation = 0Solutionstandard deviation = 0.pdf
 
A transducer is a device, usually electrical, ele.pdf
                     A transducer is a device, usually electrical, ele.pdf                     A transducer is a device, usually electrical, ele.pdf
A transducer is a device, usually electrical, ele.pdf
 

Recently uploaded

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 

Recently uploaded (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 

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