Search code examples
javaclassmethodsconstructorinstance-variables

I need help creating a Rectangle class with private instance variables, but public methods and constructors


I'm new to using java and I need to make a class called Rectangle, but the code I'm using doesn't seem to work, and the notes for class aren't helpful. I feel like it has something to do with my methods because I don't understand those too well.

The class needs to follow these requirements: Create a class called rectangle with the following instance variables and public constructors:

  • width, double

  • height, double

  • a constructor with one parameter (double) called sideLength, which sets both the width and height to sideLength.

  • a constructor with two parameters width and height (double), which sets both the width and height to the corresponding values that are passed on.

  • a public method computeArea with no parameters that returns the area of Rectangle as a double

  • a public method computePerimeter with no parameters that returns the perimeter of Rectangle as a double

  • a public method getHeight with no parameters that returns the height of Rectangle as a double

  • a public method getWidth with no parameters that returns the width of Rectangle as a double

I don't need to make a main method because that was already provided.

This is the code that I'm using, but I keep getting an error that says height cannot be found from the computeArea method and down.

 class Rectangle{
     
     private static double width;
     private static double heigth;
     
     public Rectangle(double sideLength) {
         
         Rectangle width = new Rectangle(sideLength);
         Rectangle height = new Rectangle(sideLength);
     }
         
     public Rectangle(double width, double height){
         width = width;
         height = height;
     }    

     public double computeArea() {
         return height * width;
     }
         
     public double computePerimeter() {
         return 2 * (height + width);
     }
         
     public double getHeight() {
         return height;
     }
     
     public double getWidth() {
         return width;
     }
     
 }

Solution

  • Don't make the private variables static or they will be shared by all instances of Rectangle.

    class Rectangle{
         
         private double width;
         private double height;
         
         public Rectangle(double sideLength) {
             
             this.width = sideLength;
             this.height = sideLength;
         }
             
         public Rectangle(double width, double height){
             this.width = width;
             this.height = height;
         }    
    
         public double computeArea() {
             return height * width;
         }
             
         public double computePerimeter() {
             return 2 * (height + width);
         }
             
         public double getHeight() {
             return height;
         }
         
         public double getWidth() {
             return width;
         }
     }
    

    To use it in other code:

    public class Test {
      public static void main(String[] args) {
        Rectangle square = new Rectangle(1.0);
        double area = square.computeArea();
        System.out.println(area);
        Rectangle rectangle = new Rectangle(1.0, 2.0);
        area = rectangle.computeArea();
        System.out.println(area);
      }
    }