SlideShare a Scribd company logo
Answer:
Note: Provided code shows several bugs, hence I implemented a separate code as per the given
program specifications
Program code:
import java.util.Scanner;
//declares fraction class
class Fraction
{
//private data members for the class
private int numerators,denominators;
//default constructor
public Fraction()
{
}
//parameterised constructor for the class
public Fraction(int numerators, int denominators)
{
//assigns numerator
this.numerators = numerators;
//checks the denominator
if(denominators == 0)
{
System.out.println("denominator Shouldn't be ZERO ");
System.out.println("Program Exiting.....");
System.exit(0);
}
this.denominators = denominators;
}
//parameterised constructor
public Fraction(int numerators)
{
this.numerators = numerators;
}
//method TO string
public String toString()
{
return ""+numerators+" / "+denominators;
}
//methods Equals()
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Fraction other = (Fraction) obj;
if (this.numerators != other.numerators) {
return false;
}
if (this.denominators != other.denominators) {
return false;
}
return true;
}
//method GCD declaration
public int GCD(int a, int b)
{
//declares local variables
int x = 0, GCD = 0;
int max = a > b ? a : b;
int min = a < b ? a : b;
for(int i = 1; i <= min; i++)
{
x = max * i;
if(x % min == 0)
{
GCD = x;
break;
}
}
//returns GCD
return GCD;
}
//Method to read
public static Fraction read()
{
Scanner sc1 = new Scanner(System.in);
System.out.print("Enter Fraction numerators : ");
int num1 = sc1.nextInt();
System.out.print("Enter Fraction denominators : ");
int den1 = sc1.nextInt();
System.out.println();
Fraction f1 = new Fraction(num1,den1);
return f1;
}
//Method definition to add()
public Fraction add(Fraction other1)
{
int GCD = GCD(this.denominators,other.denominators);
int numb1 = this.numerators * (GCD/this.denominators);
int numb2 = other1.numerators *(GCD/other.denominators);
int num1 = 0, den1 = 0;
num1 = numb1 + numb2;
den1 = GCD;
return new Fraction(num1,den1);
}
//method definition to add()
public Fraction add(int temp1)
{
//defines the number variable
int num1 = this.denominators * temp1 + this.numerators;
return new Fraction(num1,this.denominators);
}
//method definition to subract
public Fraction subtract(Fraction other1)
{
int GCD = GCD(this.denominators,other.denominators);
int numb1 = this.numerators *(GCD/this.denominators);
int numb2 = other1.numerators *(GCD/other1.denominators);
int num1 = 0, den1 = 0;
num1 = numb1 - numb2;
den1 = GCD;
return new Fraction(num1,den1);
}
//Method definition to multiply
public Fraction multiply(Fraction other1)
{
int num1 = 0, den1 = 0;
num1 = this.numerators * other1.numerators;
den1 = this.denominators * other1.denominators;
return new Fraction(num1,den1);
}
//method definition to Multiply
public Fraction multiply(int temp1)
{
int num 1= this.numerators * temp1;
return new Fraction(num1,this.denominators);
}
//method definition to Divide
public Fraction divide(Fraction other1)
{
int num1 = 0, den1 = 0;
num1 = this.numerators * other1.denominators;
den1 = this.denominators * other1.numerators;
return new Fraction(num1,den1);
}
//method definition to reciprocal
public Fraction reciprocal()
{
return new Fraction(this.denominators,this.numerators);
}
}
//main class declaration
public class FractionDemoSimpleClass
{
//main program for the class
public static void main (String[] args)
{
//declares object for the class
Fraction rr1 = Fraction.read();
Fraction rr2 = Fraction.read();
//declares instance for the class
Fraction rr3, rr4, rr5, rr6, rr7, rr8, rr9;
//prints the result
System.out.println (" First fraction number: " + rr1);
System.out.println ("Second fraction number: " + rr2);
if (rr1.equals(rr2))
System.out.println (" rr1 and rr2 are equal.");
else
System.out.println (" rr1 and rr2 are NOT equal.");
rr3 = rr2.reciprocal();
System.out.println (" The reciprocal of rr2 is: " + rr3);
//calls add()
rr4 = rr1.add(rr2);
//calls subtract()
rr5 = rr1.subtract(rr2);
//calls multiply()
rr6 = rr1.multiply(rr2);
//calls divide()
rr7 = rr1.divide(rr2);
//calls add()
rr8 = rr1.add(5);
//calls multiply()
rr9 = rr1.multiply(5);
System.out.println ();
System.out.println ("rr1 + rr2: " + rr4);
System.out.println ("rr1 - rr2: " + rr5);
System.out.println ("rr1 * rr2: " + rr6);
System.out.println ("rr1 / rr2: " + rr7);
System.out.println ("rr1 + 5: " + rr8);
System.out.println ("rr1 * 5: " + rr9);
}
}
Sample Output:
C:jdk1.6bin>javac FractionDemoSimpleClass.java
C:jdk1.6bin>java FractionDemoSimpleClass
Enter Fraction numerators : 1
Enter Fraction denominators : 2
Enter Fraction numerators : 2
Enter Fraction denominators : 3
First fraction number: 1 / 2
Second fraction number: 2 / 3
rr1 and rr2 are NOT equal.
The reciprocal of rr2 is: 3 / 2
rr1 + rr2: 7 / 6
rr1 - rr2: -1 / 6
rr1 * rr2: 2 / 6
rr1 / rr2: 3 / 4
rr1 + 5: 11 / 2
rr1 * 5: 5 / 2
C:jdk1.6bin>
Solution
Answer:
Note: Provided code shows several bugs, hence I implemented a separate code as per the given
program specifications
Program code:
import java.util.Scanner;
//declares fraction class
class Fraction
{
//private data members for the class
private int numerators,denominators;
//default constructor
public Fraction()
{
}
//parameterised constructor for the class
public Fraction(int numerators, int denominators)
{
//assigns numerator
this.numerators = numerators;
//checks the denominator
if(denominators == 0)
{
System.out.println("denominator Shouldn't be ZERO ");
System.out.println("Program Exiting.....");
System.exit(0);
}
this.denominators = denominators;
}
//parameterised constructor
public Fraction(int numerators)
{
this.numerators = numerators;
}
//method TO string
public String toString()
{
return ""+numerators+" / "+denominators;
}
//methods Equals()
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Fraction other = (Fraction) obj;
if (this.numerators != other.numerators) {
return false;
}
if (this.denominators != other.denominators) {
return false;
}
return true;
}
//method GCD declaration
public int GCD(int a, int b)
{
//declares local variables
int x = 0, GCD = 0;
int max = a > b ? a : b;
int min = a < b ? a : b;
for(int i = 1; i <= min; i++)
{
x = max * i;
if(x % min == 0)
{
GCD = x;
break;
}
}
//returns GCD
return GCD;
}
//Method to read
public static Fraction read()
{
Scanner sc1 = new Scanner(System.in);
System.out.print("Enter Fraction numerators : ");
int num1 = sc1.nextInt();
System.out.print("Enter Fraction denominators : ");
int den1 = sc1.nextInt();
System.out.println();
Fraction f1 = new Fraction(num1,den1);
return f1;
}
//Method definition to add()
public Fraction add(Fraction other1)
{
int GCD = GCD(this.denominators,other.denominators);
int numb1 = this.numerators * (GCD/this.denominators);
int numb2 = other1.numerators *(GCD/other.denominators);
int num1 = 0, den1 = 0;
num1 = numb1 + numb2;
den1 = GCD;
return new Fraction(num1,den1);
}
//method definition to add()
public Fraction add(int temp1)
{
//defines the number variable
int num1 = this.denominators * temp1 + this.numerators;
return new Fraction(num1,this.denominators);
}
//method definition to subract
public Fraction subtract(Fraction other1)
{
int GCD = GCD(this.denominators,other.denominators);
int numb1 = this.numerators *(GCD/this.denominators);
int numb2 = other1.numerators *(GCD/other1.denominators);
int num1 = 0, den1 = 0;
num1 = numb1 - numb2;
den1 = GCD;
return new Fraction(num1,den1);
}
//Method definition to multiply
public Fraction multiply(Fraction other1)
{
int num1 = 0, den1 = 0;
num1 = this.numerators * other1.numerators;
den1 = this.denominators * other1.denominators;
return new Fraction(num1,den1);
}
//method definition to Multiply
public Fraction multiply(int temp1)
{
int num 1= this.numerators * temp1;
return new Fraction(num1,this.denominators);
}
//method definition to Divide
public Fraction divide(Fraction other1)
{
int num1 = 0, den1 = 0;
num1 = this.numerators * other1.denominators;
den1 = this.denominators * other1.numerators;
return new Fraction(num1,den1);
}
//method definition to reciprocal
public Fraction reciprocal()
{
return new Fraction(this.denominators,this.numerators);
}
}
//main class declaration
public class FractionDemoSimpleClass
{
//main program for the class
public static void main (String[] args)
{
//declares object for the class
Fraction rr1 = Fraction.read();
Fraction rr2 = Fraction.read();
//declares instance for the class
Fraction rr3, rr4, rr5, rr6, rr7, rr8, rr9;
//prints the result
System.out.println (" First fraction number: " + rr1);
System.out.println ("Second fraction number: " + rr2);
if (rr1.equals(rr2))
System.out.println (" rr1 and rr2 are equal.");
else
System.out.println (" rr1 and rr2 are NOT equal.");
rr3 = rr2.reciprocal();
System.out.println (" The reciprocal of rr2 is: " + rr3);
//calls add()
rr4 = rr1.add(rr2);
//calls subtract()
rr5 = rr1.subtract(rr2);
//calls multiply()
rr6 = rr1.multiply(rr2);
//calls divide()
rr7 = rr1.divide(rr2);
//calls add()
rr8 = rr1.add(5);
//calls multiply()
rr9 = rr1.multiply(5);
System.out.println ();
System.out.println ("rr1 + rr2: " + rr4);
System.out.println ("rr1 - rr2: " + rr5);
System.out.println ("rr1 * rr2: " + rr6);
System.out.println ("rr1 / rr2: " + rr7);
System.out.println ("rr1 + 5: " + rr8);
System.out.println ("rr1 * 5: " + rr9);
}
}
Sample Output:
C:jdk1.6bin>javac FractionDemoSimpleClass.java
C:jdk1.6bin>java FractionDemoSimpleClass
Enter Fraction numerators : 1
Enter Fraction denominators : 2
Enter Fraction numerators : 2
Enter Fraction denominators : 3
First fraction number: 1 / 2
Second fraction number: 2 / 3
rr1 and rr2 are NOT equal.
The reciprocal of rr2 is: 3 / 2
rr1 + rr2: 7 / 6
rr1 - rr2: -1 / 6
rr1 * rr2: 2 / 6
rr1 / rr2: 3 / 4
rr1 + 5: 11 / 2
rr1 * 5: 5 / 2
C:jdk1.6bin>

