SlideShare a Scribd company logo
1 of 7
Download to read offline
When we test your Fraction.java we will use the given FractionTester.java file.
Add the following methods to the given Fraction.java file
-public Fraction add( Fraction other) returns a Fraction that is the sum of the two Fractions.
-public Fraction subtract( Fraction other) returns a Fraction that is the difference between this
Fraction minus the other Fraction.
-public Fraction multiply( Fraction other) returns a Fraction that is the product of the two
Fractions.
-public Fraction divide( Fraction other) returns a Fraction that is the quotient of the two
Fractions.
-public Fraction reciprocal() returns a Fraction that is the reciprocal of this Fractions.
-private void reduce() Does not return a Fraction. It just modifies this Fraction by reducing it to
its lowest form.
Every Fraction must reduce at all times. Every new fraction constructed it must be reduced
before the constructor exits. No Fraction at any time may be stored in a form other than its
reduced form.
/* Fraction.java A class (data type) definition file
This file just defines what a Fraction is
This file is NOT a program
** data members are PRIVATE
** method members are PUBLIC
*/
public class Fraction
{
private int numer;
private int denom;
// ACCESSORS
public int getNumer()
{
return numer;
}
public int getDenom()
{
return denom;
}
public String toString()
{
return numer + "/" + denom;
}
// MUTATORS
public void setNumer( int n )
{
numer = n;
}
public void setDenom( int d )
{
if (d!=0)
denom=d;
else
{
// error msg OR exception OR exit etc.
}
}
// DEFAULT CONSTRUCTOR - no args passed in
public Fraction( )
{
this( 0, 1 ); // "this" means call a fellow constructor
}
// 1 arg CONSTRUCTOR - 1 arg passed in
// assume user wants whole number
public Fraction( int n )
{
this( n, 1 ); // "this" means call a fellow constructor
}
// FULL CONSTRUCTOR - an arg for each class data member
public Fraction( int n, int d )
{
setNumer(n);
setDenom(d);
// call your reduce here
}
// COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
public Fraction( Fraction other )
{
this( other.numer, other.denom ); // call my full C'Tor with other Fraction's data
}
private void reduce()
{
// reduces this fraction to lowest form
}
}// EOF
JavaTester file
Solution
/* Fraction.java A class (data type) definition file
This file just defines what a Fraction is
This file is NOT a program
** data members are PRIVATE
** method members are PUBLIC
*/
public class Fraction {
private int numer;
private int denom;
// ACCESSORS
public int getNumer() {
return numer;
}
public int getDenom() {
return denom;
}
public String toString() {
return numer + "/" + denom;
}
// MUTATORS
public void setNumer(int n) {
numer = n;
}
public void setDenom(int d) {
if (d != 0)
denom = d;
else {
// error msg OR exception OR exit etc.
}
}
// DEFAULT CONSTRUCTOR - no args passed in
public Fraction() {
this(0, 1); // "this" means call a fellow constructor
}
// 1 arg CONSTRUCTOR - 1 arg passed in
// assume user wants whole number
public Fraction(int n) {
this(n, 1); // "this" means call a fellow constructor
}
// FULL CONSTRUCTOR - an arg for each class data member
public Fraction(int n, int d) {
setNumer(n);
setDenom(d);
// call your reduce here
}
// COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
public Fraction(Fraction other) {
this(other.numer, other.denom); // call my full C'Tor with other
// Fraction's data
}
private void reduce() {
// reduces this fraction to lowest form
int GCD = gcd(getNumer(), getDenom());
setNumer(getNumer() / GCD);
setDenom(getDenom() / GCD);
}
// gcd : greatest common denom, used to reduce fractions
public int gcd(int n1, int n2) {
int M, N, R;
if (n1 < n2) {
N = n1;
M = n2;
} else {
N = n2;
M = n1;
}
R = M % N;
while (R != 0) {
M = N;
N = R;
R = M % N;
}
return N;
}
/**
* operation(+): addition between fractions
*
* @param b
* @return result of addition
*/
public Fraction add(Fraction b) {
int numer = (this.numer * b.denom) + (b.numer * this.denom);
int denom = this.denom * b.denom;
Fraction fraction = new Fraction(numer, denom);
fraction.reduce();
return fraction;
}
/**
* @param b
* @return
*/
public Fraction subtract(Fraction b) {
return add(new Fraction(-b.numer, b.denom));
}
/**
* method to multiply
*
* @param b
* @return
*/
public Fraction multiply(Fraction b) {
// check preconditions
if ((denom == 0) || (b.denom == 0))
throw new IllegalArgumentException("invalid denom");
// create new fraction to return as product
Fraction product = new Fraction();
// calculate product
product.numer = numer * b.numer;
product.denom = denom * b.denom;
// reduce the resulting fraction
product.reduce();
return product;
}
/**
* method to devide
*
* @param b
* @return
*/
public Fraction divide(Fraction b) {
// check preconditions
if ((denom == 0) || (b.numer == 0))
throw new IllegalArgumentException("invalid denom");
// create new fraction to return as result
Fraction result = new Fraction();
// calculate result
result.numer = numer * b.denom;
result.denom = denom * b.numer;
// reduce the resulting fraction
result.reduce();
return result;
}
public Fraction reciprocal() {
Fraction fraction = new Fraction(this.denom, this.numer);
fraction.reduce();
return fraction;
}
}// EOF
/*
FractionTester.java A program that declares Fraction variables
We will test your Fraction class on THIS tester.
*/
public class FractionTester {
public static void main(String args[]) {
// use the word Fraction as if were a Java data type
Fraction f1 = new Fraction(44, 14); // reduces to 22/7
System.out.println("f1=" + f1); // should output 22/7
Fraction f2 = new Fraction(21, 14); // reduces to 3/2
System.out.println("f2=" + f2); // should output 3/2
System.out.println(f1.add(f2)); // should output 65/14
System.out.println(f1.subtract(f2)); // should output 23/14
System.out.println(f1.multiply(f2)); // should output 33/7
System.out.println(f1.divide(f2)); // should output 44/21
System.out.println(f1.reciprocal()); // should output 7/22
} // END main
} // EOF
OUTPUT:
f1=44/14
f2=21/14
65/14
23/14
33/7
44/21
7/22

