PDA

View Full Version : Need some help with a java program


JynxRD
11-05-2008, 09:32 PM
Hello everyone. I'm glad I found this forum. I'm doing one of those "Teach yourself Java" books. I do the practice practice programs at the end of each chapter. I've been doing ok and this is the first time I'm looking for help. I really just need to see how it works so I can learn from the code and then try to do it from memory as well. I've tried it a few times and then just scrapped what I had. I'll type up the instructions here :


Create a class named Circle with fields for the radius, area, and diameter. Be sure to include the following:

* a default constructor that sets the radius to 1
* a method to set the value of the radius
* a method to get the value of the radius
* a method to get the value of the diameter
* a method to get the value of the area
* a method to compute the diameter of the circle
* a method to compute the area of the circle

Recall that the diameter of a circle is twice its radius and that the area of a circle is 3.14 times the square of its radius.

After creating this class, write a second class, TestCircle, that has a main() method in it. Have this program create a Circle object with radius 10. It should also create a second Circle object that uses the radius set by the default constructor. Once the objects have been created, write code to display the diameter and area of both circles in an attractive fashion.

Although the output from your program is not required to look this way, it might look something like:

The Circle with radius 10 has diameter 20 and area 314.0.
The Circle with radius 1 has diameter 2 and area 3.14.


Thanks all , I really appreciate your help.

JynxRD
11-05-2008, 10:41 PM
This is what I have done so far.

Circle.java


public class Circle
{
// default constructor
private int radius;

double diameter = 2 * radius;
double area = Math.PI * radius * radius;



//Create a circle with a radius of 1.0

public double getRadius() {
return radius;
}
public void setRadius(double radius) {
radius = radius;
}
public double getDiameter() {
return diameter;
}
public void setDiameter(double diameter) {
diameter = diameter;
}
public double getArea() {
return area;
}
public void setArea(double area) {
area = area;
}
public double ComputeDiameter(){
return diameter;
}
public double ComputeArea(){
return area;
}
}



CircleTest.java


public class CircleTest
{
public static void main(String[] args)
{
Circle myCircle = new Circle(10.0);
Circle yourCircle = new Circle();
double radius;
double diameter;
double area;
radius = myCircle.getRadius();
diameter = myCircle.getDiameter();
area = myCircle.getArea();
radius = yourCircle.getRadius();
diameter = yourCircle.getDiameter();
area = yourCircle.getArea();
System.out.println("The area of the circle of radius " + radius + "is " + area + "with a diameter of " + diameter);

System.out.println("The area of the circle of radius " + radius + "is " + area + "with a diameter of " + diameter);
}
}


CircleTest.java will not compile. I get this error :


CircleTest.java:5: cannot find symbol
symbol : constructor Circle(double)
location: class Circle
Circle myCircle = new Circle(10.0);
^
1 error

brad211987
11-06-2008, 02:38 AM
2 problems that I see:

Your Circle class does not have a constructor(this is causing your compile error). You need a constructor defined as:


public Circle(Double radius)
{
//set radius here
}



The other problem is in your circle class. When you create the diameter and area variables, you shouldn't use the calculations there, instead you should do the calculations in a method so that the calculations are done when they are needed. Keep posting your code as you work on it and let me know if you have any more problems.

Gox
11-06-2008, 02:45 AM
Your compilation error is due to the fact that you don't have a Constructor in your circle class that matches that syntax.

So you'll need to add it

public Circle (double r){
//set radius to r
}

Also, according to your instructions your default constructor is suppose to set the radius to 1. But you aren't doing this, so you'll need to add it as well.

//Default constructor that sets radius to 1
public Circle(){
//Set radius to 1
}

Another note is that you'll probably want to compute the diameter and area of the circle within your getDiameter() and getArea() methods. The way you have it setup, you try to compute the diameter and area when the Circle object is constructed. But you give the user methods to change the radius of the circle, and if the user does this you're not recomputing the Diameter and Area and so you're values for these will be incorrect.

I've rearranged your code below to show what I mean.

public class Circle
{

private int radius;

//Default constructor that sets radius to 1
public Circle(){
//Set radius to 1
}

//Create a circle with a given radius
public Circle (double r){
//set radius to r
}

public double getRadius() {
return radius;
}

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

public double getDiameter() {
//compute and return diameter;
}

public double ComputeDiameter(){
//compute and return diameter;
}

public double getArea() {
//compute and return area
}

public double ComputeArea(){
//compute and return area
}
}
You'll notice that I took out some of your methods as they weren't listed in the methods list you provided.
Also, you'll notice that I've reduced your variable list to just "radius" and use this variable to compute diameter and area on the fly.
Unless I missed something I feel the ComputeArea and ComputeDiameter methods are redundant as I think the getArea and getDiameter methods should suffice.

Regardless of my personal opinions, this code should at least show you why your code wouldn't compile. I've provided a fix for your constructors and from there you should make your own decisions on methods and implementations.