More Related Content

Similar to AnswerNote Provided code shows several bugs, hence I implemented.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
manjan6
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
#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
exxonzone
 
Listing for TryLambdaExpressions
Listing for TryLambdaExpressionsListing for TryLambdaExpressions
Listing for TryLambdaExpressionsDerek Dhammaloka
 
When we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdfWhen we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdf
arihantkitchenmart
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
varadasuren
 
Hi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdfHi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdf
aniyathikitchen
 
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
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdf
AroraRajinder1
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdf
lakshmijewellery
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
HUST
 
C# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdfC# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdf
fathimalinks
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
Harish Gyanani
 
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
 
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
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
Akira Maruoka
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
Mahyuddin8
 
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
 
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
leventhalbrad49439
 

Similar to AnswerNote Provided code shows several bugs, hence I implemented.pdf (20)

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
 
Functions
FunctionsFunctions
Functions
 
#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
 
Listing for TryLambdaExpressions
Listing for TryLambdaExpressionsListing for TryLambdaExpressions
Listing for TryLambdaExpressions
 
When we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdfWhen we test your Fraction.java we will use the given FractionTester.pdf
When we test your Fraction.java we will use the given FractionTester.pdf
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
Hi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .pdfHi,I have updated your code. It is working fine now. Highllighted .pdf
Hi,I have updated your code. It is working fine now. Highllighted .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
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdf
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdf
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
 
C# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdfC# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdf
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
 
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
 
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
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
 
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
 
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
 

More from anurag1231

a. The alleles are as followsaa - AlbinismAa AA - Normal pigm.pdf
a. The alleles are as followsaa - AlbinismAa  AA - Normal pigm.pdfa. The alleles are as followsaa - AlbinismAa  AA - Normal pigm.pdf
a. The alleles are as followsaa - AlbinismAa AA - Normal pigm.pdf
anurag1231
 
9933Solution9933.pdf
9933Solution9933.pdf9933Solution9933.pdf
9933Solution9933.pdf
anurag1231
 
49) sensation is what we receive through senses and perception is wh.pdf
49) sensation is what we receive through senses and perception is wh.pdf49) sensation is what we receive through senses and perception is wh.pdf
49) sensation is what we receive through senses and perception is wh.pdf
anurag1231
 
1. INTRODUCTIONWe are currently living in the so-called informatio.pdf
1. INTRODUCTIONWe are currently living in the so-called informatio.pdf1. INTRODUCTIONWe are currently living in the so-called informatio.pdf
1. INTRODUCTIONWe are currently living in the so-called informatio.pdf
anurag1231
 
1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf
1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf
1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf
anurag1231
 
1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf
1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf
1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf
anurag1231
 
1)we can see the universe mostly in the form hydrogen and helium. .pdf
1)we can see the universe mostly in the form hydrogen and helium. .pdf1)we can see the universe mostly in the form hydrogen and helium. .pdf
1)we can see the universe mostly in the form hydrogen and helium. .pdf
anurag1231
 
