public class StoreRatioNumberClass
{
private int num;
private int den;
public RationalNumber() //here
{
num = 0;
den = 1;
}
public RationalNumber(int newNum, int newDen) //and here, but it gives me 3 separate errors for it//
{
num = newNum;
den = newDen;
simplify();
}
private void simplify()
{
int gcd = gcd();
finalNum = num/gcd;
finalDen = den/gcd;
}
private static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public double getValue()
{
return (double)num/den;
}
public String toString()
{
return(num + "/" + den);
}
}
My question is how do I solve this compiling issue on lines 7 & 11? This class takes the numerators and denominators entered in the main method class not shown here and simplifies the rational number with the GCD. Also, when I do put in a return type it just comes up with even more errors and warnings so I'm stumped! Thank you for looking and all of your inputs.
You need to use the name of your class in constructor:
public StoreRatioNumberClass(int newNum, int newDen) {
//...
otherwise compiler will think you are about to declare a method and is confused about the missing return type obviously
You named your class StoreRatioNumberClass, as in the following class declaration
public class StoreRatioNumberClass
I believe you were trying to define constructors for this class, in the lines the compiler complains. Constructors must have the same name as the class, otherwise, they're interpreted as method names (such as your simplify
). However, these false method definitions miss their return type.
Change
public RationalNumber() //here
to
public StoreRatioNumberClass() //here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With