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.
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.
For a working system, you would have to replace the definition of each of these methods with code to do the actual drawing.
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.
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Here are the 4 classes - Figure.java, Rectangle.java , Triangle.java and Canvas.java Thank You !! =========================================================================== public abstract class Figure { public abstract void draw(); public abstract void erase(); public abstract void center(); public abstract boolean equals(Figure figure); public abstract String toString(); }
========================================================
public class Rectangle extends Figure { public Rectangle() { System.out.println("Class Triangle, Method: constructor(). Object created"); } @Override public void draw() { System.out.println("Triangle class, Method: draw(). Drawing the Rectangle"); } @Override public void erase() { System.out.println("Triangle class, Method: erase(0. Currently deleting the figure.)"); } @Override public void center() { System.out.println("Triangle class, Method: center(). We are centering the image"); erase(); draw(); } @Override public boolean equals(Figure figure) { System.out.println("Triangle class, Method: equals(). We are checking two objects are same or not"); return true; } @Override public String toString() { return "Triangle class , Method : toString()"; } }
========================================================
public class Triangle extends Figure { public Triangle() { System.out.println("Class Triangle, Method: constructor(). Object created"); } @Override public void draw() { System.out.println("Triangle class, Method: draw(). Drawing the Triangle"); } @Override public void erase() { System.out.println("Triangle class, Method: erase(0. Currently deleting the figure.)"); } @Override public void center() { System.out.println("Triangle class, Method: center(). We are centering the image"); erase(); draw(); } @Override public boolean equals(Figure figure) { System.out.println("Triangle class, Method: equals(). We are checking two objects are same or not"); return true; } @Override public String toString() { return "Triangle class , Method : toString()"; } }
========================================================
public class Canvas{ public static void main(String[] args) { Figure figures [] = new Figure[4]; figures[0] = new Rectangle(); figures[1] = new Rectangle(); figures[2] = new Triangle(); figures[3] = new Triangle(); for(Figure fig:figures){ System.out.println(fig); } figures[0].draw(); figures[3].draw(); figures[1].center(); figures[2].center(); } }
========================================================
Get Answers For Free
Most questions answered within 1 hours.