I'm not sure where I'm going wrong in this program. I needed to create two different classes, one being the driver class the other the utility class. The purpose is to ask the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference. Please help, here is what I have so far.

public class Circle

{
	private double Radius;
	final double PI = 3.14159;
	double number;
	
	public double Circle(double Rad)
	{
		Radius = number;
		return Radius;
	}		
	/**
		The getArea method returns the area of the 
		circle
	*/
	
	public double getArea()
	{
		return PI*Radius*Radius;
	}
	
	/**
		The getDiameter method returns the diameter
		of the circle
	*/
	
	public double getDiameter()
	{
		return Radius*2;
	}
	
	/**
		The getCircumference method returns the 
		circumference of the circle
	*/
	
	public double getCircumference()
	{
		return 2*PI*Radius;
	}
	
}

That is one here is the other

public class CircleDemo
{
	public static void main(String[] args)
	{
		String input;		//Hold users input
		double Radius;		//hold the radius
		double Area;
		double Circumference;
		double Diameter;
		double number;
		
		//Create the radius object
		Circle radius = new Circle();
		
		//Get the radius of the circle
		input = JOptionPane.showInputDialog("What is the Radius " + 
														"of the circle?");
														
		number = Double.parseDouble(input);
		Circle.setRadius(number);
		
		//Calculate the Area of the circle
		Area = Radius.getArea();
		
		//Display the area of the circle
		JOptionPane.showMessageDialog(null, "The area of the circle " +
												"is " + Area);
		
		//Calculate the Diameter of the circle
		Diameter = Radius.getDiameter();
		
		//Display the diameter of the circle
		JOptionPane.showMessageDialog(null, "The diameter of the circle " +
												"is " + Diameter);
		
		//Calculate the Circumference of the circle
		Circumference = Radius.getCircumference();
		
		//Display the circumference of the circle
		JOptionPane.showMessageDialog(null, "The circumference of the circle " +
												"is " + Circumference);
												
		System.exit(0);
	}
}

Recommended Answers

All 3 Replies

Java is case sensitive. When you declare an instance like this:
Circle radius = new Circle(),
this would be wrong: Area = Radius.getArea().
This is correct: Area = radius.getArea()


Also you call a method:
Circle.setRadius(number) and that method doesn't exist. On top of that if it did exist you should declare it non static and call it: radius.setRadius(number)


You declare a constructor: public double Circle(double Rad) . Classes may have the default constructor so if you declare none you can use the:
Circle radius = new Circle() as you do in the code, bit since you declare a constructor you must use only that: public double Circle(double Rad)
or have 2 constructors


And lastly the constructor shouldn't return anything. This is wrong:

public double Circle(double Rad)
	{
		Radius = number;
		return Radius;
	}

Remove the return

commented: This is for faste answer +10

LOL, just seen I was beaten for the fastest answer :twisted:

Sorted that few errors in your code. Please make your self familiar with Java naming standards

public class Circle

{
	private double radius;
	final double PI = 3.14159;
	double number;
	
	//Missing setter for radius variable
	public void setRadius(double r)
	{
		radius = r;
	}
	
		
	/**
		The getArea method returns the area of the 
		circle
	*/
	
	public double getArea()
	{
		return PI*radius*radius;
	}
	
	/**
		The getDiameter method returns the diameter
		of the circle
	*/
	
	public double getDiameter()
	{
		return radius*2;
	}
	
	/**
		The getCircumference method returns the 
		circumference of the circle
	*/
	
	public double getCircumference()
	{
		return 2*PI*radius;
	}
	
}
import javax.swing.JOptionPane;

public class CircleDemo
{
	public static void main(String[] args)
	{
		String input;		//Hold users input
		double radius;		//hold the radius
		double area;
		double circumference;
		double diameter;
		double number;
		
		//Create the radius object
		Circle circleCal = new Circle();
		
		//Get the radius of the circle
		input = JOptionPane.showInputDialog("What is the Radius " + 
														"of the circle?");
														
		number = Double.parseDouble(input);
		circleCal.setRadius(number);
		
		//Calculate the Area of the circle
		area = circleCal.getArea();
		
		//Display the area of the circle
		JOptionPane.showMessageDialog(null, "The area of the circle " +
												"is " + area);
		
		//Calculate the Diameter of the circle
		diameter = circleCal.getDiameter();
		
		//Display the diameter of the circle
		JOptionPane.showMessageDialog(null, "The diameter of the circle " +
												"is " + diameter);
		
		//Calculate the Circumference of the circle
		circumference = circleCal.getCircumference();
		
		//Display the circumference of the circle
		JOptionPane.showMessageDialog(null, "The circumference of the circle " +
												"is " + circumference);
												
		System.exit(0);
	}
}

Thank you for the help. Sorry I am not as familiar with the syntax as I need to be, but I have just gotten into Java and it is a bit confusing.
Thanks again

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.