Results 1 to 7 of 7
  1. #1
    fluteattack is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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);
    	}
    }

  2. #2
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    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 07:54 PM.

  3. #3
    fluteattack is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks.. the errors are on line 22 though

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fluteattack View Post
    Thanks.. the errors are on line 22 though
    You might want to tell us which line is 22.

  5. #5
    fluteattack is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    22: setRadius(double newRadius);

    In the first method

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by fluteattack View Post
    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?

  7. #7
    SHE
    SHE is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

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

    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();
            
        }
    try this one it work 100 %
    I was wonder why you put PI as private ?

Similar Threads

  1. Help in object oriented programming .
    By renu in forum New To Java
    Replies: 18
    Last Post: 10-26-2010, 08:57 PM
  2. is java is pure object oriented program?
    By 435.mahesh in forum New To Java
    Replies: 1
    Last Post: 04-21-2009, 04:42 PM
  3. Object Oriented
    By GraemeH in forum New To Java
    Replies: 3
    Last Post: 03-29-2009, 08:50 PM
  4. Object- Oriented guide?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-18-2009, 04:33 AM
  5. Is JAVA completely object-oriented???
    By venkateshk in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •