Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need someone to add comments to the code below some I can understand what each

ID: 3695824 • Letter: I

Question

I need someone to add comments to the code below some I can understand what each step is and how to write it.

Thank you I will comment and rate.

int Num;

int Den;

public Fraction(){

Num=0;

Den=1;

}

public Fraction(int Num,int Den){

this.Num=Num;

this.Den=Den;

}

public Fraction multipliedBy(Fraction f){

Fraction temp=new Fraction();

temp.Num=this.Num*f.Num;

temp.Den=this.Den*f.Den;

temp.reduce();

return temp;

}

public Fraction dividedBy(Fraction f){

Fraction temp=new Fraction();

temp.Num=this.Num*f.Den;

temp.Den=this.Den*f.Num;

temp.reduce();

return temp;

}

public Fraction addedTo(Fraction f){

Fraction temp=new Fraction();

temp.Num=this.Num*f.Den+this.Den*f.Num;

temp.Den=this.Den*f.Den;

temp.reduce();

return temp;

}

public Fraction subtract(Fraction f){

Fraction temp=new Fraction();

temp.Num=this.Num*f.Den-this.Den*f.Num;

temp.Den=this.Den*f.Den;

temp.reduce();

return temp;

}

public boolean isEqualTo(Fraction f){

return ((this.Num==f.Num) && (this.Den==f.Den));

}

public void reduce(){

int i=2;

int nm=this.Num;

int dn=this.Den;

while(i

if(this.Num%i==0 && this.Den%i==0){

this.Num=this.Num/i;

this.Den=this.Den/i;

}else{i++;}

}

}