More Related Content

Similar to When we test your Fraction.java we will use the given FractionTester.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.pdfravikapoorindia
 
I need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfI need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfeyeonsecuritysystems
 
can someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfcan someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfvinaythemodel
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxbudbarber38650
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfajayadinathcomputers
 
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-------------.pdfshanki7
 
Write a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docxWrite a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docxrochellwa9f
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfaromanets
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfadityastores21
 
3. constructors and destructor
3. constructors and destructor3. constructors and destructor
3. constructors and destructorjashobhan pradhan
 
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.pdfaioils
 
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
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfrohit219406
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfcontact41
 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptxMrMudassir
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxfestockton
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Eric Hogue
 
Passing arguments by-Value and using return types.#includeiost.pdf
Passing arguments by-Value and using return types.#includeiost.pdfPassing arguments by-Value and using return types.#includeiost.pdf
Passing arguments by-Value and using return types.#includeiost.pdfjillisacebi75827
 
Design patterns
Design patternsDesign patterns
Design patternsBa Tran
 
Write a class called Fraction with the two instance variables denomin.docx
 Write a class called Fraction with the two instance variables denomin.docx Write a class called Fraction with the two instance variables denomin.docx
Write a class called Fraction with the two instance variables denomin.docxajoy21
 

Similar to When we test your Fraction.java we will use the given FractionTester.pdf (20)

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
 
I need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdfI need help completing this C++ code with these requirements.instr.pdf
I need help completing this C++ code with these requirements.instr.pdf
 
can someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfcan someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdf
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docx
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.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
 
Write a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docxWrite a program in Java to implement the ADT Binary Tree part of who.docx
Write a program in Java to implement the ADT Binary Tree part of who.docx
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
 
3. constructors and destructor
3. constructors and destructor3. constructors and destructor
3. constructors and destructor
 
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
 
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdfC++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
C++, Implement the class BinarySearchTree, as given in listing 16-4 .pdf
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdf
 
Oop lect3.pptx
Oop lect3.pptxOop lect3.pptx
Oop lect3.pptx
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014
 
