| | |
hey guys, just need a quick tip on something!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 9
Reputation:
Solved Threads: 0
Hi all, I have an assignment to hand in tomorrow for programming and would appreciate any help that is given. I have done a couple of years programming with VB but java is like on a completely level compared to it.
Ok, so basically I have to program an application that will have a constructor to create a circle with a unit radius. The method will then return
- The radius
- The area
- The circumference
- The total area of the object and another circle
- The total circumference of the object and another circle
I’ve practically finished with the program but keeps giving me 1 error at the end that I can’t seem to solve.
My code for the method is here:
Ok, so basically I have to program an application that will have a constructor to create a circle with a unit radius. The method will then return
- The radius
- The area
- The circumference
- The total area of the object and another circle
- The total circumference of the object and another circle
I’ve practically finished with the program but keeps giving me 1 error at the end that I can’t seem to solve.
My code for the method is here:
java Syntax (Toggle Plain Text)
//Circle //An application that can create a circle using the radius given from the user and to calculate the //area, circumference of it and to display it to the user. import java.io.*; public class Circle { public int radiusInt; public String radiusStringInput; public void main(String[] args) throws IOException { // Create BufferedReader instance to enable input from keyboard InputStreamReader input = new InputStreamReader(System.in); BufferedReader keyboardInput = new BufferedReader(input); System.out.println("please enter the radius:"); radiusStringInput = keyboardInput.readLine(); radiusInt = Integer.parseInt(radiusStringInput); } public Circle(int x, int y,int r){ r = radiusInt; x = radiusInt *2; y= radiusInt * 2; } public int radiusInt(){ radiusInt = radiusInt; return (radiusInt); } public Double getCircumference(){ Double Circ = 2 * 3.14 * radiusInt; return (Circ); } public Double getArea(){ double Area = 3.14 * radiusInt * radiusInt; return (Area); } } [B]My code for the constructor is here:[/B] import java.io.*; public class CircleApp { public static void main(String[] args) { Circle Circle = new Circle(Circle.radiusInt); System.out.println("Circumference is " + Circle.getCircumference()); System.out.println("The Area is:" + Circle.getArea()); System.out.println("The Radius is:" + Circle.radiusInt); int radiusB = 5; Circle CircleB = new Circle(radiusB); double circleTotalArea = 3.14* radiusB * radiusB; double circleTotalCircumference = 2 * 3.14 * radiusB; System.out.println("Circumference is " + Circle.getCircumference()); System.out.println("The Area is:" + Circle.getArea()); System.out.println("The Radius is:" + Circle.radiusInt); System.out.println("The Total Area is:" + circleTotalArea); System.out.println("The Circumference is:" + circleTotalCircumference); } }
Last edited by cscgal; Nov 5th, 2008 at 7:32 pm. Reason: Added code tags
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
please put code in code tags, and what is the error?
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Dec 2007
Posts: 9
Reputation:
Solved Threads: 0
sorry my bad, its this part here from top of the constructor code
public class CircleApp {
public static void main(String[] args) {
Circle Circle = new Circle (Circle.radiusInt);
Error is: cannot find symbol
System.out.println("Circumference is " + Circle.getCircumference());
System.out.println("The Area is:" + Circle.getArea());
System.out.println("The Radius is:" + Circle.radiusInt);
I've added and edited it so many times just can't seem to get this one right. it needs to take the value of the radius to create a circle and as far as i know this is the method of doing it.
public class CircleApp {
public static void main(String[] args) {
Circle Circle = new Circle (Circle.radiusInt);
Error is: cannot find symbol
System.out.println("Circumference is " + Circle.getCircumference());
System.out.println("The Area is:" + Circle.getArea());
System.out.println("The Radius is:" + Circle.radiusInt);
I've added and edited it so many times just can't seem to get this one right. it needs to take the value of the radius to create a circle and as far as i know this is the method of doing it.
Last edited by AceAtch; Nov 5th, 2008 at 4:17 pm.
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
then just as ezzaral stated its the radiusint getter
also i would suggest you to name it as
just as your other ones have been named
also i would suggest you to name it as
Java Syntax (Toggle Plain Text)
public int getRadiusInt()
just as your other ones have been named
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
What they suggested above isn't your problem (well, it is a problem, but its not your error). Your problem is that you can't name your variable with the class name. Circle Circle is invalid because Circle is the class name. The word after the first Circle should be your variable name, which cannot be the name of a class or a reserved word (such as int). For example, you could use
Circle myCircle = new Circle();
However, you should also consider the fact that your code is confusing. You have a variable named radiusInt and you also have a method named radiusInt(). This won't confuse the compiler, since the compiler knows methods are called with parentheses, but it will confuse people. Method names should be descriptive, for example, a method that returns your radiusInt should be called getRadiusInt, like the guy above me said.
Circle myCircle = new Circle();
However, you should also consider the fact that your code is confusing. You have a variable named radiusInt and you also have a method named radiusInt(). This won't confuse the compiler, since the compiler knows methods are called with parentheses, but it will confuse people. Method names should be descriptive, for example, a method that returns your radiusInt should be called getRadiusInt, like the guy above me said.
Last edited by BestJewSinceJC; Nov 5th, 2008 at 4:23 pm.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
I didn't even notice that, good catch Ezzaral. Also, AceAtch, take a look at the comments I put for your constructor.
I don't think you're understanding the basic idea of how things work. I'll write something real quick to try to help you out. Then, you can try to apply the ideas I give you to your own code.
Java Syntax (Toggle Plain Text)
public Circle(int x, int y,int r){ r = radiusInt; //This makes r have the value that was in radiusInt x = radiusInt *2; //this makes x have 2 * the value that was in radiusInt y= radiusInt * 2; //this makes y have 2 * the value that was in radiusInt //You probably meant to do this: radiusInt = r; //The other code you have doesn't make any sense, since 'Circle' doesn't have any //variables other than radiusInt }
I don't think you're understanding the basic idea of how things work. I'll write something real quick to try to help you out. Then, you can try to apply the ideas I give you to your own code.
Last edited by BestJewSinceJC; Nov 5th, 2008 at 4:29 pm.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
java Syntax (Toggle Plain Text)
public class Rectangle{ private int height; private int width; public static void main(String[] args){ Rectangle rect = new Rectangle(1, 5); System.out.println(rect.getWidth()); // prints 5 rect.setWidth(3); System.out.println(rect.getWidth()); //prints 3 } public int getWidth(){ return width; } public int getHeight(){ return height; } public void setWidth(int wdth){ this.width = wdth; } //Calling this method creates a new Rectangle object, //which will have its height and width set to the h and w //you pass in. public Rectangle(int h, int w){ height = h; width = w; } /**Note, this method has the SAME method header as the method below *and the compiler will NOT let you declare the same method twice. *However, this method does the exact same thing as the one above. *The reason? Because when you want to create an object, you *use a constructor (like this one). Where I have this.height, *saying 'this' means I am referring to the height variable of *the object I am creating, NOT the height that got passed in (the one in parens). */ public Rectangle(int height, int width){ this.height = height; this.width = width; } }
Last edited by BestJewSinceJC; Nov 5th, 2008 at 4:39 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: Help me!
- Next Thread: inter JVM method calls
Views: 1573 | Replies: 29
| Thread Tools | Search this Thread |
Tag cloud for Java
6 android api apple applet application arc arguments array arrays automation binary bluetooth bold c++ chat class classes client code compare component coordinates database datagram doctype draw eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect ip j2me java javaexcel javaprojects jmf jni jpanel jtextarea julia linux list loop map method methods mobile netbeans newbie nextline number object oracle print problem program programming project recursion recursive scanner screen sell server set size sms socket sort sql string swing test threads time transfer tree user websites windows