public int min(int a,int b){return a

public void print(){

System.out.println(this.Num+"/"+this.Den);

}

public static void main(String[] args) {

Fraction f1 = new Fraction(9,8);

Fraction f2 = new Fraction(2,3);

Fraction result = new Fraction();

System.out.print("The result starts off at ");

result.print();

System.out.println();

System.out.print("The product of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.multipliedBy(f2);

result.print();

System.out.println();

System.out.print("The quotient of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.dividedBy(f2);

result.print();

System.out.println();

System.out.print("The sum of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.addedTo(f2);

result.print();

System.out.println();

System.out.print("The difference of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.subtract(f2);

result.print();

System.out.println();

if (f1.isEqualTo(f2)){

System.out.println("The two Fractions are equal.");

} else {

System.out.println("The two Fractions are not equal.");

}

Fraction f3 = new Fraction(12, 8);

Fraction f4 = new Fraction(202, 303);

result = f3.multipliedBy(f4);

System.out.print("The product of ");

f3.print();

System.out.print(" and ");

f4.print();

System.out.print(" is ");

result.print();

System.out.println();

}

}

Explanation / Answer

Answer:

_______

int Num; // declaration of Global variable Num

int Den; // declaration of global variable Den

public Fraction() // declaration of public method Fraction()

{ // fraction() method scope starts here

Num=0; // declaration of local variable Num and initialized to zero

Den=1; // declaration of local variable Den and initialized to one

} // fraction() method scope ends here

public Fraction(int Num,int Den) // declaration of Fraction() method with parameters Num and Den

{ // parameterized Fraction() method scope starts here

this.Num=Num; // parameter of Fraction() method Num is initialized to Num variable,

here 'this' reffers to the current object of parameterized Fraction() method

this.Den=Den; // parameter of Fraction() method Den is initialized to Den variable,

here 'this' reffers to the current object of parameterized Fraction() method

} // parameterized Fraction() method scope ends here

public Fraction multipliedBy(Fraction f) // declaration of public Fraction multipliedBy method with parameter Fraction and reference f

{ // Fraction multipliedBy() method scope starts here

Fraction temp=new Fraction(); // Object creation for Fraction class with reference temp

temp.Num=this.Num*f.Num; // multiplication of Fraction class Num variable and Fraction multilpiedBy() method Num variable is initialized to calling Num variable with reference temp

temp.Den=this.Den*f.Den; // multiplication of Fraction class Den variable and Fraction multilpiedBy() method Den variable is initialized to calling Den variable with reference temp

temp.reduce(); // call reduce() method with reference temp

return temp; // return temp value

} // Fraction multipliedBy() method scope ends here

public Fraction dividedBy(Fraction f) // declaration of public Fraction divideBy method with parameter Fraction and reference f

{ //  Fraction divideBy() method scope starts here

Fraction temp=new Fraction(); // Object creation for Fraction class with reference temp

temp.Num=this.Num*f.Den; // multiplication of Fraction class Num variable and Fraction divideBy() method Den variable is initialized to calling Num variable with reference temp

temp.Den=this.Den*f.Num;   // multiplication of Fraction class Den variable and Fraction divideBy() method Den variable is initialized to calling Num variable with reference temp

temp.reduce();    // call reduce() method with reference temp

return temp; // return temp value

} //  Fraction divideBy() method scope ends here

public Fraction addedTo(Fraction f) // declaration of public Fraction addedTo() method with with parameter Fraction and reference f

{ // Fraction addedTo() method scope starts here

Fraction temp=new Fraction(); // Object creation for Fraction class with reference temp

temp.Num=this.Num*f.Den+this.Den*f.Num;   // addition of multiplication of Fraction class Num variable and Fraction addedTo() method Den variable and multiplication of Fraction class Num variable and Fraction addedTo() method Den variable is initialized to calling Num variable with reference temp

temp.Den=this.Den*f.Den; // multiplication of Fraction class Den variable and Fraction addedTo() method Den variable is initialized to calling Den variable with reference temp

temp.reduce();   // call reduce() method with reference temp

return temp; // return temp value

}   // Fraction addedTo() method scope ends here

public Fraction subtract(Fraction f)   // declaration of public Fraction subtract() method with with parameter Fraction and reference f

{ // Fraction subtract method scope starts here

Fraction temp=new Fraction(); // Object creation for Fraction class with reference temp

temp.Num=this.Num*f.Den-this.Den*f.Num; // subtraction of multiplication of Fraction class Num variable and Fraction subtarct() method Den variable and multiplication of Fraction class Num variable and Fraction subtract() method Den variable is initialized to calling Num variable with reference temp

temp.Den=this.Den*f.Den;   // multiplication of Fraction class Den variable and Fraction subtract() method Den variable is initialized to calling Den variable with reference temp

temp.reduce(); // call reduce() method with reference temp

return temp; // return temp value

} // Fraction subtract() method scope ends here

public boolean isEqualTo(Fraction f) // declaration of public boolean isEqualTo() method with parameter Fraction with reference f

{ // public boolean isEqualTo() method scope starts here

return ((this.Num==f.Num) && (this.Den==f.Den)); //returns the value, if Num variable of Fraction class is equal ti Num variable of isEqualTo() method and Den variable of Fraction class and Den variable of isEqualTo() method which is true or false

} // isEqualTo() method scope ends here

public void reduce() // declaration of reduce() method with void return type

{ // reduce() method scope starts here

int i=2; // declaration and initialization of local variable integer i

int nm=this.Num; // declaration of integer variable nm and it is initialized Num variable of current class which is Fraction

int dn=this.Den; // declaration of integer variable dn and it is initialized Den variable of current class which is Fraction

while(i // incomplete condition of while

if(this.Num%i==0 && this.Den%i==0) // if condition to check Num variable of Fraction class modular division of i is equal to zero and Den variable of Fraction class modular division of i is equal to zero

{ // if condition scope starts here

this.Num=this.Num/i; // initialization of Num variable of Fraction class is divided by i to Num variable of Fraction class

this.Den=this.Den/i; // initialization of Den variable of Fraction class is divided by i to Den variable of Fraction class

} // if condition scope ends here

else{ // else condition for if

i++;} // increment of i

} // while loop scope ends here

} // reduce() method scope ends here

public int min(int a,int b) // initialization of min() method with two integers a and b

{ // min() method scope starts here

return a // retuns value of a

public void print() // declaration of print() method with return type void

{ // print metgod scope starts here

System.out.println(this.Num+"/"+this.Den); // prints value of this.Num/this.Den

} // print method scope ends here

public static void main(String[] args) // main method declaration

{ // main method scope starts here

Fraction f1 = new Fraction(9,8); // instantiation of Fraction class with passing two parameters by giving reference f1

Fraction f2 = new Fraction(2,3); // instantiation of Fraction class with passing two parameters by giving reference f2

Fraction result = new Fraction();   // instantiation of Fraction class with reference result

System.out.print("The result starts off at "); //prints result starts off at

result.print(); // clalling method print() with reference result

System.out.println(); // prints empty space

System.out.print("The product of "); //prints The product of

f1.print(); // clalling method print() with reference f1

System.out.print(" and ");   //prints and

f2.print();    // clalling method print() with reference f2

System.out.print(" is ");   //prints is

result = f1.multipliedBy(f2); // calling multipliedBy() method by passing parameter as f2 with reference f1 is initialized to result

result.print(); // clalling method print() with reference result

System.out.println(); // prints empty space

System.out.print("The quotient of "); //prints The quotient of

f1.print(); // calling print() method with reference f1

System.out.print(" and "); // prints and

f2.print(); // calling print() method with reference f2

System.out.print(" is "); // prints is

result = f1.dividedBy(f2);   // calling dividedBy() method by passing parameter as f2 with reference f1 is initialized to result

result.print();    // calling print() method with reference result

System.out.println(); // prints empty space

System.out.print("The sum of "); //prints The sum of

f1.print(); //calling print() method with reference f1

System.out.print(" and "); // prints and

f2.print(); // calling print() method with reference f2

System.out.print(" is "); // prints is

result = f1.addedTo(f2);    // calling addedTo() method by passing parameter as f2 with reference f1 is initialized to result

result.print();   //calling print() method with reference result

System.out.println(); //prints empty space

System.out.print("The difference of "); //prints The difference of

f1.print(); // calling print method with reference f1

System.out.print(" and "); //prints and

f2.print();   // calling print method with reference f2

System.out.print(" is "); //prints is

result = f1.subtract(f2); // calling subtract() method by passing parameter as f2 with reference f1 is initialized to result

result.print();   // calling print method with reference result

System.out.println(); //prints empty space

if (f1.isEqualTo(f2)){ //if condition to check f1value is equal to f2

System.out.println("The two Fractions are equal."); // if condition is true then prints The two Fractions are equal

} else { // else condition

System.out.println("The two Fractions are not equal."); // if condition is not true then prints The two Fractions are not equal

}

Fraction f3 = new Fraction(12, 8); // instantiation of Fraction class with passing two parameters by giving reference f3

Fraction f4 = new Fraction(202, 303); // instantiation of Fraction class with passing two parameters by giving reference f4

result = f3.multipliedBy(f4); // calling multipledBy() method by passing parameter as f4 with reference f3 and is initialized to result

System.out.print("The product of "); //prints The product of

f3.print(); // calling print() method wit refernce f3

System.out.print(" and "); // prints and

f4.print(); // calling print() method wit refernce f4

System.out.print(" is "); //prints is

result.print(); // calling print() method wit refernce result

System.out.println(); //prints empty space

}

} // main method scope ends here