Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A newbie has a lot of errors with a simple Circle program

Reply
Thread Tools

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

 
 
Sphenxes
Guest
Posts: n/a
 
      09-25-2003
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
----


 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      09-25-2003
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

 
Reply With Quote
 
 
 
 
Sphenxes
Guest
Posts: n/a
 
      09-25-2003
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" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
This book has helped me a lot Tim Chmielewski Digital Photography 0 06-28-2005 09:37 PM
John Henry has a lot of nerve trying to peddle spyware on Usenet mark_digital Digital Photography 65 07-08-2004 07:39 PM
Jisatsu circle (Suicide Circle) Col's Cavern DVD Video 1 06-07-2004 06:55 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments