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

I am getting no out put for my functions. Can someone explain why after entering

ID: 3841415 • Letter: I

Question

I am getting no out put for my functions. Can someone explain why after entering the values the menu doesnt out put the results? here is both my classes. currently I only have "add" finished and when user enters the value the menu just wants to exit instead of displaying the output and also, is there a way where I can allow the user to loop back to the menu to do subtraction and other stuff?

Fraction.Java

public class Fraction {
  
private long numerator;
private long denominator;

public Fraction( ) {

numerator = 1;
denominator = 1;

}

public Fraction( long numerator, long denominator ) {

this.numerator = numerator;

if ( denominator == 0 ) {
  
System.out.println("WARNING: Fraction( long, long ) Constructor");
System.out.println(" A value of zero for denominator detected.");
System.out.println(" The value has been changed to one.");
  
this.denominator = 1;

}

else

this.denominator = denominator;


Reduce();


}
public Fraction( Fraction fraction ) {

if (fraction == null) {
System.out.println("Fraction Constructor: Deep Copy Error.");
System.exit( 0 );

}


numerator = fraction.numerator;
denominator = fraction.denominator;

Reduce();

}
public void Add( Fraction that ) {

long a;
long b;
long c;
long d;

a = this.numerator;
b = this.denominator;

c = that.numerator;

d = that.denominator;

this.numerator = a*d + c*b;
this.denominator = b*d;
  
Reduce();


}
  
public void Subtract( Fraction that ) {

long a;
long b;
long c;
long d;

a = this.numerator;
b = this.denominator;

c = that.numerator;

d = that.denominator;

this.numerator = a*d - c*b;
this.denominator = b*d;
  
Reduce();


}
  
public void Divide( Fraction that ) {

long a;
long b;
long c;
long d;

a = this.numerator;
b = this.denominator;

c = that.numerator;
d = that.denominator;

this.numerator = a*d;
this.denominator = c*d;
  
Reduce();


}
  
public void Multiply( Fraction that ) {

long a;
long b;
long c;
long d;

a = this.numerator;
b = this.denominator;

c = that.numerator;

d = that.denominator;

this.numerator = a*c;
this.denominator = b*d;
  
Reduce();


}
  
public void SetNumerator( long numerator ) {

this.numerator = numerator;
Reduce();


}
public void SetDenominatortor( long denominator ) {


if ( denominator == 0 ) {
System.out.println("WARNING: SetDenominatortor( long ) Mutator Method.");
System.out.println(" A value of zero for denominator detected.");
System.out.println(" The value has been changed to one.");
this.denominator = 1;

}

else

this.denominator = denominator;

Reduce();


}
  
public void Set( Fraction fraction ) {

numerator = fraction.numerator;
denominator = fraction.denominator;
  
Reduce();


}

public double GetNumerator( ) {

return numerator;

}

public double GetDenominator( ) {


return denominator;

}

public Fraction Get( ) {


return new Fraction(numerator, denominator);
  

}

@Override
public String toString( ) {


return numerator + "/" + denominator;

}

@Override

public int hashCode( ) {


int hash = 7;

hash = 41 * hash + (int) (this.numerator ^ (this.numerator >>> 32));
hash = 41 * hash + (int) (this.denominator ^ (this.denominator >>> 32));


return hash;

}

@Override

public boolean equals(Object otherObject ) {


if ( otherObject == null )
return false;

else if ( getClass() != otherObject.getClass() )

return false;

else {

Fraction otherFraction = (Fraction)otherObject;

return ( (numerator == otherFraction.numerator) &&

(denominator == otherFraction.denominator) );

}

}
private long GreatestCommonDivisor( long a,
long b ) {

if ( a%b == 0 )
return b;
else
return GreatestCommonDivisor(b, a%b);


}
  
private long IterativeGCD( long a,
long b ) {

if ( a == 0 )
  
return b;

while ( b != 0 ){

if ( a > b )
a = a - b;
  
else
  
b = b - a;


} // while
  
return a;

}

private void Reduce( ) {


long gcd = GreatestCommonDivisor( numerator, denominator );

numerator = numerator/gcd;

denominator = denominator/gcd;
}

}

