Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling error: Return type for the method is missing

Tags:

java

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.

like image 398
NeverWalkAlone Avatar asked Mar 20 '23 08:03

NeverWalkAlone


2 Answers

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

like image 163
donfuxx Avatar answered Apr 26 '23 00:04

donfuxx


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
like image 45
afsantos Avatar answered Apr 26 '23 01:04

afsantos