1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf
1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf
1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf
anurag1231
 
1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf
1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf
1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf
anurag1231
 
0.8726Solution0.8726.pdf
0.8726Solution0.8726.pdf0.8726Solution0.8726.pdf
0.8726Solution0.8726.pdf
anurag1231
 
UV radiation has shorter wavelength than visible .pdf
                     UV radiation has shorter wavelength than visible .pdf                     UV radiation has shorter wavelength than visible .pdf
UV radiation has shorter wavelength than visible .pdf
anurag1231
 
(1) CA genetic model organism needs to have all the above features.pdf
(1) CA genetic model organism needs to have all the above features.pdf(1) CA genetic model organism needs to have all the above features.pdf
(1) CA genetic model organism needs to have all the above features.pdf
anurag1231
 
The relation in partSolution The relation in part.pdf
 The relation in partSolution The relation in part.pdf The relation in partSolution The relation in part.pdf
The relation in partSolution The relation in part.pdf
anurag1231
 
Step1 Concentration of hydroxide ion = 2x.00500=..pdf
                     Step1 Concentration of hydroxide ion = 2x.00500=..pdf                     Step1 Concentration of hydroxide ion = 2x.00500=..pdf
Step1 Concentration of hydroxide ion = 2x.00500=..pdf
anurag1231
 
molality = (11258.5)1 = 1.914 m Percent mass =.pdf
                     molality = (11258.5)1 = 1.914 m  Percent mass =.pdf                     molality = (11258.5)1 = 1.914 m  Percent mass =.pdf
molality = (11258.5)1 = 1.914 m Percent mass =.pdf
anurag1231
 
NADPH is a reducing agent that act as an electron.pdf
                     NADPH is a reducing agent that act as an electron.pdf                     NADPH is a reducing agent that act as an electron.pdf
NADPH is a reducing agent that act as an electron.pdf
anurag1231
 
