Results 1 to 7 of 7
Thread: New to object-oriented
- 03-26-2011, 05:08 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
New to object-oriented
In class, we're moving from procedural programming to object-oriented. Here's the assignment I'm working on. I won't be surprised if all of this is wrong (I don't have a great handle on it yet), but I keep getting the same two errors: ".class expected" and "; expected".
Java Code:// ----------------------------------------------------------------- // This program prompts the user to enter a circle's radius, and // calculates and displays the circle's area, diameter and circumfrence. // ----------------------------------------------------------------- public class Circle { private double radius; //radius private final double PI = 3.14159; //Pi //**************************************************** //Accepts the radius of the circle as an argument public Circle(double newRadius) { setRadius(double newRadius); }//end Constructor //**************************************************** //A mutator method for the radius field public void setRadius(double newRadius) { this.radius = radius; }//end setRadius //**************************************************** //An accessor method for the radius field public double getRadius() { return this.radius; }//end getRadius //**************************************************** //Returns the area of the circle, calculated as pi*R^2, R is the radius public double getArea() { return PI*radius*radius; }//end getArea //**************************************************** //Returns the diameter of the circle, calculated as 2R, R is the radius public double getDiameter() { return 2*radius; }//end getDiameter //**************************************************** //Returns the diameter of the circle, calculated as 2piR, R is the radius public double getCircumfrence() { return 2*PI*radius; }//end getCircumfrence } // end class Circle public class CircleRun { public static void main(String args[]) { System.out.println("Please enter the radius of a circle: "); radius = in.nextDouble(); Circle cir = new Circle(); System.out.println("Area is: " + cir.getArea); System.out.println("Diameter is: " + cir.getDiameter); System.out.println("Circumfrence is: " + cir.getCircumfrence); } }
- 03-26-2011, 05:38 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
public Circle(double newRadius) --
Circle cir = new Circle(); --> why you do not insert the parameter !
Java Code:System.out.println("Please enter the radius of a circle: "); double [B][COLOR="YellowGreen"]radius [/COLOR][/B]= in.nextDouble(); Circle cir = new Circle([B][COLOR="YellowGreen"]radius [/COLOR][/B]);Last edited by SHE; 03-26-2011 at 06:54 PM.
- 03-26-2011, 06:19 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Thanks.. the errors are on line 22 though
-
- 03-26-2011, 06:36 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
22: setRadius(double newRadius);
In the first method
-
You're confusing calling a method with defining a method. When you create a method, you need to declare the variable types of the parameters, but when you call the same method, you don't declare the variable types, but rather you simply pass in the correct type of variable.
For example, you would define the method foo like so:
Java Code:// here we have to declare the parameter as int type public void foo(int value) { System.out.println("value is: " + value); }
But when you call foo, you don't declare the parameter type:
Java Code:int i = 10; // foo(int i); // incorrect foo(i); //correct
Make sense?
- 03-28-2011, 01:43 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Java Code:public class Circle { private double redius ; public final double PI = 3.14159; public Circle(double redius) { this.redius = redius; } public double getRedius() { return redius; } public void setRedius(double redius) { this.redius = redius; } public double getArea() { System.out.println("your Area is "+PI*redius*redius); return PI*redius*redius; } public double getDimeter() { System.out.println("your Dimeter is "+2*redius); return 2*redius; } public double getCircumfrence() { System.out.println("your Circumfrence is "+2*PI*redius); return 2*PI*redius; } }
try this one it work 100 %Java Code:public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please Enter the redius to count the area"); double redius = input.nextDouble(); System.out.println("your redius is"+"["+redius+"]"); Circle crc1 = new Circle(redius); crc1.getArea(); crc1.getDimeter(); crc1.getCircumfrence(); }
I was wonder why you put PI as private ?
Similar Threads
-
Help in object oriented programming .
By renu in forum New To JavaReplies: 18Last Post: 10-26-2010, 07:57 PM -
is java is pure object oriented program?
By 435.mahesh in forum New To JavaReplies: 1Last Post: 04-21-2009, 03:42 PM -
Object Oriented
By GraemeH in forum New To JavaReplies: 3Last Post: 03-29-2009, 07:50 PM -
Object- Oriented guide?
By sciguy77 in forum New To JavaReplies: 4Last Post: 01-18-2009, 03:33 AM -
Is JAVA completely object-oriented???
By venkateshk in forum New To JavaReplies: 2Last Post: 11-17-2008, 09:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks