Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 4 of 4

11052008, 09:32 PM #1
 Join Date
 Nov 2008
 Posts
 15
 Thanks
 0
 Thanked 0 Times in 0 Posts
Need some help with a java program
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 :
Code: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.
11052008, 10:41 PM
#2
 Join Date
 Nov 2008
 Posts
 15
 Thanks
 0
 Thanked 0 Times in 0 Posts
This is what I have done so far.
Circle.java
Code: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
CircleTest.java will not compile. I get this error :Code: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:5: cannot find symbol
symbol : constructor Circle(double)
location: class Circle
Circle myCircle = new Circle(10.0);
^
1 error
11062008, 02:38 AM
#3
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:
Code: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.
11062008, 02:45 AM
#4
 Join Date
 May 2006
 Location
 Ontario, Canada
 Posts
 392
 Thanks
 2
 Thanked 20 Times in 20 Posts
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
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.Code:public Circle (double r){ //set radius to r }
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.Code://Default constructor that sets radius to 1 public Circle(){ //Set radius to 1 }
I've rearranged your code below to show what I mean.
You'll notice that I took out some of your methods as they weren't listed in the methods list you provided.Code: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 } }
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.
Last edited by Gox; 11062008 at 04:02 AM.