In: Computer Science
Using Java
Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes:
Consider a graphics system that has classes for various figures — say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and either a center point or upper-left corner point, while a box and circle might have only a center point (or upper-right corner point) and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. In this homework assignment, you will implement such a system.
The class Figure is an abstract base class. In this class, create abstract methods for draw, erase, center, equals, and toString.
Add Rectangle and Triangle classes derived from Figure. Each class has stubs for methods erase, draw, and center. In these "stubs", the method simply prints a message telling the name of the class and which method has been called in that class. Because these are just stubs, they do nothing more than output this message. The method center calls the erase and draw methods to erase the object at its current location and redraw the figure at the center of the picture being displayed. Because you have only stubs for erase and draw, center will not do any real “centering” but simply will call the methods erase and draw, which will allow you to see which versions of draw and center it calls. Add an output message in the method center that announces that center is being called. The methods should take no arguments.
Define a demonstration program for your classes which contains main. Test your programs:
In main, make sure that you tell the user what's going on at each step.
Your output should consist primarily of messages from your stub methods. An example of the output from your program might be the following (note: output from main method is shown below in italics):
** Create 2 triangles ** Entering Figure Constructor for triangle 0 Running Triangle constructor for triangle 0 Entering Figure Constructor for triangle 1 Running Triangle constructor for triangle 1 ** Create 2 rectangles ** Entering Figure Constructor for rectangle 0 Running Rectangle constructor for rectangle 0 Entering Figure Constructor for rectangle 1 Running Rectangle constructor for rectangle 1 ** Draw both triangles ** Entering draw() method for Triangle 0 Entering draw() method for Triangle 1 ** Draw both rectangles ** Entering draw() method for Rectangle 0 Entering draw() method for Rectangle 1 ** Center one triangle ** Entering center() method for Triangle 0 center() calling erase() Entering erase() method for Triangle 0> center() calling setCenter() to reset center. Entering Figure's setCenter() for figure 0 center() calling draw() Entering draw() method for Triangle 0 ** Center one rectangle ** Entering center() method for Rectangle 1 center() calling erase() Entering erase() method for Rectangle 1 center() calling setCenter() Entering Figure's setCenter() for figure 1 center() calling draw() Entering draw() method for Rectangle 1 |
Program
abstract class Figure
{
Figure(String s)
{
System.out.println("Entering Figure
Constructor for " + s);
}
abstract void erase();
abstract void draw();
abstract void center();
abstract boolean equals();
abstract String tostring();
void setCenter()
{
System.out.println("Entering
Figure's setCenter() for figure");
}
}
class Rectangle extends Figure
{
double height, width;
int n;
Rectangle(int i)
{
super("rectangle " + i);
n = i;
System.out.println("Entering
Rectangle Constructor for rectangle " + i);
}
void draw()
{
System.out.println("Entering draw()
method for Rectangle " + n);
}
void erase()
{
System.out.println("Entering
erase() method for Rectangle " + n);
}
void center()
{
System.out.println("Entering
center() method for Rectangle " + n);
System.out.println("center()
calling erase()");
erase();
System.out.println("center()
calling setCenter()");
setCenter();
System.out.println("center()
calling draw()");
draw();
}
boolean equals()
{
return true;
}
String tostring()
{
return "toString() has been
revoked";
}
}
class Triangle extends Figure
{
double radius;
int n;
Triangle(int i)
{
super("triangle " + i);
n = i;
System.out.println("Entering
Triangle Constructor for triangle " + i);
}
void draw()
{
System.out.println("Entering draw()
method for Triangle " + n);
}
void erase()
{
System.out.println("Entering
erase() method for Triangle " + n );
}
void setCenter()
{
System.out.println("Entering
setCenter() method for Triangle");
}
void center()
{
System.out.println("Entering
center() method for Triangle " + n);
System.out.println("center()
calling erase()");
erase();
System.out.println("center()
calling setCenter() for reset center.");
setCenter();
System.out.println("center()
calling draw()");
draw();
}
boolean equals()
{
return true;
}
String tostring()
{
return "toString() has been
revoked";
}
}
class Driver
{
public static void main (String[] args)
{
System.out.println("\n** Create 2
triangles **");
Triangle t1 = new
Triangle(0);
Triangle t2 = new Triangle(1);
System.out.println("\n** Create
2 rectangles **");
Rectangle r1 = new
Rectangle(0);
Rectangle r2 = new
Rectangle(1);
System.out.println("\n** Draw both
triangles **");
t1.draw();
t2.draw();
System.out.println("\n** Draw both
rectangles **");
r1.draw();
r2.draw();
System.out.println("\n** Center one
triangle **");
t1.center();
System.out.println("\n** Center one
rectangle **");
r2.center();
}
}
Output:
** Create 2 triangles **
Entering Figure Constructor for triangle 0
Entering Triangle Constructor for triangle 0
Entering Figure Constructor for triangle 1
Entering Triangle Constructor for triangle 1
** Create 2 rectangles **
Entering Figure Constructor for rectangle 0
Entering Rectangle Constructor for rectangle 0
Entering Figure Constructor for rectangle 1
Entering Rectangle Constructor for rectangle 1
** Draw both triangles **
Entering draw() method for Triangle 0
Entering draw() method for Triangle 1
** Draw both rectangles **
Entering draw() method for Rectangle 0
Entering draw() method for Rectangle 1
** Center one triangle **
Entering center() method for Triangle 0
center() calling erase()
Entering erase() method for Triangle 0
center() calling setCenter() for figure
Entering setCenter() method for Triangle
center() calling draw()
Entering draw() method for Triangle 0
** Center one rectangle **
Entering center() method for Rectangle 1
center() calling erase()
Entering erase() method for Rectangle 1
center() calling setCenter()
Entering Figure's setCenter() for figure
center() calling draw()
Entering draw() method for Rectangle 1