I need help to create a class named rectangle that contains
-fields for length and width
-setters and getters for the fields
-a constructor that accepts length and width as parameters
-an overloaded 0-arg constructor that sets field values to 0
-a method named computeArea that accepts no parameters and returns the area

This is what I had so far:
Rectangle.java

public class Rectangle {

  private double length;
  private double width;

  public void getLength(){
    return length;
  }

  public void getWidth(){
    return width;
  }

  public void setLength(double newLength){
    this.length = newLength;
  }

  public void setWidth(double newWidth){
    this.width = newWidth;
  }

  public Rectangle(double length, double width){
    this.length = length;
    this.width = width;
  }


  public Rectangle(){
    length = 0;
    width = 0;
  }

  double area = length * width;

  public double computeArea(){
    return area;
  }
}

TestRectangle.java

public class TestRectangle{
  public static void main(String[] arg){

    Rectangle rect1 = new Rectangle();

    rect1.setLength = 4.0;
    rect1.setWidth = 5.0;

    System.out.println("Length: " + rect1.getLength());
  }
}

setLength is method, use parentheses() for input values

    /*
    rect1.setLength = 4.0;
    rect1.setWidth  = 5.0;
    */
    rect1.setLength( 4.0);
    rect1.setWidth(  5.0);

getters shouldn't return void type

// public void getLength() {
public double getLength() {
  return length;
}
// public void getWidth() {
public double getWidth() {
  return width;
}

You’re on the right track!

The overloaded 0-arg constructor should set this.length and this.width, not just length and width as you have it. We are setting the object’s properties when we create a new Rectangle object, as so:

  public Rectangle(){
    this.length = 0;
    this.width = 0;
  }

The area variable should be calculated within the function to get an area. Where you have it, above computeArea, is inappropriate. Instead, it should be as so, calculating the area of the object’s length and width:

  public double computeArea(){
    double area = this.length * this.width;
    return area;
  }

As pointed out by zemiak, the purpose of getters is to return values, so they shouldn’t have a void return type. In this case, you also want them to be retrieving the object’s values:

  public double getLength(){
    return this.length;
  }

  public double getWidth(){
    return this.width;
  }

Those changes should do the trick!

Oh, and then, as zemiak proposed above:

rect1.setLength(4.0);
rect1.setWidth(5.0);

As you can see, we are calling the setLength and setWidth functions and passing in those double values. Once passed into the function, they are assigned to the object’s respective properties.

this.length vs length
Dani: you only need this when you want to refer to the instance variable and its name has been hidden by a parameter of the same name. The two-args constructor is the classic example.
Every other use of this in your code is redundant, and stylistically very odd.
OP’s 0 args constructor is correct as written.

The computeArea doesn’t need the area variable. All it needs is
return length*width;

It’s worth explaining why the area must be computed in the getter and not earlier: just think what happens when you change the length or width.

Ps; current Java thinking would be to make this class immutable (thread safety etc) and create a new instance rather than mutating. It’s also missing toString, hashCode, equals. Declaring it as a record rather than an ordinary class gives you all those for free.

this.length vs length
Dani: you only need this when you want to refer to the instance variable and its name has been hidden by a parameter of the same name. The two-args constructor is the classic example.
Every other use of this in your code is redundant, and stylistically very odd.

Thank you for letting me know! I haven’t looked at Java code in 20+ years, and frankly only took one college class with it.

I know that you say it’s stylistically very old, but couldn’t one argue that, as a beginner student, it’s overly redundant such that the reader had a clearer understanding of what is happening?

The computeArea doesn’t need the area variable. All it needs is
return length*width;

True, but I wanted to present working code with as little changes to what they had as possible, so it would make the most sense to them as to following their logic of what was going on.

Ps; current Java thinking would be to make this class immutable (thread safety etc) and create a new instance rather than mutating. It’s also missing toString, hash, compare. Declaring it as a record rather than an ordinary class gives you all those for free.

My guess is that these ideas are all far beyond the scope of what seems to be an introduction to Java class.

I know that you say it’s stylistically very old, but couldn’t one argue that, as a beginner student, it’s overly redundant such that the reader had a clearer understanding of what is happening?

Sorry for my confusing sentence. It’s late and I’m very tired. What I’m trying to say is that I think being overly redundant is a good thing when you’re first learning. Take shortcuts only once you understand and have a complete grasp over why you’re doing what you’re doing.

It’s unclear if the student who asked this question understands the difference between this.length and length, and when to use each. For example, why do you suspect he used this.length in setLength but not in getLength?

You almost never see a redundant this. in standard Java. A programmer seeing it would immediately assume that there was something unusual going on that the writer wanted to draw his attention to.
Unnecessary use of this. is confusing for beginners. It’s redundant in the same toxic way that
<some Boolean expression>==true
is redundant. (Another common beginner mistake that follows from a fundamental misunderstanding of the if statement.)

I suspect OP has spurious this. because he is unclear on this simple rule:
in Java you refer to instance variables by their simple unqualified name (unless it’s necessary to do otherwise).
It’s not a shortcut, it’s the language. Adding unnecessary qualifiers is confusing and bad practice.

I said shortcut but I meant shorthand.

If it’s not shorthand, but rather the way the language is designed to be written, that’s another story.

As you know, I write php, not Java. I try my best to avoid php shorthand that I would consider the “proper” way of doing things when working with newbies in the forums. For example, I would myself write <?= “hello world” ?> but I found myself writing out <?php echo “hello world”; ?> which is something a php programmer would never, ever, ever write, but is much clearer for new programmers to grasp what is going on. It also doesn’t break the simple heuristics they are practicing: semi-colon at the end of every statement, etc. Classroom code just tends to not be written the same as code you would find out in the wild, and that’s okay.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.