A newbie has a lot of errors with a simple Circle program

Discussion in 'Java' started by Sphenxes, Sep 25, 2003.

  1. Sphenxes

    Sphenxes Guest

    Hi Everyone

    I am reading OOP Problem Solving Java. In one of its excercises It was
    recommended to build a program to calculate the Area and Circumfernce of a
    circle. I do like to follow the example in the book and decided to do my own
    program. I got a lot of comilation errors. I do not know whether these
    errors are due to design failure or simple declaration errors.

    /**
    * The Circle Class program
    * Oject Oriented Programming
    * Java, Java, Java
    * p. 99
    */

    class TestCircle {

    Circle circleOne = new Circle();
    circleOne.area(10, 15);
    circleOne.circumference(10, 2);
    }


    public class Circle{
    public static void main (String arrgv[]) throws Exception{
    //private float diameter= 10;
    float area;
    float circumference;
    final double Pi=2.323;
    float r; //radius

    //r = diameter/2;


    double area(float r, double Pi){
    area = Pi*r*r;
    System.out.println("The area of the Circle := " + area);
    return;

    }

    double circumference(float r, double Pi){
    circumference = 2*Pi*r;
    System.out.println("The Ciruference of the Circle is := " +
    circumference);
    return;

    }
    }
    }


    Comilation Errors:
    Circle.java [11:1] <identifier> expected
    circleOne.area(10, 15);
    ^
    Circle.java [12:1] <identifier> expected
    circleOne.circumference(10, 2);
    ^
    Circle.java [27:1] ';' expected
    double area(float r, double Pi){
    ^
    Circle.java [11:1] package circleOne does not exist
    circleOne.area(10, 15);
    ^
    Circle.java [12:1] package circleOne does not exist
    circleOne.circumference(10, 2);
    ^
    Circle.java [27:1] area is already defined in main(java.lang.String[])
    double area(float r, double Pi){
    ^
    6 errors
    Errors compiling Circle.


    --
    Regards

    Hesham Marei
    ----
     
    Sphenxes, Sep 25, 2003
    #1
    1. Advertising

  2. Sphenxes

    Chris Smith Guest

    Sphenxes wrote:
    > Hi Everyone
    >
    > I am reading OOP Problem Solving Java. In one of its excercises It was
    > recommended to build a program to calculate the Area and Circumfernce of a
    > circle. I do like to follow the example in the book and decided to do my own
    > program. I got a lot of comilation errors. I do not know whether these
    > errors are due to design failure or simple declaration errors.


    The way Java works is this:

    1. You declare a class.

    2. Inside that class, you declare fields, methods, constructors, static
    and instance initializers, or nested classes and interfaces (for now,
    you can ignore everything but methods and fields).

    3. Inside those methods (or constructors, or static and instance
    initializers), you write procedural code.

    You have two problems. First, in your TestCircle class, you've written:

    > class TestCircle {
    >
    > Circle circleOne = new Circle();
    > circleOne.area(10, 15);
    > circleOne.circumference(10, 2);
    > }


    You did steps 1 and 3, but skipped step 2. You need to declare a method
    first, before you start writing code. If you want to be able to run
    this class, you should call the method main and use a signature that
    conforms to the expectations for a main method, so this would look like:

    class TestCircle
    {
    public static void main(String[] args)
    {
    Circle circleOne = new Circle();
    circleOne.area(10, 15);
    circleOne.circumference(10, 2);
    }
    }

    And then,

    > public class Circle{
    > public static void main (String arrgv[]) throws Exception{
    > //private float diameter= 10;
    > float area;
    > float circumference;
    > final double Pi=2.323;
    > float r; //radius
    >
    > //r = diameter/2;
    >
    >
    > double area(float r, double Pi){
    > area = Pi*r*r;
    > System.out.println("The area of the Circle := " + area);
    > return;
    >
    > }
    >
    > double circumference(float r, double Pi){
    > circumference = 2*Pi*r;
    > System.out.println("The Ciruference of the Circle is := " +
    > circumference);
    > return;
    >
    > }
    > }
    > }


    Here, you've done step #2 twice! You put a method in this class, but
    then you put another method inside the first method. That's not
    allowed. Actually, it looks like there's no purpose behind your "main"
    method that you added directly. You don't intend to run this class
    directly, but rather have it used by the TestCircle class, so this class
    doesn't need a main method. Just take that out. The variables you
    declare right after the main method will become fields, and then the
    other methods will be valid.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Sep 25, 2003
    #2
    1. Advertising

  3. Sphenxes

    Sphenxes Guest

    Hi Larry

    Thanks for your comments. But my main problem was not using Math.PI library
    which is still a little advance for me. The problem was I did not understand
    the compiling error message. This error could arise with any variable and I
    would not get it either.
    I will try my next program with the Math.PI and hope that I will get it
    compiled easily.

    Thank you both Chris and Larry your comments are very helpful that I can try
    to learn Java on my own and can find such a wondefull support online.

    --
    Regards

    Hesham Marei
    ----

    "Larry Coon" <> schrieb im Newsbeitrag
    news:...
    > Chris Smith wrote:
    >
    > > final float Pi = 3.141592f;
    > >
    > > Either of the above would work, but I'd favor the latter.

    >
    > In the case of Pi (and e), why declare a variable at all?
    > Math.PI is already defined.
    >
    >
    > Larry Coon
    > University of California
     
    Sphenxes, Sep 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Talon

    Circle Hell

    Talon, Sep 4, 2003, in forum: Perl
    Replies:
    2
    Views:
    1,122
    Mark Carter
    Sep 4, 2003
  2. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,027
    Mark Goldin
    Jan 17, 2004
  3. =?Utf-8?B?dGFnaHJlZWQ=?=

    moving label in c# around circle

    =?Utf-8?B?dGFnaHJlZWQ=?=, May 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,882
    clintonG
    May 3, 2004
  4. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    41
    Views:
    1,032
    Monique Y. Mudama
    Dec 20, 2005
  5. Found your site in google, and it has a lot of use

    Found your site in google, and it has a lot of usefullinformation. Thanx,some sites

    Found your site in google, and it has a lot of use, May 12, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    294
    Found your site in google, and it has a lot of use
    May 12, 2007
Loading...

Share This Page