1

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;
 }
}            
9
  • why do you post incomplete code? This doesn't even remotely compile. Commented Nov 26, 2015 at 17:16
  • @wero but it does, I'm just missing one return statement Commented Nov 26, 2015 at 17:18
  • For example it is missing class name definition... Commented Nov 26, 2015 at 17:18
  • 1
    If its missing a return statement then it does not, in fact, compile. Commented Nov 26, 2015 at 17:19
  • 1
    @PetterFriberg exactly, and I bet that it is not "class SimpleRectangle" which explains the error Commented Nov 26, 2015 at 17:19

5 Answers 5

2

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"

Sign up to request clarification or add additional context in comments.

6 Comments

don't make perimeter an attribute. you can calculate that.
If it's up to me I would make it a private final field along with all other fields and calculate it in the constructor. This way it's only calculated once and getPerimeter, that may get called many times, will just return it.
private final? what if the width changes and the height stays the same? don't create attributes that depend on each other!!!
I got the functional virus recently and I try to make all classes immutable. There are advantages to that. If there is good reason for this class to be mutable a developer has to consider what fields change more often and who makes the changes. Calculating the perimeter each time the getter is called is a legitimate option if all other fields change often.
we're talking about java here. remember that. java is not fast, java is not functional.
|
1

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()

Comments

1

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;
}

4 Comments

thanks, but there is still an error message for the return statement
error: invalid method declaration; return type required SimpleRectangle(){ ^ 1 error
In your method getPerimeter() you are using length instead of height.
@Patrick I already fixed all of that, just haven't updated it on the page yet
1

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());
    }

}

Comments

1

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());

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.