Passing arguments by-Value and using return types.#includeiost.pdf
Passing arguments by-Value and using return types.#includeiost.pdfPassing arguments by-Value and using return types.#includeiost.pdf
Passing arguments by-Value and using return types.#includeiost.pdf
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Write a class called Fraction with the two instance variables denomin.docx
 Write a class called Fraction with the two instance variables denomin.docx Write a class called Fraction with the two instance variables denomin.docx
Write a class called Fraction with the two instance variables denomin.docx
 

More from arihantkitchenmart

Identify the four factors of production, and tell what type of incom.pdf
Identify the four factors of production, and tell what type of incom.pdfIdentify the four factors of production, and tell what type of incom.pdf
Identify the four factors of production, and tell what type of incom.pdfarihantkitchenmart
 
Identify the membranes that line the cavity surrounding the lungs. p.pdf
Identify the membranes that line the cavity surrounding the lungs.  p.pdfIdentify the membranes that line the cavity surrounding the lungs.  p.pdf
Identify the membranes that line the cavity surrounding the lungs. p.pdfarihantkitchenmart
 
How is explicit knowledge different from tacit knowledgeSolutio.pdf
How is explicit knowledge different from tacit knowledgeSolutio.pdfHow is explicit knowledge different from tacit knowledgeSolutio.pdf
How is explicit knowledge different from tacit knowledgeSolutio.pdfarihantkitchenmart
 
How did printing technology impact literacy and education How d.pdf
How did printing technology impact literacy and education How d.pdfHow did printing technology impact literacy and education How d.pdf
How did printing technology impact literacy and education How d.pdfarihantkitchenmart
 
Could a violent video games make a person become a terrorist .pdf
Could a violent video games make a person become a terrorist  .pdfCould a violent video games make a person become a terrorist  .pdf
Could a violent video games make a person become a terrorist .pdfarihantkitchenmart
 
For each of the four rows in Table 5.10-1, organize and combine the .pdf
For each of the four rows in Table 5.10-1, organize and combine the .pdfFor each of the four rows in Table 5.10-1, organize and combine the .pdf
For each of the four rows in Table 5.10-1, organize and combine the .pdfarihantkitchenmart
 
Applying change management theoryto discuss the integration ofone of.pdf
Applying change management theoryto discuss the integration ofone of.pdfApplying change management theoryto discuss the integration ofone of.pdf
Applying change management theoryto discuss the integration ofone of.pdfarihantkitchenmart
 
Albinism in humans is inherited as a simple recessive trait. For the.pdf
Albinism in humans is inherited as a simple recessive trait. For the.pdfAlbinism in humans is inherited as a simple recessive trait. For the.pdf
Albinism in humans is inherited as a simple recessive trait. For the.pdfarihantkitchenmart
 
Exercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdf
Exercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdfExercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdf
Exercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdfarihantkitchenmart
 
Does isoforms have do do anything with transcription and translat.pdf
Does isoforms have do do anything with transcription and translat.pdfDoes isoforms have do do anything with transcription and translat.pdf
Does isoforms have do do anything with transcription and translat.pdfarihantkitchenmart
 
discuss the evolutionary significance of bilateral symmetry, cephali.pdf
discuss the evolutionary significance of bilateral symmetry, cephali.pdfdiscuss the evolutionary significance of bilateral symmetry, cephali.pdf
discuss the evolutionary significance of bilateral symmetry, cephali.pdfarihantkitchenmart
 
Compare the best-case time complexities of Exchange Sort and Inserti.pdf
Compare the best-case time complexities of Exchange Sort and Inserti.pdfCompare the best-case time complexities of Exchange Sort and Inserti.pdf
Compare the best-case time complexities of Exchange Sort and Inserti.pdfarihantkitchenmart
 
What are transposons How have they been exploited for bacterial gen.pdf
What are transposons How have they been exploited for bacterial gen.pdfWhat are transposons How have they been exploited for bacterial gen.pdf
What are transposons How have they been exploited for bacterial gen.pdfarihantkitchenmart
 
A population of 1000 dung beetles was split into live populations whe.pdf
A population of 1000 dung beetles was split into live populations whe.pdfA population of 1000 dung beetles was split into live populations whe.pdf
A population of 1000 dung beetles was split into live populations whe.pdfarihantkitchenmart
 
Which of these sentences are TRUE in Java Select all that apply, You.pdf
Which of these sentences are TRUE in Java Select all that apply, You.pdfWhich of these sentences are TRUE in Java Select all that apply, You.pdf
Which of these sentences are TRUE in Java Select all that apply, You.pdfarihantkitchenmart
 
