In the main window, clink: New Class
A window will pop up.
Enter the class name and click OK
An icon will appear in the BlueJ main window.
Double click on the class icon to edit the class.
|
In this assignment, we will be writing a Java class called Rectangle that is used to create rectangle objects.
A rectangle object has 2 properties:
double width: the width of the rectangle double height: the height of the rectangle |
We want to allow the user to create rectangles in 2 ways:
new Rectangle(): creates a rectangle with width=1 and height=1 new Rectangle(w,h): creates a rectangle with width=w and height=h |
The Rectangle object will have 3 actions:
getArea(): return the area of this rectangle setWidth(w): set the width of this rectangle to w setHeight(h): set the height of this rectangle to h |
Use the following myProg class to test the Rectangle class that you write for this assignment:
public class myProg
{
public static void main(String[] args)
{
Rectangle r1 = new Rectangle();
System.out.println("Area of r1 = " + r1.getArea() );
Rectangle r2 = new Rectangle(2,3);
System.out.println("Area of r2 = " + r2.getArea() );
r2.setWidth(4);
System.out.println("Area2 of r2 = " + r2.getArea() );
r2.setHeight(5);
System.out.println("Area3 of r2 = " + r2.getArea() );
}
}
|
If your Rectangle class is correct, the output of the test program will be:
Area of r1 = 1.0 Area of r2 = 6.0 Area2 of r2 = 12.0 Area3 of r2 = 20.0 |