1

The following code is what I'm having issues understanding:

public class Rectangle {

    public Rectangle() {
        double width = 1;
        double height = 1;
    }

    public Rectangle(double w, double h) {
        double width = w;
        double height = h;
    }

    public double getArea(double w, double h) {
        return (w*h);
    }

    public double getPerimeter(double w, double h) {
        return ((2*w)+(2*h));
    }

    public static void main(String[] args) {
        Rectangle oldRectangle = new Rectangle(4, 40);
        Rectangle newRectangle = new Rectangle(3.5, 35.9);
        double height1 = oldRectangle.height;
        double height2 = newRectangle.height;
        double width1 = oldRectangle.width;
        double width2 = newRectangle.width;

        System.out.println("Width of Rectangle 1 is: " + 4);
        System.out.println("Height of Rectangle 1 is: " + 40);
        System.out.println("Area of Rectangle 1 is: " + oldRectangle.getArea(4, 40));
        System.out.println("Perimeter of Rectangle 1 is: " + oldRectangle.getPerimeter(4, 40));
        System.out.println("Width of Rectangle 1 is: " + 3.5);
        System.out.println("Height of Rectangle 1 is: " + 35.9);
        System.out.println("Area of Rectangle 1 is: " + newRectangle.getArea(3.5, 35.9));
        System.out.println("Perimeter of Rectangle 1 is: " + newRectangle.getPerimeter(3.5, 35.9));

    }

}

I was instructed to create two constructors for a Rectangle class—one with no arguments but assigned a default value of 1 for both variables width and height. The second constructor was to contain parameters that would take in two doubles that would get assigned to their appropriate variable.

I was then told to create two 'get()' methods that returned their respective values—in my case, they were perimeter and area of said Rectangle.

I was then instructed to create two Rectangle instances, one with a width of 4 and a height of 40—and another with a width of 4.5 and a height of 35.9. So, I did just that and made two new Rectangle objects, as you can see.

Lastly, I was instructed to print out the Width, Height, Perimeter, and Area of both Rectangle objects. My issue is that I don't know of a way to reference them. I took a beginners tutorial class for JavaScript and if I'm not mistaken, I recall there was a way to reference a property value of an object by assigning it to a variable. Again, I'm saying "If I'm not mistaken", so I could be wrong. It's been a while...

I do realize that Java and Java Script are entirely different things in their own right. Java Script was a scripting language developed and based off of Java.

Anyway, any help will be grand.

Please feel free to help me understand how I can implement what I'm trying to do by giving examples. You don't have use my exact code, but I'd like to be able to get my code to make more sense.. I'm using Eclipse btw.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78

3 Answers3

1

You are on the right track. What you are looking to do is something like this:

public double getArea(Rectangle r){
      return r.width*r.height;
}
public double getPerimeter(Rectangle r){
      return (2*r.width + 2*r.height);
}

For the print statements you are hard-coding in values which you dont have to do.

 System.out.println("Width of Rectangle 1 is: " + oldRectangle.width);
    System.out.println("Height of Rectangle 1 is: " + oldRectangle.height);
    System.out.println("Area of Rectangle 1 is: " + getArea(oldRectangle));
    System.out.println("Perimeter of Rectangle 1 is: " + getPerimeter(oldRectangle));
Kevin Mee
  • 539
  • 3
  • 14
  • It's **incorrect** to pass the object to the method this is **Object Oriented Programming** and not **Functional Programming** here, in OOP methods are applied to objects and they're not functions that expects objects. Please DON'T be Confused with [**Functional Programming**](http://fr.wikipedia.org/wiki/Functional_Programming). A **method** in object-oriented programming (OOP) is a procedure associated with an object class, take a look [**here**](http://en.wikipedia.org/wiki/Method_(computer_programming)) for further information. – cнŝdk Apr 02 '15 at 10:17
0

My issue is that I don't know of a way to reference them

As Java is a strong-typed language, to store the references to each Rectangle (or any object) attribute, you must create variables that match the field.

First, you need your rectangle have attributes:

public class Rectangle {
    public double width;
    public dobule height;