Which of the following is a stated principle of a NYSE report identi.pdf
Which of the following is a stated principle of a NYSE report identi.pdfWhich of the following is a stated principle of a NYSE report identi.pdf
Which of the following is a stated principle of a NYSE report identi.pdfarihantkitchenmart
 
Why do some elderly take B12 sublingually or transdermally Provide .pdf
Why do some elderly take B12 sublingually or transdermally Provide .pdfWhy do some elderly take B12 sublingually or transdermally Provide .pdf
Why do some elderly take B12 sublingually or transdermally Provide .pdfarihantkitchenmart
 
Why is water a more hospitable environment for life than dry land O.pdf
Why is water a more hospitable environment for life than dry land  O.pdfWhy is water a more hospitable environment for life than dry land  O.pdf
Why is water a more hospitable environment for life than dry land O.pdfarihantkitchenmart
 
Which of the following triploblastic invertebrates is incorrectly ma.pdf
Which of the following triploblastic invertebrates is incorrectly ma.pdfWhich of the following triploblastic invertebrates is incorrectly ma.pdf
Which of the following triploblastic invertebrates is incorrectly ma.pdfarihantkitchenmart
 
What is the fundamental difference between shell subscript and C prog.pdf
What is the fundamental difference between shell subscript and C prog.pdfWhat is the fundamental difference between shell subscript and C prog.pdf
What is the fundamental difference between shell subscript and C prog.pdfarihantkitchenmart
 

More from arihantkitchenmart (20)

Identify the four factors of production, and tell what type of incom.pdf
Identify the four factors of production, and tell what type of incom.pdfIdentify the four factors of production, and tell what type of incom.pdf
Identify the four factors of production, and tell what type of incom.pdf
 
Identify the membranes that line the cavity surrounding the lungs. p.pdf
Identify the membranes that line the cavity surrounding the lungs.  p.pdfIdentify the membranes that line the cavity surrounding the lungs.  p.pdf
Identify the membranes that line the cavity surrounding the lungs. p.pdf
 
How is explicit knowledge different from tacit knowledgeSolutio.pdf
How is explicit knowledge different from tacit knowledgeSolutio.pdfHow is explicit knowledge different from tacit knowledgeSolutio.pdf
How is explicit knowledge different from tacit knowledgeSolutio.pdf
 
How did printing technology impact literacy and education How d.pdf
How did printing technology impact literacy and education How d.pdfHow did printing technology impact literacy and education How d.pdf
How did printing technology impact literacy and education How d.pdf
 
Could a violent video games make a person become a terrorist .pdf
Could a violent video games make a person become a terrorist  .pdfCould a violent video games make a person become a terrorist  .pdf
Could a violent video games make a person become a terrorist .pdf
 
For each of the four rows in Table 5.10-1, organize and combine the .pdf
For each of the four rows in Table 5.10-1, organize and combine the .pdfFor each of the four rows in Table 5.10-1, organize and combine the .pdf
For each of the four rows in Table 5.10-1, organize and combine the .pdf
 
Applying change management theoryto discuss the integration ofone of.pdf
Applying change management theoryto discuss the integration ofone of.pdfApplying change management theoryto discuss the integration ofone of.pdf
Applying change management theoryto discuss the integration ofone of.pdf
 
Albinism in humans is inherited as a simple recessive trait. For the.pdf
Albinism in humans is inherited as a simple recessive trait. For the.pdfAlbinism in humans is inherited as a simple recessive trait. For the.pdf
Albinism in humans is inherited as a simple recessive trait. For the.pdf
 
Exercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdf
Exercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdfExercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdf
Exercise 3 Cell Structure Name Tacabac 1. Cells in the body have a fl.pdf
 
Does isoforms have do do anything with transcription and translat.pdf
Does isoforms have do do anything with transcription and translat.pdfDoes isoforms have do do anything with transcription and translat.pdf
Does isoforms have do do anything with transcription and translat.pdf
 
discuss the evolutionary significance of bilateral symmetry, cephali.pdf
discuss the evolutionary significance of bilateral symmetry, cephali.pdfdiscuss the evolutionary significance of bilateral symmetry, cephali.pdf
discuss the evolutionary significance of bilateral symmetry, cephali.pdf
 
