I am getting no out put for my functions. Can someone explain why after entering
ID: 3841482 • 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. Any help is apperciated thank you.
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
Dear friend,
Based upon your description you have two requirements.
Ans for 1) and 2)
You should be doing like this
….
….
….
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){
case1:
….
…
…
…
} // end of switch
System.out.println("Enter 0 to exit: OR Enter 1 to do more operations");
ch=scan.nextInt();
}while(ch != 0);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.