    // constructors here...

After, methods to calculate area and perimeter:

public double getArea() {
    return (width*height);
}

public double getPerimeter() {
    return ((2*width)+(2*height));
}

Now, to reference them (i guess you still mean area and perimeter):

double area1 = getArea(newRectangle);
double area2 = getArea(oldRectangle);

and

double perimeter1 = getPerimeter(newRectangle);
double perimeter2 = getPerimeter(oldRectangle);
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • I wasn't aware I could do this, so thank you @Jordi Castilla, however, how would I be able to reference the height and width properties of the objects I've created? – That Random Guy Apr 02 '15 at 03:31
  • simply with name of created object and `.propertyName` i.e: for a new `Rectangle rectangle = new Rectangle()` you can refer `height` with `rectangle.height` – Jordi Castilla Apr 02 '15 at 07:16
  • *in order to improve your code, methods to calculate area and perimeter **must** take a Rectangle object* !!! **No they don't**, It's unfortunately incorrect, in OOP methods are applied to objects and they're not functions that expects objects. Please DON'T be Confused with [**Functional Programming**](http://fr.wikipedia.org/wiki/Functional_Programming). A **method** in object-oriented programming (OOP) is a procedure associated with an object class, take a look [**here**](http://en.wikipedia.org/wiki/Method_(computer_programming)) for further information. – cнŝdk Apr 02 '15 at 10:05
0

There are no width and height properties in your class, they are only defined inside the constructor, declare them in the class and not inside the constructors so you can acces them like this:

public class Rectangle {

    double width;
    double height;

  public Rectangle() {
      width = 1;
      height = 1;
  }

public Rectangle(double w, double h) {
    width = w;
    height = h;
}

public double getArea(double w, double h) {
    return (w*h);
}

public double getPerimeter(double w, double h) {
    return ((2*w)+(2*h));
}
//....
}

Note: It's better to declare them as private and use getters and setters to acces them, take a look at Adding Setter and Getter Methods for further information.

EDIT:

In your case you don't need to pass parameters to your methods (because the calculation needs this rectangle width and height), just calculate them using your class fields, like this:

public double getArea() {
    return (width*height);
}

public double getPerimeter() {
    return ((2*width)+(2*height));
}

Here's what you need to do to acces your variables:

public static void main(String[] args) {
    Rectangle oldRectangle = new Rectangle(4, 40);
    Rectangle newRectangle = new Rectangle(3.5, 35.9);

    //In the following you access all the object variables and methods

    System.out.println("Width of Rectangle 1 is: " + oldRectangle.width);
    System.out.println("Height of Rectangle 1 is: " + oldRectangle.height);
    System.out.println("Area of Rectangle 1 is: " + oldRectangle.getArea());
    System.out.println("Perimeter of Rectangle 1 is: " + oldRectangle.getPerimeter());
    System.out.println("Width of Rectangle 1 is: " + newRectangle.width);
    System.out.println("Height of Rectangle 1 is: " + newRectangle.height);
    System.out.println("Area of Rectangle 1 is: " + newRectangle.getArea());
    System.out.println("Perimeter of Rectangle 1 is: " + newRectangle.getPerimeter());

}

You can test the DEMO here.

And to answer your question about using constructor, calling the parametrized constructor like this:

Rectangle oldRectangle = new Rectangle(4, 40);

Is equivalent to:

// create the object
Rectangle oldRectangle = new Rectangle(); 
//And then assigning the values 4 and 40 to its width and height    
oldRectangle.width=4; 
oldRectangle.height=40;
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Thanks for your input. I'm still a bit lost on something however: What does creating the two Rectangle objects actually do then? Why do I bother making constructors if the arguments I pass it don't actually ever get set as proper values for those objects? Furthermore, when I use the constructors to make those objects, what is actually being "constructed"? – That Random Guy Apr 02 '15 at 03:23
  • @ThatRandomGuy take alook at my EDIT, you will find the answer to all your questions. – cнŝdk Apr 02 '15 at 07:14
  • private getters ans setters are not a good option to him, AND if you use `this` in a Main method INSIDE Rectangle class, you won't get desired results... this answer is really complete (btw IS NOT full correct) but I think too much for OP question... – Jordi Castilla Apr 02 '15 at 07:24
  • @JordiCastilla You are right about the `this`, It's not necessarily needed, and for the private variables and using getters and setters I said it's a better approach but I didn't use it in my answer as you can see, so tehre's no need for the downvote. – cнŝdk Apr 02 '15 at 07:45
  • @JordiCastilla **No** they will never get only one value every instance of the class Rectangle has its own values. It's the **first** and **main** purpose of OOP !!! Here's a [**DEMO of the execution**](https://ideone.com/FMRd34). Please make sure you're right before judging and downvoting. – cнŝdk Apr 02 '15 at 07:54