Compare the best-case time complexities of Exchange Sort and Inserti.pdf
Compare the best-case time complexities of Exchange Sort and Inserti.pdfCompare the best-case time complexities of Exchange Sort and Inserti.pdf
Compare the best-case time complexities of Exchange Sort and Inserti.pdf
 
What are transposons How have they been exploited for bacterial gen.pdf
What are transposons How have they been exploited for bacterial gen.pdfWhat are transposons How have they been exploited for bacterial gen.pdf
What are transposons How have they been exploited for bacterial gen.pdf
 
A population of 1000 dung beetles was split into live populations whe.pdf
A population of 1000 dung beetles was split into live populations whe.pdfA population of 1000 dung beetles was split into live populations whe.pdf
A population of 1000 dung beetles was split into live populations whe.pdf
 
Which of these sentences are TRUE in Java Select all that apply, You.pdf
Which of these sentences are TRUE in Java Select all that apply, You.pdfWhich of these sentences are TRUE in Java Select all that apply, You.pdf
Which of these sentences are TRUE in Java Select all that apply, You.pdf
 
Which of the following is a stated principle of a NYSE report identi.pdf
Which of the following is a stated principle of a NYSE report identi.pdfWhich of the following is a stated principle of a NYSE report identi.pdf
Which of the following is a stated principle of a NYSE report identi.pdf
 
Why do some elderly take B12 sublingually or transdermally Provide .pdf
Why do some elderly take B12 sublingually or transdermally Provide .pdfWhy do some elderly take B12 sublingually or transdermally Provide .pdf
Why do some elderly take B12 sublingually or transdermally Provide .pdf
 
Why is water a more hospitable environment for life than dry land O.pdf
Why is water a more hospitable environment for life than dry land  O.pdfWhy is water a more hospitable environment for life than dry land  O.pdf
Why is water a more hospitable environment for life than dry land O.pdf
 
Which of the following triploblastic invertebrates is incorrectly ma.pdf
Which of the following triploblastic invertebrates is incorrectly ma.pdfWhich of the following triploblastic invertebrates is incorrectly ma.pdf
Which of the following triploblastic invertebrates is incorrectly ma.pdf
 
What is the fundamental difference between shell subscript and C prog.pdf
What is the fundamental difference between shell subscript and C prog.pdfWhat is the fundamental difference between shell subscript and C prog.pdf
What is the fundamental difference between shell subscript and C prog.pdf
 

Recently uploaded

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
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_.pdfSherif Taha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
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
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 

