SlideShare a Scribd company logo
1 of 9
Download to read offline
import java.util.Scanner;
public class Fraction {
/**
* instance variable
*/
private int numerator;
/**
* instance variable
*/
private int denominator;
/**
* param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Fraction f1 = new Fraction();
final Fraction f2 = new Fraction(10, 15);
final Fraction f3 = new Fraction();
Fraction f4 = new Fraction();
Fraction f5;
f3.setNumerator(14);
f3.setDenominator(16);
f1.print();
f2.print();
f3.print();
// f1.get();
// f1.print();
f4.assign(12, 50);
f4.print();
f5 = Fraction.multiply(f2, f4);
f5.print();
System.out.println(" f2: ");
f2.print();
f2.multiply(f4);
System.out.println(" f2: ");
f2.print();
}
/**
* default constructor
* initlizing numerator with 0 and denominator with 1
*/
public Fraction() {
// System.out.println("In default construction");
numerator = 0;
denominator = 1;
}
/**
* param n
* param d
*/
public Fraction(final int n, final int d) {
// System.out.println("In Parameter construction");
numerator = n;
denominator = d;
}
/**
* return numerator
*/
public int getNumerator() {
return numerator;
}
/**
* param numerator
*/
public void setNumerator(final int numerator) {
this.numerator = numerator;
}
/**
* return denominator
*/
public int getDenominator() {
return denominator;
}
/**
* param denominator
*/
public void setDenominator(final int denominator) {
this.denominator = denominator;
}
/**
* print fraction
*/
public void print() {
System.out.print(numerator + "/" + denominator + " ");
}
/**
*
*/
public void get() {
final Scanner kb = new Scanner(System.in);
System.out.print(" Enter numerator: ");
numerator = kb.nextInt();
System.out.print("Enter denominator: ");
denominator = kb.nextInt();
kb.close();
}
/**
* param n
* param d
*/
public void assign(final int n, final int d) {
if (d == 0) {
throw new IllegalArgumentException("denominator can not be 0");
}
if (n < 0 || d < 0) {
throw new IllegalArgumentException("values must be positive");
}
numerator = n;
denominator = d;
reduce();
}
/**
* it reduce numerator and denominator by GCD of numerator and denominator
*/
private void reduce() {
int min = numerator;
int gcd;
if (numerator > denominator) {
min = denominator;
}
for (gcd = min; gcd > 1; gcd--) {
if (numerator % gcd == 0 && denominator % gcd == 0) {
break;
}
}
numerator = numerator / gcd;
denominator = denominator / gcd;
}
/**
* param f1
* param f2
* return f
*/
public static Fraction multiply(Fraction f1, Fraction f2) {
Fraction f = new Fraction();
int num;
int den;
num = f1.getNumerator() * f2.getNumerator();
den = f1.getNumerator() * f2.getDenominator();
f.assign(num, den);
f.reduce();
return f;
}
/**
* param f
*/
public void multiply(Fraction f) {
this.numerator = this.numerator * f.getNumerator();
this.denominator = this.denominator * f.getDenominator();
this.reduce();
return;
}
}
Solution
import java.util.Scanner;
public class Fraction {
/**
* instance variable
*/
private int numerator;
/**
* instance variable
*/
private int denominator;
/**
* param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Fraction f1 = new Fraction();
final Fraction f2 = new Fraction(10, 15);
final Fraction f3 = new Fraction();
Fraction f4 = new Fraction();
Fraction f5;
f3.setNumerator(14);
f3.setDenominator(16);
f1.print();
f2.print();
f3.print();
// f1.get();
// f1.print();
f4.assign(12, 50);
f4.print();
f5 = Fraction.multiply(f2, f4);
f5.print();
System.out.println(" f2: ");
f2.print();
f2.multiply(f4);
System.out.println(" f2: ");
f2.print();
}
/**
* default constructor
* initlizing numerator with 0 and denominator with 1
*/
public Fraction() {
// System.out.println("In default construction");
numerator = 0;
denominator = 1;
}
/**
* param n
* param d
*/
public Fraction(final int n, final int d) {
// System.out.println("In Parameter construction");
numerator = n;
denominator = d;
}
/**
* return numerator
*/
public int getNumerator() {
return numerator;
}
/**
* param numerator
*/
public void setNumerator(final int numerator) {
this.numerator = numerator;
}
/**
* return denominator
*/
public int getDenominator() {
return denominator;
}
/**
* param denominator
*/
public void setDenominator(final int denominator) {
this.denominator = denominator;
}
/**
* print fraction
*/
public void print() {
System.out.print(numerator + "/" + denominator + " ");
}
/**
*
*/
public void get() {
final Scanner kb = new Scanner(System.in);
System.out.print(" Enter numerator: ");
numerator = kb.nextInt();
System.out.print("Enter denominator: ");
denominator = kb.nextInt();
kb.close();
}
/**
* param n
* param d
*/
public void assign(final int n, final int d) {
if (d == 0) {
throw new IllegalArgumentException("denominator can not be 0");
}
if (n < 0 || d < 0) {
throw new IllegalArgumentException("values must be positive");
}
numerator = n;
denominator = d;
reduce();
}
/**
* it reduce numerator and denominator by GCD of numerator and denominator
*/
private void reduce() {
int min = numerator;
int gcd;
if (numerator > denominator) {
min = denominator;
}
for (gcd = min; gcd > 1; gcd--) {
if (numerator % gcd == 0 && denominator % gcd == 0) {
break;
}
}
numerator = numerator / gcd;
denominator = denominator / gcd;
}
/**
* param f1
* param f2
* return f
*/
public static Fraction multiply(Fraction f1, Fraction f2) {
Fraction f = new Fraction();
int num;
int den;
num = f1.getNumerator() * f2.getNumerator();
den = f1.getNumerator() * f2.getDenominator();
f.assign(num, den);
f.reduce();
return f;
}
/**
* param f
*/
public void multiply(Fraction f) {
this.numerator = this.numerator * f.getNumerator();
this.denominator = this.denominator * f.getDenominator();
this.reduce();
return;
}
}

