Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Need some help with a java program

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-05-2008, 10:32 PM PM User | #1 JynxRD New Coder   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.``` Thanks all , I really appreciate your help.
11-05-2008, 11:41 PM   PM User | #2
JynxRD
New Coder

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

double diameter = 2 * radius;

//Create a circle with a radius of 1.0

}
}
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

Code:
```public class CircleTest
{
public static void main(String[] args)
{
Circle myCircle = new Circle(10.0);
Circle yourCircle = new Circle();
double diameter;
double area;
diameter = myCircle.getDiameter();
area = myCircle.getArea();
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 :

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

 11-06-2008, 03:38 AM PM User | #3 brad211987 Regular Coder     Join Date: Sep 2005 Location: Ohio Posts: 631 Thanks: 10 Thanked 50 Times in 50 Posts 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.
 11-06-2008, 03:45 AM PM User | #4 Gox Regular Coder     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 Code: ```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. Code: ``` //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. 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 } }``` 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. Last edited by Gox; 11-06-2008 at 05:02 AM..

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:48 AM.