Question: Question : Geoshapes Using the shapes Project (below), construct an Abstract Class, GeoShapes. Make classes Circle, Square and Triangle extend the Abstract Class, GeoShapes. start
Question : Geoshapes
- Using the shapes Project (below), construct an Abstract Class, GeoShapes. Make classes Circle, Square and Triangle extend the Abstract Class, GeoShapes.
- start with the Circle Class and perform the modification on one method or method pair at a time, compiling in between.
public abstract class GeoShapes { boolean isVisible; /** copy/paste all commonly defined methods for circle, square & triangle */ /** * Make this circle visible. If it was already visible, do nothing. */ public void makeVisible() { isVisible = true; draw(); } /** * Make this circle invisible. If it was already invisible, do nothing. */ public void makeInvisible() { erase(); isVisible = false; }
/** define all geometrically dependent classes as abstract */ abstract void draw();
abstract void erase(); }
Actions for Geoshapes Project
public class GeoMain {
public static void main(String[] args) { /** Instanctiate objects */ Circle circle1 = new Circle(); Circle circle2 = new Circle(); Triangle triangle1 = new Triangle(); Square square1 = new Square(); /** do stuff to circle1 object*/ circle1.changeColor("yellow"); circle1. makeVisible();
/** do stuff to circle2 object*/ circle2.changeColor("green"); circle2. makeVisible(); triangle1. makeVisible(); square1. makeVisible(); /** animate all objects */ circle1.slowMoveHorizontal(50); circle2.slowMoveVertical(50); triangle1.slowMoveHorizontal(100); square1.slowMoveVertical(150); } }
Actions for Geoshapes Project
(1) Abstract (Parent) Class Geoshapes - Created an abstract Class named Geoshapes - Copied into the Geoshapes Class the Class variables used by all of the child classes (did not copy dimension vars) Change scope (private to none) int xPosition; int yPosition; String color; boolean isVisible; - Copied into the Geoshapes Class the methods with code used by all of the child classes that were defined the same void makeVisible() void makeInvisible() void moveRight() void moveLeft() void moveUp() void moveDown() void moveHorizontal(int distance) void moveVertical(int distance) void slowMoveHorizontal(int distance) void slowMoveVertical(int distance) void changeColor(String newColor) void erase() Did not copy constructor methods in child classes - Copy signature line of methods that require unique definition in child Classes into Geoshapes and make abstract. (Modify the scope) abstract void changeSize(int dimension); // changesize is overloaded = 2 versions abstract void changeSize(int hdimension, int vdimension); abstract void draw(); void erase();
(2,3,4) Extended Child Classes: Circle, Square, Triangle - Make the child class extend the abstract-parent Class Geoshapes Allows the child class to inherit all methods defined methods defined (with code between {}) in the abstract-parent - Keep Class Variables that are different between classes (dimensions) Modify the scope (private to none) Eliminate those those class variable that are shared int xPosition; int yPosition; String color; boolean isVisible; - Remove all methods for which code is inherited from the parent class void makeVisible() void makeInvisible() void moveRight() void moveLeft() void moveUp() void moveDown() void moveHorizontal(int distance) void moveVertical(int distance) void slowMoveHorizontal(int distance) void slowMoveVertical(int distance) void changeColor(String newColor) void erase() - Keep contructor methods in the child class - Keep code for methods in the child class that are different (dependent on geometry)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
