We are learning how to use multiple classes in Java
now, and there is a project asking about creating a class Circle
which will contain a radius
and a diameter
, then reference it from a main class to find the diameter. This code continues to receive an error (mentioned in the title)
public class Circle
{
public CircleR(double r)
{
radius = r;
}
public diameter()
{
double d = radius * 2;
return d;
}
}
Thanks for any help, -AJ
Update 1:
Okay, but I shouldn't have to declare the third line public CircleR(double r)
as a double, right? In the book I am learning from, the example doesn't do that.
public class Circle
{
//This part is called the constructor and lets us specify the radius of a
//particular circle.
public Circle(double r)
{
radius = r;
}
//This is a method. It performs some action (in this case it calculates the
//area of the circle and returns it.
public double area( ) //area method
{
double a = Math.PI * radius * radius;
return a;
}
public double circumference( ) //circumference method
{
double c = 2 * Math.PI * radius;
return c;
}
public double radius; //This is a State Variable…also called Instance
//Field and Data Member. It is available to code
// in ALL the methods in this class.
}
As you can see, the code public Circle(double r)....
how is that different from what I did in mine with public CircleR(double r)
? For whatever reason, no error is given in the code from the book, however mine says there is an error there.
If you observe the code, then you will find that the return type of the method printName() is missing. Hence, the compilation error. The above error can be fixed by adding the return type in the method signature. Since method printName() does not return anything, hence, its return type is void.
Every method in Java is declared with a return type and it is mandatory for all java methods. A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing). The type of data returned by a method must be compatible with the return type specified by the method.
As you can see, the code public Circle(double r).... how is that different from what I did in mine with public CircleR(double r)? For whatever reason, no error is given in the code from the book, however mine says there is an error there.
When defining constructors of a class, they should have the same name as its class. Thus the following code
public class Circle
{
//This part is called the constructor and lets us specify the radius of a
//particular circle.
public Circle(double r)
{
radius = r;
}
....
}
is correct while your code
public class Circle
{
private double radius;
public CircleR(double r)
{
radius = r;
}
public diameter()
{
double d = radius * 2;
return d;
}
}
is wrong because your constructor has different name from its class. You could either follow the same code from the book and change your constructor from
public CircleR(double r)
to
public Circle(double r)
or (if you really wanted to name your constructor as CircleR) rename your class to CircleR.
So your new class should be
public class CircleR
{
private double radius;
public CircleR(double r)
{
radius = r;
}
public double diameter()
{
double d = radius * 2;
return d;
}
}
I also added the return type double in your method as pointed out by Froyo and John B.
Refer to this article about constructors.
HTH.
Every method (other than a constructor) must have a return type.
public double diameter(){...
You forgot to declare double as a return type
public double diameter()
{
double d = radius * 2;
return d;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With