More Related Content

Similar to import java.util.Scanner;public class Fraction {   instan.pdf

fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdf
anjanadistribution
 
Help in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdfHelp in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdf
manjan6
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
aptcomputerzone
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdf
angelfragranc
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
aioils
 
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
aroramobiles1
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
aquazac
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
shanki7
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
anukoolelectronics
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
jyothimuppasani1
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
ezzi552
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdfQ1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
abdulrahamanbags
 
operating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdfoperating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdf
aquadreammail
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
herminaherman
 

Similar to import java.util.Scanner;public class Fraction {   instan.pdf (20)

fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdf
 
Help in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdfHelp in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdf
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdf
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.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
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
 
Functions
FunctionsFunctions
Functions
 
C++ Programm.pptx
C++ Programm.pptxC++ Programm.pptx
C++ Programm.pptx
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdfQ1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
Q1 Consider the below omp_trap1.c implantation, modify the code so t.pdf
 
operating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdfoperating system linux,ubuntu,Mac Geometri.pdf
operating system linux,ubuntu,Mac Geometri.pdf
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
 

More from apleathers

While a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdfWhile a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdf
apleathers
 
What is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdfWhat is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdf
apleathers
 
Thousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdfThousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdf
apleathers
 
To understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdfTo understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdf
apleathers
 
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdfQues-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdf
apleathers
 
QuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdfQuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdf
apleathers
 
Programimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdfProgramimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdf
apleathers
 
Please follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdfPlease follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdf
apleathers
 
package DataStructures; public class HelloWorld AnyType extends.pdf
package DataStructures; public class HelloWorld AnyType extends.pdfpackage DataStructures; public class HelloWorld AnyType extends.pdf
package DataStructures; public class HelloWorld AnyType extends.pdf
apleathers
 
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdfHi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
apleathers
 
Family as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdfFamily as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdf
apleathers
 

More from apleathers (20)

While a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdfWhile a majority of the worlds current electricity supply is gener.pdf
While a majority of the worlds current electricity supply is gener.pdf
 
What is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdfWhat is broadbandWhat is the potential for broadband wireless syste.pdf
What is broadbandWhat is the potential for broadband wireless syste.pdf
 
WaterSolutionWater.pdf
WaterSolutionWater.pdfWaterSolutionWater.pdf
WaterSolutionWater.pdf
 
Thousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdfThousands of repeating units called nephrons will form a kidney and .pdf
Thousands of repeating units called nephrons will form a kidney and .pdf
 
To understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdfTo understand the complexicities of the issue at hand we must first .pdf
To understand the complexicities of the issue at hand we must first .pdf
 
The P is the present value of the cash flow streamP = 100 (1+10.pdf
The P is the present value of the cash flow streamP = 100  (1+10.pdfThe P is the present value of the cash flow streamP = 100  (1+10.pdf
The P is the present value of the cash flow streamP = 100 (1+10.pdf
 
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdfRibose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
Ribose sugar puckers adopt for both Deoxy nucleotides and ribonucleo.pdf
 
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdfQues-1 Prenatal diagnosis has both positive and potentially negativ.pdf
Ques-1 Prenatal diagnosis has both positive and potentially negativ.pdf
 
QuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdfQuestionWhat are the elements of safe drinking water act What ar.pdf
QuestionWhat are the elements of safe drinking water act What ar.pdf
 
Programimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdfProgramimport java.util.; import java.io.; class FoodTruck.pdf
Programimport java.util.; import java.io.; class FoodTruck.pdf
 
Please follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdfPlease follow the data and description Active Directory In gen.pdf
Please follow the data and description Active Directory In gen.pdf
 
package DataStructures; public class HelloWorld AnyType extends.pdf
package DataStructures; public class HelloWorld AnyType extends.pdfpackage DataStructures; public class HelloWorld AnyType extends.pdf
package DataStructures; public class HelloWorld AnyType extends.pdf
 
it always attacks oxygen.the image doesnot workSolutionit alwa.pdf
it always attacks oxygen.the image doesnot workSolutionit alwa.pdfit always attacks oxygen.the image doesnot workSolutionit alwa.pdf
it always attacks oxygen.the image doesnot workSolutionit alwa.pdf
 
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdfHi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
Hi,Please find the Ansswer below.PLAYLIST.h#include iostrea.pdf
 
Family as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdfFamily as a Social Institution A family can be defined as group of .pdf
Family as a Social Institution A family can be defined as group of .pdf
 
D. The actual development of new standards and protocols for the Int.pdf
D. The actual development of new standards and protocols for the Int.pdfD. The actual development of new standards and protocols for the Int.pdf
D. The actual development of new standards and protocols for the Int.pdf
 
Dependent Variable. A variable that depends on one or more other var.pdf
Dependent Variable. A variable that depends on one or more other var.pdfDependent Variable. A variable that depends on one or more other var.pdf
Dependent Variable. A variable that depends on one or more other var.pdf
 
C is correct. While all Layer 2 devices split collision domains, swi.pdf
C is correct. While all Layer 2 devices split collision domains, swi.pdfC is correct. While all Layer 2 devices split collision domains, swi.pdf
C is correct. While all Layer 2 devices split collision domains, swi.pdf
 
Answer.The following organisms are present in pod.1. Prokaryotes.pdf
Answer.The following organisms are present in pod.1. Prokaryotes.pdfAnswer.The following organisms are present in pod.1. Prokaryotes.pdf
Answer.The following organisms are present in pod.1. Prokaryotes.pdf
 
AnswerThe mobile elements are the elerments move form one place t.pdf
AnswerThe mobile elements are the elerments move form one place t.pdfAnswerThe mobile elements are the elerments move form one place t.pdf
AnswerThe mobile elements are the elerments move form one place t.pdf
 

Recently uploaded

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
AnaAcapella
 

Recently uploaded (20)

Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
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Ă...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
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Ữ Â...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
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
 

import java.util.Scanner;public class Fraction {   instan.pdf

  • 1. import java.util.Scanner; public class Fraction { /** * instance variable */ private int numerator; /** * instance variable */ private int denominator; /** * param args */ public static void main(String[] args) { // TODO Auto-generated method stub final Fraction f1 = new Fraction(); final Fraction f2 = new Fraction(10, 15); final Fraction f3 = new Fraction(); Fraction f4 = new Fraction(); Fraction f5; f3.setNumerator(14); f3.setDenominator(16); f1.print(); f2.print(); f3.print(); // f1.get(); // f1.print(); f4.assign(12, 50); f4.print(); f5 = Fraction.multiply(f2, f4); f5.print(); System.out.println(" f2: "); f2.print(); f2.multiply(f4);
  • 2. System.out.println(" f2: "); f2.print(); } /** * default constructor * initlizing numerator with 0 and denominator with 1 */ public Fraction() { // System.out.println("In default construction"); numerator = 0; denominator = 1; } /** * param n * param d */ public Fraction(final int n, final int d) { // System.out.println("In Parameter construction"); numerator = n; denominator = d; } /** * return numerator */ public int getNumerator() { return numerator; } /** * param numerator */ public void setNumerator(final int numerator) { this.numerator = numerator; }
  • 3. /** * return denominator */ public int getDenominator() { return denominator; } /** * param denominator */ public void setDenominator(final int denominator) { this.denominator = denominator; } /** * print fraction */ public void print() { System.out.print(numerator + "/" + denominator + " "); } /** * */ public void get() { final Scanner kb = new Scanner(System.in); System.out.print(" Enter numerator: "); numerator = kb.nextInt(); System.out.print("Enter denominator: "); denominator = kb.nextInt(); kb.close(); } /** * param n * param d */ public void assign(final int n, final int d) { if (d == 0) {
  • 4. throw new IllegalArgumentException("denominator can not be 0"); } if (n < 0 || d < 0) { throw new IllegalArgumentException("values must be positive"); } numerator = n; denominator = d; reduce(); } /** * it reduce numerator and denominator by GCD of numerator and denominator */ private void reduce() { int min = numerator; int gcd; if (numerator > denominator) { min = denominator; } for (gcd = min; gcd > 1; gcd--) { if (numerator % gcd == 0 && denominator % gcd == 0) { break; } } numerator = numerator / gcd; denominator = denominator / gcd; } /** * param f1 * param f2 * return f */ public static Fraction multiply(Fraction f1, Fraction f2) { Fraction f = new Fraction(); int num; int den; num = f1.getNumerator() * f2.getNumerator();
  • 5. den = f1.getNumerator() * f2.getDenominator(); f.assign(num, den); f.reduce(); return f; } /** * param f */ public void multiply(Fraction f) { this.numerator = this.numerator * f.getNumerator(); this.denominator = this.denominator * f.getDenominator(); this.reduce(); return; } } Solution import java.util.Scanner; public class Fraction { /** * instance variable */ private int numerator; /** * instance variable */ private int denominator; /** * param args */ public static void main(String[] args) { // TODO Auto-generated method stub final Fraction f1 = new Fraction(); final Fraction f2 = new Fraction(10, 15);
  • 6. final Fraction f3 = new Fraction(); Fraction f4 = new Fraction(); Fraction f5; f3.setNumerator(14); f3.setDenominator(16); f1.print(); f2.print(); f3.print(); // f1.get(); // f1.print(); f4.assign(12, 50); f4.print(); f5 = Fraction.multiply(f2, f4); f5.print(); System.out.println(" f2: "); f2.print(); f2.multiply(f4); System.out.println(" f2: "); f2.print(); } /** * default constructor * initlizing numerator with 0 and denominator with 1 */ public Fraction() { // System.out.println("In default construction"); numerator = 0; denominator = 1; } /** * param n * param d */ public Fraction(final int n, final int d) { // System.out.println("In Parameter construction"); numerator = n;
  • 7. denominator = d; } /** * return numerator */ public int getNumerator() { return numerator; } /** * param numerator */ public void setNumerator(final int numerator) { this.numerator = numerator; } /** * return denominator */ public int getDenominator() { return denominator; } /** * param denominator */ public void setDenominator(final int denominator) { this.denominator = denominator; } /** * print fraction */ public void print() { System.out.print(numerator + "/" + denominator + " "); }
  • 8. /** * */ public void get() { final Scanner kb = new Scanner(System.in); System.out.print(" Enter numerator: "); numerator = kb.nextInt(); System.out.print("Enter denominator: "); denominator = kb.nextInt(); kb.close(); } /** * param n * param d */ public void assign(final int n, final int d) { if (d == 0) { throw new IllegalArgumentException("denominator can not be 0"); } if (n < 0 || d < 0) { throw new IllegalArgumentException("values must be positive"); } numerator = n; denominator = d; reduce(); } /** * it reduce numerator and denominator by GCD of numerator and denominator */ private void reduce() { int min = numerator; int gcd; if (numerator > denominator) { min = denominator; } for (gcd = min; gcd > 1; gcd--) {
  • 9. if (numerator % gcd == 0 && denominator % gcd == 0) { break; } } numerator = numerator / gcd; denominator = denominator / gcd; } /** * param f1 * param f2 * return f */ public static Fraction multiply(Fraction f1, Fraction f2) { Fraction f = new Fraction(); int num; int den; num = f1.getNumerator() * f2.getNumerator(); den = f1.getNumerator() * f2.getDenominator(); f.assign(num, den); f.reduce(); return f; } /** * param f */ public void multiply(Fraction f) { this.numerator = this.numerator * f.getNumerator(); this.denominator = this.denominator * f.getDenominator(); this.reduce(); return; } }