moleculesions geometry hybridization ClF3 T-shap.pdf
                     moleculesions geometry hybridization ClF3 T-shap.pdf                     moleculesions geometry hybridization ClF3 T-shap.pdf
moleculesions geometry hybridization ClF3 T-shap.pdf
anurag1231
 
The major shortcoming was that the electrons were.pdf
                     The major shortcoming was that the electrons were.pdf                     The major shortcoming was that the electrons were.pdf
The major shortcoming was that the electrons were.pdf
anurag1231
 
The ball is an atom, the stick is a bond. You pu.pdf
                     The ball is an atom, the stick is a bond.  You pu.pdf                     The ball is an atom, the stick is a bond.  You pu.pdf
The ball is an atom, the stick is a bond. You pu.pdf
anurag1231
 
The anion is a good leaving group because it is p.pdf
                     The anion is a good leaving group because it is p.pdf                     The anion is a good leaving group because it is p.pdf
The anion is a good leaving group because it is p.pdf
anurag1231
 

More from anurag1231 (20)

a. The alleles are as followsaa - AlbinismAa AA - Normal pigm.pdf
a. The alleles are as followsaa - AlbinismAa  AA - Normal pigm.pdfa. The alleles are as followsaa - AlbinismAa  AA - Normal pigm.pdf
a. The alleles are as followsaa - AlbinismAa AA - Normal pigm.pdf
 
9933Solution9933.pdf
9933Solution9933.pdf9933Solution9933.pdf
9933Solution9933.pdf
 
49) sensation is what we receive through senses and perception is wh.pdf
49) sensation is what we receive through senses and perception is wh.pdf49) sensation is what we receive through senses and perception is wh.pdf
49) sensation is what we receive through senses and perception is wh.pdf
 
1. INTRODUCTIONWe are currently living in the so-called informatio.pdf
1. INTRODUCTIONWe are currently living in the so-called informatio.pdf1. INTRODUCTIONWe are currently living in the so-called informatio.pdf
1. INTRODUCTIONWe are currently living in the so-called informatio.pdf
 
1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf
1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf
1. d.) Organisms using oxygenic photosynthesis release oxygen into t.pdf
 
1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf
1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf
1. All RTKs have a similar molecular architecture, with a ligand-bin.pdf
 
1)we can see the universe mostly in the form hydrogen and helium. .pdf
1)we can see the universe mostly in the form hydrogen and helium. .pdf1)we can see the universe mostly in the form hydrogen and helium. .pdf
1)we can see the universe mostly in the form hydrogen and helium. .pdf
 
1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf
1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf
1)D. Bacteria.2)B. Nuclear envelope3)A. Similar in function and .pdf
 
1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf
1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf
1) Pen R contains penicillin resistance gene .Usually + or - is not .pdf
 
0.8726Solution0.8726.pdf
0.8726Solution0.8726.pdf0.8726Solution0.8726.pdf
0.8726Solution0.8726.pdf
 
UV radiation has shorter wavelength than visible .pdf
                     UV radiation has shorter wavelength than visible .pdf                     UV radiation has shorter wavelength than visible .pdf
UV radiation has shorter wavelength than visible .pdf
 
(1) CA genetic model organism needs to have all the above features.pdf
(1) CA genetic model organism needs to have all the above features.pdf(1) CA genetic model organism needs to have all the above features.pdf
(1) CA genetic model organism needs to have all the above features.pdf
 
The relation in partSolution The relation in part.pdf
 The relation in partSolution The relation in part.pdf The relation in partSolution The relation in part.pdf
The relation in partSolution The relation in part.pdf
 
Step1 Concentration of hydroxide ion = 2x.00500=..pdf
                     Step1 Concentration of hydroxide ion = 2x.00500=..pdf                     Step1 Concentration of hydroxide ion = 2x.00500=..pdf
Step1 Concentration of hydroxide ion = 2x.00500=..pdf
 
