I've been working on this code for two days now and I still can't figure out what is going wrong. I'm very new to programming and we just started learning about creating objects and constructors so I appreciate any help I can get. I have to turn in a set of assignments in 3 hours so if you could help me out you'd really be a life saver.

The part that is in BOLD is what I am having trouble with. I believe I'm supposed to call an argument but every time I do it says that I cannot call a static reference to a non-static field. So when I change all of my variables to static and then try to run the program my output is 0 for all of the methods that I try to call.

The purpose of my program is to "demonstrate the Circle class by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference."

import java.util.Scanner; //Needed for the Scanner class

public class Circle
{
	
	double radius; //User's input radius
	final double PI = 3.14159; //Initialized value of PI
	
	//Constructor Method
	public Circle(double radius)
	{
		this.radius = radius;
	}
	
	//The setRadius method accepts an argument that is stored in the radius field
	public void setRadius(double radius)
	{
		this.radius = radius;
	}
	
	//The getRadius method returns the value that is stored in the radius field
	public double getRadius()
	{
		return radius;
	}
	
	//The getArea method returns the value that is stored in the area field
	double area = (PI * radius * radius); //Formula to get the area of the circle
	public double getArea()
	{
		return area;
	}
	//The getDiameter method returns the value that is stored in the diameter field
	double diameter = (radius * 2); //Formula to get the diameter of the circle
	public double getDiameter()
	{
		return diameter;
	}
	//The getCircumference method returns the value that is stored in the circumference field
	double circumference = (2 * PI * radius); //Formula to get the circumference of the circle
	public double getCircumference()
	{
		return circumference;
	}


	public static void main(String[] args) {
		
	//Create a Scanner class for user input
	Scanner keyboard = new Scanner(System.in);
	
	[B]Circle myCircle = new Circle();[/B]
	
	System.out.println("Please enter the circle's radius.");
	double radius = keyboard.nextDouble();
	myCircle.getArea(); //Calls for the circles area
	System.out.println("The circle's area is:" + " " + myCircle.getArea()); //Displays the circles area
	
	myCircle.getDiameter(); //Calls for the circles diameter
	System.out.println("The circle's diameter is:" + " " + myCircle.getDiameter()); //Displays the circles diameter
	
	myCircle.getCircumference(); //Calls for the circles circumference
	System.out.println("The circle's area is:" + " " + myCircle.getCircumference()); //Displays the circles circumference
}

	

	
	
	}

You need to provide a default constructor for the `Circle' class. Also, since `main' method is `static', you can't access instance variables inside it. It wouldn't make sense to access something which pertains to the state of a `Circle' inside a method which can be invoked without a `Circle' instance. Also consider using Math.PI instead of rolling in your own unless you have a good reason to do so.

These calculation need to go inside the methods:

//The getArea method returns the value that is stored in the area field
	double area = (PI * radius * radius); //Formula to get the area of the circle
	public double getArea()
	{
		return area;
	}
	//The getDiameter method returns the value that is stored in the diameter field
	double diameter = (radius * 2); //Formula to get the diameter of the circle
	public double getDiameter()
	{
		return diameter;
	}
	//The getCircumference method returns the value that is stored in the circumference field
	double circumference = (2 * PI * radius); //Formula to get the circumference of the circle
	public double getCircumference()
	{
		return circumference;
	}

Try it this way:

public double getArea()
	{
		return (PI * radius * radius);
	}
	
        public double getDiameter()
	{
		return (radius * 2);
	}
	
        public double getCircumference()
	{
		return (2 * PI * radius);
	}

With the way you had it the variables were initialized when the object was created but then if you changed the radius, these values (area, diameter, ...) weren't changed.

You need to provide a default constructor for the `Circle' class.

~s.o.s~ mentioned the biggest problem you had there. Propably your instructor told you, that during compilation there's automatically a default-constructor created, so you don't need to write it in your code to use. actually, this is true, BUT only if there is no constructor at all present.

if you have another constructor, like the one you have taking a double as argument, you either have to pass a double while calling the constructor, or write a second constructor (the default one)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.