Consider a graphic system that has classes for various figures, say rectangles,
ID: 3572100 • Letter: C
Question
Consider a graphic system that has classes for various figures, say rectangles, squares, triangles, circles and so on. For example, a rectangle might have data members height, width and center point. While a square and a circle might have only a center point and an edge length and a radius, respectively. In a well-designed system these would be derived from a common class, Figure. You are to implement such a system. The class Figure is the base class. You should add only Rectangle and Circle classes derived from Figure. Each class has stubs for member functions erase() and draw(). Each of these member functions outputs a message telling what function has been called and what class of the calling objects it. Since these are just stubs, they do nothing more than output this message. The member function center() calls erase() and draw() to erase and redraw the figure at the center. Because you have only stubs for erase() and draw(), center() will not do any “centering” but will call the member function erase() and draw(). Also, add an output message in the member function center() that announces that center is being called. Make the base class member functions virtual. Write your own driver program to test the center() and draw() for rectangle and circle class objects. For this simple project, we can divide our codes into several files:
Assignment6.cpp: the application file.
figure.h: Interface of class figure.
figure.cpp: Implementation of class figure.
rectangle.h: Interface of class figure.
rectangle.cpp: Implementation of class figure.
circle.h: Interface of class figure.
circle.cpp: Implementation of class figure.
Explanation / Answer
public abstract class Figure
{
public Figure ()
{
System.out.println("Creating a figure with no parameters.");
}
public abstract double area();
public void erase()
{
System.out.println("Call to Figure's erase method.");
}
public void draw()
{
System.out.println("Call to Figure's draw method.");
}
public void center()
{
System.out.println("Calling Figure's center method.");
this.erase();
this.draw();
}
}// Figure
// Rectangle.java
public class Rectangle extends Figure
{
private int _width;
private int _height;
public Rectangle ()
{
System.out.println("Creating Rectangle Class with no parameters.");
_width = 0;
_height = 0;
}
public Rectangle(Rectangle other)
{
System.out.println("Creating Rectangle Class from another "
+ "Rectangle.");
_width = other._width;
_height = other._heightand draw. ;
}
public Rectangle(int width, int height)
{
System.out.println("Creating Rectangle Class given width and " +
"height.");
_width = width;
_height = height;
}
public double area(){
return length * width;
}
public void draw()
{
System.out.println("Calling Rectangle's draw method.");
}
public void erase()
{
System.out.println("Calling Rectangle's erase method.");
}
}// Rectangle
// Circle.java
public class Circle extends Figure{
private int radius;
public Circle ()
{
System.out.println("Creating Circle Class with no parameters.");
radius = 0;
}
public Circle (Circle other)
{
System.out.println("Creating Circle Class from another Circle.");
_radius = other. radius;
}
public Circle (int radius)
{
System.out.println("Creating Circle Class given radius.");
_radius = radius;
}
public void draw()
{
System.out.println("Calling Circle’s draw method.");
}
public void erase()
{
System.out.println("Calling Circle‘s erase method.");
}
}// Triangle
public class Test
{
public static void main(String[] args)
{
Figure f1 = new Figure();
f1.draw();
f1.erase();
f1.center();
Circle t1 = new Circle ();
t1.draw();
t1.erase();
t1.center();
Rectangle r1 = new Rectangle();
r1.draw();
r1.erase();
r1.center();
}
} // Test
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.