Java

OOP GUIDE / WORK

 

RECTANGLE CLASS 3

 

Topics

  • Encapsulation

 

TASK – PART 1 – SETUP

 

Copy the follow class into your IDE (Eclipse).

 

 

public class Rectangle

{

   public double width;

   public double height;

   public double x;

   public double y;

 

   public Rectangle( double x, double y, double w, double h)

   {

      this.x = x;

      this.y = y;

      width = w;

      height = h;

   }

  

   public double area()

   {

        return width * height;

   }

}

 

 

 

TASK – PART 2 – SHORT QUESTIONS

 

a)            How many instance variables does this class have?

b)            How many constructors does this class have?

c)            How many instance methods does this class have?

 

d)            Why do two of the statements in the constructor have a “this” and two do not?

 

 

TASK – PART 3 – DATA ENCAPSULATION

 

Use data encapsulation on the class. 

Make all instance variables private.

Include get and set methods for all instance variables.


Make sure that the width and height values cannot take on a negative number.  In such a situation, you should set the width or height to the absolute value of the negative number.

Test your new version of the Rectangle class inside of a RectangleTester class.  Create a Rectangle object and call all methods.