I'm writing some code for school (a test class and a primary class), and when I create a test class, the methods I call from the primary class are "undefined for type CylinderTest". When I try to run the program anyway, Eclipse can't even find the main class.
I've looked over so many stack overflow questions for the answer. I've had a similar problem here, but that doesn't fix my current problem. I've also tried (on my own) importing java.util.Scanner into the problem class, but that didn't work, either
File #1
package CirclePackage;
import java.util.Scanner;
public class Cylinder {
public static double askForRadius() {
System.out.println("What would you like the radius of the cylinder to be?");
Scanner in = new Scanner(System.in);
double r = in.nextDouble();
in.close();
return r;
}
public static double askForheight() {
System.out.println("What would you like the height of the cylinder to be?");
Scanner in = new Scanner(System.in);
double h = in.nextDouble();
in.close();
return h;
}
public static double getVolume(double radius, double height) {
double area = Math.PI*radius*radius*height;
return area;
}
}
File #2:
package CirclePackage;
public class CylinderTest {
static void main(String[] args) {
double r = askForRadius(); //<--------------Errors appear HERE,
double h = askForheight(); //<----------------------------HERE,
double result = getVolume(r, h); //<------------------and HERE.
System.out.println("The Volume of the cylinder is: " + result);
}
}
This program is supposed to calculate the volume of a cylinder based on user inputs for the cylinder's radius and height. It does not.
Your main problem is that you are trying to invoke the methods askForRadius
, askForheight
, and getVolume
, without using references in a class where they are undefined. Consequently, the compiler will search for these methods in the class CylinderTest
and, upon not finding them, produce and error. You need to specify where these methods are located before the compiler can compile your code.
To do this is actually quite simple. All need to do is import the class which contains these methods and reference it when you invoke your method. To reference a class, you state its path and name, or, if it is imported, you simply state its name.
double r = Cylinder.askForRadius();
double h = Cylinder.askForheight();
double result = Cylinder.getVolume(r, h);
I suggest you do some supplementary reading on Classes and Objects, and take a look at this question: What are classes, references and objects?
The other main problem here is your main method. If you look at the Oracle Tutorial, you'll see that it says:
In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
The modifiers public and static can be written in either order (
public static
orstatic public
), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".
Now, if you look at your code, you'll see that your main method has the static
modifier, but not the public
one. You better make it public or your program will never run ;)
Alright, that solves the two main problems, but you still have two more small ones.
First of all, you are closing System.in
in your askForRadius
method, so you will not be able to read from it when you call your askForheight
method.
Secondly, you are creating a new scanner instance every time you want to read from System.in
. This is ultimately not a threat, but it is inefficient and very "dirty" code.
The solution to these next two problems is already defined in an answer on another question:
You should create just one Scanner which you use for the life of the program
and you should not close this scanner.