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

S

Sphenxes

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.
 
C

Chris Smith

Sphenxes said:
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
 
S

Sphenxes

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top