Truzen
Truzen

Reputation: 29

Issue returning value from class

So this is my first time with Stackoverflow and this is a Balloon program I am building. This is also my first time building independent classes to be called in the main program and I feel like I'm not understanding something fundamental.

import java.util.Scanner;

public class Inflate
   {
    public static void main(String[] args)
        {
            //Part 1: Open Scanner
            Scanner keyboard = new Scanner(System.in);

            //Part 2: Create a balloon and inflate it
            System.out.println("To what radius would you like to inflate the balloon? ");
            Balloon newBalloon = new Balloon();
            newBalloon.setRadius(keyboard.nextDouble());

            //Part 3: Get the new volume
            System.out.println("The volume is: " + newBalloon.volume);

            //Part 4: Close scanner
            keyboard.close();
        }
}                       

class Balloon
{
    private double radius;
    public double volume;

    Scanner keyboard = new Scanner(System.in);

    public void setRadius(double Radius)
        {
            this.radius=radius;
        }

    public double getVolume()
        {
            volume=radius*radius*radius*Math.PI;
            return volume();
        }
}

The main issue is that my line 38: return volume is stating that the symbol isn't found though it's created at the start of class Balloon.

Also, I don't think my line 13: newBalloon.setRadius is using the right method to define a new radius.

Thanks for any help.

Upvotes: 0

Views: 166

Answers (1)

Reimeus
Reimeus

Reputation: 159774

volume is a field. Remove the parenthesis

return volume;
             ^

To elaborate on this: volume() means that you're trying to execute a method called volume that takes no arguments. By removing the parenthesis the compiler will treat it as a variable, which you do have. You're calling newBalloon.setRadius correctly.

Keep in mind naming conventions though: variables start with a lowercase character unless they're a constant.

Upvotes: 4

Related Questions