Rectangle Example
Setup these two files and try running the program(s).
File 1: What is the name of this file?
// No comments on purpose - Do you understand what is going on?
import java.awt.*;
public class MyRect1
{
public static void main(String args[])
{
MyRect2 testRect = new MyRect2();
System.out.println("Using MyRect2 object.");
testRect.printRect();
testRect.x1 = 1;
testRect.printRect();
System.out.println("Calling MyRect2 main().");
MyRect2.main(null);
}
}
File 2: What is the name of this file?
import java.awt.*;
public class MyRect2
{
int x1;
int y1;
int x2;
int y2;
public MyRect2()
{
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
}
public MyRect2(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public MyRect2(Point topLeft, Point bottomRight)
{
x1 = topLeft.x;
y1 = topLeft.y;
x2 = bottomRight.x;
y2 = bottomRight.y;
}
public MyRect2(Point topLeft, int width, int height)
{
x1 = topLeft.x;
y1 = topLeft.y;
x2 = (x1 + width);
y2 = (y1 + height);
}
void printRect()
{
System.out.println("Draw rectangle: top left at " + x1 + ", " + y1 +
" bottom right at " + x2 + ", " + y2);
}
public static void main(String args[])
{
MyRect2 rectangle;
rectangle = new MyRect2(25, 25, 50, 50);
rectangle.printRect();
rectangle = new MyRect2(new Point(10, 75), new Point (200, 150));
rectangle.printRect();
rectangle = new MyRect2(new Point(50, 200), 100, 125);
rectangle.printRect();
}
}