Hi everyone, I am brand new to Java and I like it a lot so far. I am very stuck on an assignment for school that I need help with. We are just learning about method invocation, how to write and call methods in Java. My program should take user generated input for a radius value and output the volume, area, and radius of a sphere in a dialoge box. The Output/Input classes are just classes my professor wrote to allow us a pretty interface (we haven't used sys.out.println yet). I cannot get my method invocation to work. The compilation window tells me the the variables in my method invocation are not yet declared. I am stuck. Help would be so appreciated! thanks

Here is what I've got:

    public class Sphere
    {
        public static void sphereOut(double rad, double volume, double a)
        {

            String menu = "Your radius is: " +rad+ "\n";
            menu = menu + "Your volume is: " +volume+ "\n";
            menu = menu + "Your area is: " +a; 
            Output.showMessage(menu);

        }//method sphereOut

        public static double area (double rad)
        {

            double a;   
            a = 4.0 * 3.14 * rad * rad;
            return a;

        }//method area

        public static double volume (double volume)
        {

        double volume
        4.0/3.0 * 3.14 * rad * rad * rad;
        return volume;

        }//method volume

        public static double getRadius ()
        {

            double rad; 
            rad = Input.readDouble("Enter a positive radius");
            while (rad < 0)
                rad = Input.readDouble("Error enter a positive value");     
            Output.showValue("The radius you entered is: ", rad);
            return rad;

        }//method getRadius     


        public static void main(String [] args)
        {

            Sphere.getRadius();
            Sphere.volume(volume);
            Sphere.area(rad);
            Sphere.sphereOut(rad, volume, a);

        }//method main
    }//class Sphere 

I'm not sure what you are talking about, but my guess is that stuff like this

Output.showMessage(menu);

is where your code crashes. Unless you have a class Output with static methods (along which showMessage(..)) this will cause compile time errors..

my suggestion, for as far as I can see your code, replace the above by:

System.out.println(menu);

the same kind of addaptations for the other methods.

No, I think lines 48~50 are causing the problem (in main()). The reason is that all of those variable being passed to methods are not declare/initialised anywhere inside the main(). I believe the professor asks the OP to enter a real number, not a variable.

// i.e.
Sphere.volumn(5);
...

But then all variable name and the method definition are all wrong though... So not sure what the OP really needs to do.

Edited 4 Years Ago by Taywin

so, Taywin: do you know of an output class with a static showMessage method that can be used without an import? I can't even think of one in the basic api's.

No I don't know what it is. However, the OP said the display is taken from the professor. Therefore, I would assume that it would be as simple as leaving the class in the same directory/folder as the OP class. That way, the program does not need to call import if the program is not being in a package.

This article has been dead for over six months. Start a new discussion instead.