Question
Java Imagine the design of a graphics system that has classes for various figures (rectangles, triangles, circles, etc.) drawn on a canvas or grid. When
Java
Imagine the design of a graphics system that has classes for various figures (rectangles, triangles, circles, etc.) drawn on a canvas or grid. When designing the properties of a rectangle, for example, you might include data members for height, width, and either a center point or upper-left corner point. A circle might have only a center point (or upper-right corner point) and an edge length or radius. In this homework assignment, you will create a strawman for such a system.
"Strawman" systems don't do any real processing. Rather they are used simply to track how programs navigate through various methods. They are very useful when designing systems with hundreds or thousands of methods calls.
For this assignment, create two classes named Rectangle and Triangle. Each class should have "stubs" for constructors and methods erase, draw, center, and toString. In these stubs, each method simply prints a message telling the name of the class and which method has been called, nothing more. (For a working system, you would have to replace the definition of each of these methods with code to actually draw or erase figures on some sort of canvas, which is not part of this homework assignment.)
All methods will have a single parameter which is the name of the object being drawn. For example, when passed an object called "Rectangle 1", the draw method might print "Entering draw() for Rectangle 1".
Here is a description of the methods in Rectangle and Triangle.
- The constructor will print "Running the constructor for ".
- The draw method will print "Entering draw() for <object name>".
- The erase method will print "Entering erase() for ".
- The center method will print "Entering center() for , invoke erase, then print "Centering ", then invoke draw.
- The toString method returns a String similar to "Running toString() for ".
Other messages which might be helpful in tracking what's going on in your program are also encouraged.
In a separate class, create a main method which:
- creates at least two Rectangle objects and two Triangle objects.
- "draws" all of the objects you just created
- "centers" at least one Rectangle and one Triangle object
In each of your classes, make sure that you add comments to tell another programmer what's going on at each step.
An example of the output from your program might be the following (note: output from main methods is shown in italics):
** Create 2 triangles ** Running Triangle constructor for triangle 0 Running Triangle constructor for triangle 1 ** Create 2 rectangles ** Running Rectangle constructor for rectangle 0 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() is centering the 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() is centering the Rectangle center() calling draw() Entering draw() method for Rectangle 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started