molality = (11258.5)1 = 1.914 m Percent mass =.pdf
                     molality = (11258.5)1 = 1.914 m  Percent mass =.pdf                     molality = (11258.5)1 = 1.914 m  Percent mass =.pdf
molality = (11258.5)1 = 1.914 m Percent mass =.pdf
 
NADPH is a reducing agent that act as an electron.pdf
                     NADPH is a reducing agent that act as an electron.pdf                     NADPH is a reducing agent that act as an electron.pdf
NADPH is a reducing agent that act as an electron.pdf
 
moleculesions geometry hybridization ClF3 T-shap.pdf
                     moleculesions geometry hybridization ClF3 T-shap.pdf                     moleculesions geometry hybridization ClF3 T-shap.pdf
moleculesions geometry hybridization ClF3 T-shap.pdf
 
The major shortcoming was that the electrons were.pdf
                     The major shortcoming was that the electrons were.pdf                     The major shortcoming was that the electrons were.pdf
The major shortcoming was that the electrons were.pdf
 
The ball is an atom, the stick is a bond. You pu.pdf
                     The ball is an atom, the stick is a bond.  You pu.pdf                     The ball is an atom, the stick is a bond.  You pu.pdf
The ball is an atom, the stick is a bond. You pu.pdf
 
The anion is a good leaving group because it is p.pdf
                     The anion is a good leaving group because it is p.pdf                     The anion is a good leaving group because it is p.pdf
The anion is a good leaving group because it is p.pdf
 

Recently uploaded

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 

Recently uploaded (20)

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 

