Reputation: 21
I am currently trying to create a class that prints out a rectangle with a height and width 1. I have the program set up (there is a template we are supposed to use) and I incorporated all of the steps. However there is one problem with the return statement on the line "SimpleRectangle(){" it says it is missing a return statement but no matter what I return it still comes up with an error.
public static void main (String[] args){
SimpleRectangle rectangle1=new SimpleRectangle();
System.out.println("The area of radius "+rectangle1.perimeter+" is "+rectangle1.getArea());
}
double height;
double width;
SimpleRectangle(){
height=1;
width=1;
}
double getArea(){
return height*width;
}
double getPerimeter(){
return length+length+width+width;
}
}
Upvotes: 1
Views: 111
Reputation: 22
In order to use rectangle1.getPerimeter()
or rectangle1.getArea()
, you need to create a class that looks something like this:
public class SimpleRectangle {
double height;
double width;
SimpleRectangle() {
height = 1;
width = 1;
}
double getArea() {
return height * width;
}
double getPerimeter() {
return 2 * (height + width);
}
}
Then you need to create the object (as shown below) before you can use rectangle1.getPerimeter()
:
public class MainClass {
public static void main (String[] args) {
SimpleRectangle rectangle1 = new SimpleRectangle();
System.out.println("The area of radius " + rectangle1.getPerimeter() + " is " + rectangle1.getArea());
}
}
Upvotes: 1
Reputation: 21710
You have several issues with your code it is not compiling
The current error is related to missing class definition, but there will be others.......
Try to not copy and past but to understand what you where missing, class definition, no field declaration for length, wrong call to method ecc.
I have included some public and private declaration I suggest that you study some also what this means...
AND NR 1 TRY TO USE AND IDE AS ECLIPSE, THIS WILL HELP YOU ENORMOUSLY AVOIDING AL OF THESE PROBLEMS AND WHEN YOU LEARN TO DEBUG YOU BECOME A PROGRAMMER., no need for SO, for debugging problems
public class SimpleRectangle {
private double height;
private double width;
public SimpleRectangle() {
this.height = 1;
this.width = 1;
}
public double getArea() {
return height * width;
}
public double getPerimeter() {
return height + height + width + width;
}
public static void main(String[] args) {
SimpleRectangle rectangle1 = new SimpleRectangle();
System.out.println("The area of radius " + rectangle1.getPerimeter() + " is " + rectangle1.getArea());
}
}
Upvotes: 1
Reputation: 22244
This looks like a constructor for a class called SimpleRectange
SimpleRectangle(){
height=1;
width=1;
}
In the code you provide there doesn't seem to be such a class. Make sure your code is included in a class with that name and that it has all the fields you are accessing e.g.
public class SimpleRectangle {
double height;
double width;
double perimeter;
double length;
public static void main(String[] args) {
...
...
}
If your code is in a class with any other name you will get "Invalid method declaration. Return type required"
Upvotes: 2
Reputation: 10249
rectangle1.perimeter
should be rectangle1.getPerimeter()
also you dont have a field called length. it's called height
double getPerimeter(){
return height+height+width+width;
}
the consturctor need to be public
public SimpleRectangle(){
height=1;
width=1;
}
Upvotes: 1
Reputation: 48297
rectangle1.perimeter
is not working because there is no field defined with that name, instead you have a method , therefore you need to call it
this is wrong, you need to do rectangle1.getPerimeter()
Upvotes: 1