----------------------------------------------------------------

Main.java

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Fraction fobj = new Fraction();
Fraction fobj1 = new Fraction();
int choice,ch,n,d;
  
Scanner scan=new Scanner(System.in);
do{
System.out.println("MENU");
System.out.println("Enter 1 for Addition.");
System.out.println("Enter 2 for Substraction.");
System.out.println("Enter 3 for Multiplication.");
System.out.println("Enter 4 for Division.");
System.out.println("Enter your choice: ");
choice=scan.nextInt();
switch(choice){   
  
case 1:
System.out.println("Enter the value of numerator: ");
n = scan.nextInt();
System.out.println("Enter The value of the denominator: ");
d = scan.nextInt();
fobj1.SetNumerator(n);
fobj1.SetDenominatortor(d);
  
System.out.println("Enter the value of numerator: ");
n = scan.nextInt();
System.out.println("Enter The value of the denominator: ");
d = scan.nextInt();
fobj.SetNumerator(n);
fobj.SetDenominatortor(d);
fobj.Add(fobj1); //calling the Add function
break;
case 2:
fobj.Subtract(fobj1);
break;
case 3:
fobj.Divide(fobj1);
break;
case 4:
fobj.Multiply(fobj1);
break;
default: System.out.println("Wrong choice.");
}
System.out.println("Enter 0 to exit: ");
ch=scan.nextInt();
}
while(ch!=0);
if(ch==0){
System.out.println("Exit!!Thank You.");
}
}
}

Explanation / Answer

The reason there is no output coming is that your have not written any statement to print the output on screen.

in java, generally System.out.println("") is used to print an output on screen.

Below is your modified main class with implementation added for other operations as well as added the print statements to display the output. Also added the code to continue the program for other operations.

public class Main {
  
public static void main(String[] args) {
Fraction fobj = new Fraction();
Fraction fobj1 = new Fraction();
int choice,ch,n,d;
  
Scanner scan=new Scanner(System.in);
do{
System.out.println("MENU");
System.out.println("Enter 1 for Addition.");
System.out.println("Enter 2 for Substraction.");
System.out.println("Enter 3 for Multiplication.");
System.out.println("Enter 4 for Division.");
System.out.println("Enter your choice: ");
choice=scan.nextInt();
  
//Since we will need these values in all 4 operations , moved it out at this common location
System.out.println("Enter the value of numerator: ");
n = scan.nextInt();
System.out.println("Enter The value of the denominator: ");
d = scan.nextInt();
fobj1.SetNumerator(n);
fobj1.SetDenominatortor(d);
  
System.out.println("Enter the value of numerator: ");
n = scan.nextInt();
System.out.println("Enter The value of the denominator: ");
d = scan.nextInt();
fobj.SetNumerator(n);
fobj.SetDenominatortor(d);
  
  
switch(choice)
{
case 1:
fobj.Add(fobj1); //calling the Add function
System.out.println("Answer is : "+ fobj.toString()); //Added for printing output
break;
case 2:
fobj.Subtract(fobj1);
System.out.println("Answer is : "+ fobj.toString());
break;
case 3:
fobj.Multiply(fobj1); //corrected to call multiply on pressing 3
System.out.println("Answer is : "+ fobj.toString());
break;
case 4:

fobj.Divide(fobj1);//corrected to call Divide on pressing 4
System.out.println("Answer is : "+ fobj.toString());
break;
default: System.out.println("Wrong choice.");
}
System.out.println("Press 1 to do continue : "); //Modified for continuing
ch=scan.nextInt();
}
while(ch==1);
  
System.out.println("Exit!!Thank You.");
  
}
}