AnswerNote Provided code shows several bugs, hence I implemented.pdf

  • 1. Answer: Note: Provided code shows several bugs, hence I implemented a separate code as per the given program specifications Program code: import java.util.Scanner; //declares fraction class class Fraction { //private data members for the class private int numerators,denominators; //default constructor public Fraction() { } //parameterised constructor for the class public Fraction(int numerators, int denominators) { //assigns numerator this.numerators = numerators; //checks the denominator if(denominators == 0) { System.out.println("denominator Shouldn't be ZERO "); System.out.println("Program Exiting....."); System.exit(0); } this.denominators = denominators; } //parameterised constructor public Fraction(int numerators) { this.numerators = numerators; } //method TO string public String toString()
  • 2. { return ""+numerators+" / "+denominators; } //methods Equals() @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Fraction other = (Fraction) obj; if (this.numerators != other.numerators) { return false; } if (this.denominators != other.denominators) { return false; } return true; } //method GCD declaration public int GCD(int a, int b) { //declares local variables int x = 0, GCD = 0; int max = a > b ? a : b; int min = a < b ? a : b; for(int i = 1; i <= min; i++) { x = max * i; if(x % min == 0) { GCD = x; break; }
  • 3. } //returns GCD return GCD; } //Method to read public static Fraction read() { Scanner sc1 = new Scanner(System.in); System.out.print("Enter Fraction numerators : "); int num1 = sc1.nextInt(); System.out.print("Enter Fraction denominators : "); int den1 = sc1.nextInt(); System.out.println(); Fraction f1 = new Fraction(num1,den1); return f1; } //Method definition to add() public Fraction add(Fraction other1) { int GCD = GCD(this.denominators,other.denominators); int numb1 = this.numerators * (GCD/this.denominators); int numb2 = other1.numerators *(GCD/other.denominators); int num1 = 0, den1 = 0; num1 = numb1 + numb2; den1 = GCD; return new Fraction(num1,den1); } //method definition to add() public Fraction add(int temp1) { //defines the number variable int num1 = this.denominators * temp1 + this.numerators; return new Fraction(num1,this.denominators); } //method definition to subract public Fraction subtract(Fraction other1)
  • 4. { int GCD = GCD(this.denominators,other.denominators); int numb1 = this.numerators *(GCD/this.denominators); int numb2 = other1.numerators *(GCD/other1.denominators); int num1 = 0, den1 = 0; num1 = numb1 - numb2; den1 = GCD; return new Fraction(num1,den1); } //Method definition to multiply public Fraction multiply(Fraction other1) { int num1 = 0, den1 = 0; num1 = this.numerators * other1.numerators; den1 = this.denominators * other1.denominators; return new Fraction(num1,den1); } //method definition to Multiply public Fraction multiply(int temp1) { int num 1= this.numerators * temp1; return new Fraction(num1,this.denominators); } //method definition to Divide public Fraction divide(Fraction other1) { int num1 = 0, den1 = 0; num1 = this.numerators * other1.denominators; den1 = this.denominators * other1.numerators; return new Fraction(num1,den1); } //method definition to reciprocal public Fraction reciprocal() { return new Fraction(this.denominators,this.numerators); }
  • 5. } //main class declaration public class FractionDemoSimpleClass { //main program for the class public static void main (String[] args) { //declares object for the class Fraction rr1 = Fraction.read(); Fraction rr2 = Fraction.read(); //declares instance for the class Fraction rr3, rr4, rr5, rr6, rr7, rr8, rr9; //prints the result System.out.println (" First fraction number: " + rr1); System.out.println ("Second fraction number: " + rr2); if (rr1.equals(rr2)) System.out.println (" rr1 and rr2 are equal."); else System.out.println (" rr1 and rr2 are NOT equal."); rr3 = rr2.reciprocal(); System.out.println (" The reciprocal of rr2 is: " + rr3); //calls add() rr4 = rr1.add(rr2); //calls subtract() rr5 = rr1.subtract(rr2); //calls multiply() rr6 = rr1.multiply(rr2); //calls divide() rr7 = rr1.divide(rr2); //calls add() rr8 = rr1.add(5); //calls multiply() rr9 = rr1.multiply(5); System.out.println (); System.out.println ("rr1 + rr2: " + rr4); System.out.println ("rr1 - rr2: " + rr5);
  • 6. System.out.println ("rr1 * rr2: " + rr6); System.out.println ("rr1 / rr2: " + rr7); System.out.println ("rr1 + 5: " + rr8); System.out.println ("rr1 * 5: " + rr9); } } Sample Output: C:jdk1.6bin>javac FractionDemoSimpleClass.java C:jdk1.6bin>java FractionDemoSimpleClass Enter Fraction numerators : 1 Enter Fraction denominators : 2 Enter Fraction numerators : 2 Enter Fraction denominators : 3 First fraction number: 1 / 2 Second fraction number: 2 / 3 rr1 and rr2 are NOT equal. The reciprocal of rr2 is: 3 / 2 rr1 + rr2: 7 / 6 rr1 - rr2: -1 / 6 rr1 * rr2: 2 / 6 rr1 / rr2: 3 / 4 rr1 + 5: 11 / 2 rr1 * 5: 5 / 2 C:jdk1.6bin> Solution Answer: Note: Provided code shows several bugs, hence I implemented a separate code as per the given program specifications Program code: import java.util.Scanner; //declares fraction class class Fraction { //private data members for the class
  • 7. private int numerators,denominators; //default constructor public Fraction() { } //parameterised constructor for the class public Fraction(int numerators, int denominators) { //assigns numerator this.numerators = numerators; //checks the denominator if(denominators == 0) { System.out.println("denominator Shouldn't be ZERO "); System.out.println("Program Exiting....."); System.exit(0); } this.denominators = denominators; } //parameterised constructor public Fraction(int numerators) { this.numerators = numerators; } //method TO string public String toString() { return ""+numerators+" / "+denominators; } //methods Equals() @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) {
  • 8. return false; } final Fraction other = (Fraction) obj; if (this.numerators != other.numerators) { return false; } if (this.denominators != other.denominators) { return false; } return true; } //method GCD declaration public int GCD(int a, int b) { //declares local variables int x = 0, GCD = 0; int max = a > b ? a : b; int min = a < b ? a : b; for(int i = 1; i <= min; i++) { x = max * i; if(x % min == 0) { GCD = x; break; } } //returns GCD return GCD; } //Method to read public static Fraction read() { Scanner sc1 = new Scanner(System.in); System.out.print("Enter Fraction numerators : "); int num1 = sc1.nextInt();
  • 9. System.out.print("Enter Fraction denominators : "); int den1 = sc1.nextInt(); System.out.println(); Fraction f1 = new Fraction(num1,den1); return f1; } //Method definition to add() public Fraction add(Fraction other1) { int GCD = GCD(this.denominators,other.denominators); int numb1 = this.numerators * (GCD/this.denominators); int numb2 = other1.numerators *(GCD/other.denominators); int num1 = 0, den1 = 0; num1 = numb1 + numb2; den1 = GCD; return new Fraction(num1,den1); } //method definition to add() public Fraction add(int temp1) { //defines the number variable int num1 = this.denominators * temp1 + this.numerators; return new Fraction(num1,this.denominators); } //method definition to subract public Fraction subtract(Fraction other1) { int GCD = GCD(this.denominators,other.denominators); int numb1 = this.numerators *(GCD/this.denominators); int numb2 = other1.numerators *(GCD/other1.denominators); int num1 = 0, den1 = 0; num1 = numb1 - numb2; den1 = GCD; return new Fraction(num1,den1); } //Method definition to multiply
  • 10. public Fraction multiply(Fraction other1) { int num1 = 0, den1 = 0; num1 = this.numerators * other1.numerators; den1 = this.denominators * other1.denominators; return new Fraction(num1,den1); } //method definition to Multiply public Fraction multiply(int temp1) { int num 1= this.numerators * temp1; return new Fraction(num1,this.denominators); } //method definition to Divide public Fraction divide(Fraction other1) { int num1 = 0, den1 = 0; num1 = this.numerators * other1.denominators; den1 = this.denominators * other1.numerators; return new Fraction(num1,den1); } //method definition to reciprocal public Fraction reciprocal() { return new Fraction(this.denominators,this.numerators); } } //main class declaration public class FractionDemoSimpleClass { //main program for the class public static void main (String[] args) { //declares object for the class Fraction rr1 = Fraction.read(); Fraction rr2 = Fraction.read();
  • 11. //declares instance for the class Fraction rr3, rr4, rr5, rr6, rr7, rr8, rr9; //prints the result System.out.println (" First fraction number: " + rr1); System.out.println ("Second fraction number: " + rr2); if (rr1.equals(rr2)) System.out.println (" rr1 and rr2 are equal."); else System.out.println (" rr1 and rr2 are NOT equal."); rr3 = rr2.reciprocal(); System.out.println (" The reciprocal of rr2 is: " + rr3); //calls add() rr4 = rr1.add(rr2); //calls subtract() rr5 = rr1.subtract(rr2); //calls multiply() rr6 = rr1.multiply(rr2); //calls divide() rr7 = rr1.divide(rr2); //calls add() rr8 = rr1.add(5); //calls multiply() rr9 = rr1.multiply(5); System.out.println (); System.out.println ("rr1 + rr2: " + rr4); System.out.println ("rr1 - rr2: " + rr5); System.out.println ("rr1 * rr2: " + rr6); System.out.println ("rr1 / rr2: " + rr7); System.out.println ("rr1 + 5: " + rr8); System.out.println ("rr1 * 5: " + rr9); } } Sample Output: C:jdk1.6bin>javac FractionDemoSimpleClass.java C:jdk1.6bin>java FractionDemoSimpleClass Enter Fraction numerators : 1
  • 12. Enter Fraction denominators : 2 Enter Fraction numerators : 2 Enter Fraction denominators : 3 First fraction number: 1 / 2 Second fraction number: 2 / 3 rr1 and rr2 are NOT equal. The reciprocal of rr2 is: 3 / 2 rr1 + rr2: 7 / 6 rr1 - rr2: -1 / 6 rr1 * rr2: 2 / 6 rr1 / rr2: 3 / 4 rr1 + 5: 11 / 2 rr1 * 5: 5 / 2 C:jdk1.6bin>