Recently uploaded (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
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
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
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
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

When we test your Fraction.java we will use the given FractionTester.pdf

  • 1. When we test your Fraction.java we will use the given FractionTester.java file. Add the following methods to the given Fraction.java file -public Fraction add( Fraction other) returns a Fraction that is the sum of the two Fractions. -public Fraction subtract( Fraction other) returns a Fraction that is the difference between this Fraction minus the other Fraction. -public Fraction multiply( Fraction other) returns a Fraction that is the product of the two Fractions. -public Fraction divide( Fraction other) returns a Fraction that is the quotient of the two Fractions. -public Fraction reciprocal() returns a Fraction that is the reciprocal of this Fractions. -private void reduce() Does not return a Fraction. It just modifies this Fraction by reducing it to its lowest form. Every Fraction must reduce at all times. Every new fraction constructed it must be reduced before the constructor exits. No Fraction at any time may be stored in a form other than its reduced form. /* Fraction.java A class (data type) definition file This file just defines what a Fraction is This file is NOT a program ** data members are PRIVATE ** method members are PUBLIC */ public class Fraction { private int numer; private int denom; // ACCESSORS public int getNumer() { return numer; } public int getDenom() { return denom; } public String toString()
  • 2. { return numer + "/" + denom; } // MUTATORS public void setNumer( int n ) { numer = n; } public void setDenom( int d ) { if (d!=0) denom=d; else { // error msg OR exception OR exit etc. } } // DEFAULT CONSTRUCTOR - no args passed in public Fraction( ) { this( 0, 1 ); // "this" means call a fellow constructor } // 1 arg CONSTRUCTOR - 1 arg passed in // assume user wants whole number public Fraction( int n ) { this( n, 1 ); // "this" means call a fellow constructor } // FULL CONSTRUCTOR - an arg for each class data member public Fraction( int n, int d ) { setNumer(n); setDenom(d); // call your reduce here } // COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
  • 3. public Fraction( Fraction other ) { this( other.numer, other.denom ); // call my full C'Tor with other Fraction's data } private void reduce() { // reduces this fraction to lowest form } }// EOF JavaTester file Solution /* Fraction.java A class (data type) definition file This file just defines what a Fraction is This file is NOT a program ** data members are PRIVATE ** method members are PUBLIC */ public class Fraction { private int numer; private int denom; // ACCESSORS public int getNumer() { return numer; } public int getDenom() { return denom; } public String toString() { return numer + "/" + denom; } // MUTATORS public void setNumer(int n) { numer = n; }
  • 4. public void setDenom(int d) { if (d != 0) denom = d; else { // error msg OR exception OR exit etc. } } // DEFAULT CONSTRUCTOR - no args passed in public Fraction() { this(0, 1); // "this" means call a fellow constructor } // 1 arg CONSTRUCTOR - 1 arg passed in // assume user wants whole number public Fraction(int n) { this(n, 1); // "this" means call a fellow constructor } // FULL CONSTRUCTOR - an arg for each class data member public Fraction(int n, int d) { setNumer(n); setDenom(d); // call your reduce here } // COPY CONSTRUCTOR - takes ref to some already initialized Fraction object public Fraction(Fraction other) { this(other.numer, other.denom); // call my full C'Tor with other // Fraction's data } private void reduce() { // reduces this fraction to lowest form int GCD = gcd(getNumer(), getDenom()); setNumer(getNumer() / GCD); setDenom(getDenom() / GCD); } // gcd : greatest common denom, used to reduce fractions public int gcd(int n1, int n2) { int M, N, R;
  • 5. if (n1 < n2) { N = n1; M = n2; } else { N = n2; M = n1; } R = M % N; while (R != 0) { M = N; N = R; R = M % N; } return N; } /** * operation(+): addition between fractions * * @param b * @return result of addition */ public Fraction add(Fraction b) { int numer = (this.numer * b.denom) + (b.numer * this.denom); int denom = this.denom * b.denom; Fraction fraction = new Fraction(numer, denom); fraction.reduce(); return fraction; } /** * @param b * @return */ public Fraction subtract(Fraction b) { return add(new Fraction(-b.numer, b.denom)); } /**
  • 6. * method to multiply * * @param b * @return */ public Fraction multiply(Fraction b) { // check preconditions if ((denom == 0) || (b.denom == 0)) throw new IllegalArgumentException("invalid denom"); // create new fraction to return as product Fraction product = new Fraction(); // calculate product product.numer = numer * b.numer; product.denom = denom * b.denom; // reduce the resulting fraction product.reduce(); return product; } /** * method to devide * * @param b * @return */ public Fraction divide(Fraction b) { // check preconditions if ((denom == 0) || (b.numer == 0)) throw new IllegalArgumentException("invalid denom"); // create new fraction to return as result Fraction result = new Fraction(); // calculate result result.numer = numer * b.denom; result.denom = denom * b.numer; // reduce the resulting fraction result.reduce(); return result;
  • 7. } public Fraction reciprocal() { Fraction fraction = new Fraction(this.denom, this.numer); fraction.reduce(); return fraction; } }// EOF /* FractionTester.java A program that declares Fraction variables We will test your Fraction class on THIS tester. */ public class FractionTester { public static void main(String args[]) { // use the word Fraction as if were a Java data type Fraction f1 = new Fraction(44, 14); // reduces to 22/7 System.out.println("f1=" + f1); // should output 22/7 Fraction f2 = new Fraction(21, 14); // reduces to 3/2 System.out.println("f2=" + f2); // should output 3/2 System.out.println(f1.add(f2)); // should output 65/14 System.out.println(f1.subtract(f2)); // should output 23/14 System.out.println(f1.multiply(f2)); // should output 33/7 System.out.println(f1.divide(f2)); // should output 44/21 System.out.println(f1.reciprocal()); // should output 7/22 } // END main } // EOF OUTPUT: f1=44/14 f2=21/14 65/14 23/14 33/7 44/21 7/22