Here's My First Class:
public class Fraction
{
int Numerator;
int Denominator;
/**
* Constructor for objects of class Fraction
*/
public Fraction()
{
// initialise instance variables
Numerator=0;
Denominator=0;
}
public Fraction(int StartNumerator , int StartDenominator)
{
StartNumerator = Numerator;
StartDenominator = Denominator;
}
public String toString()
{
String FractionOne = Numerator + "/" + Denominator;
String FractionTwo = Numerator + "/" + Denominator;
return FractionOne;
return FractionTwo;
}
}
Here's my second Class:
public class TestFraction
{
public static void main (String [] args)
{
Fraction FractionOne = new Fraction(1 , 4);
Fraction FractionTwo = new Fraction(2 , 3);
System.out.println(FractionOne);
System.out.println(FractionTwo);
}
}
When I compile, I get an error upon: return FractionTwo; as an unreachable statement
Please help me understand what I am doing wrong.
You can't have two return statements one after the other. The second one can never be executed, since the first one will exit the method. That's why the second statement is unreachable.
If what you want is to return both Strings, you should probably concatenate them :
or
or
However, since both of them are identical, I don't see the point in returning both.