Really struggling with this program, looking for help it. It needs to be done in
ID: 3633560 • Letter: R
Question
Really struggling with this program, looking for help it. It needs to be done in Java
Thanks
Inheritance:
Problem: Develop the ‘Shape’ application such that:
• ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class.
• Develop the ‘Square’ and ‘Circle’ class where ‘Square’ inherits from ‘Rectangle’ and ‘Circle’ inherits
from ‘Ellipse’. ‘Triangle’ has no derived class.
• For each class, implement the overridden methods ‘draw’, ‘move’, and ‘erase’. Each method should only
have an output statement such as “Rectangle – draw method” that will be displayed when the method is
invoked.
• Implement the default constructors for each class with a corresponding message to be displayed when
invoked. No initializations are required; that is, the output message will be the only executable statement in
the constructors.
• Do not implement any other methods for these classes ( i.e., ‘toString’, ‘equals’, getters and setters ).
• Implement a ‘ShapeTest’ class which will instantiate an object of each class.
• Exercise each of the ‘draw’, ‘move’, and ‘erase’ methods of each class
Explanation / Answer
abstract class Shape { Shape(){ System.out.println("Initialize generic shape parameters"); } public abstract void draw(); public abstract void move(); public abstract void erase(); } class Rectangle extends Shape { Rectangle(){ super(); System.out.println("Rect-constructor"); } public void draw(){ System.out.println("draw rect");} public void erase(){ System.out.println("erase rect");} public void move(){ System.out.println("move rect");} } class Triangle extends Shape { Triangle(){ super(); System.out.println("Tri-constructor"); } public void draw(){ System.out.println("draw tri");} public void erase(){ System.out.println("erase tri");} public void move(){ System.out.println("move tri");} } class Ellipse extends Shape { Ellipse(){ super(); System.out.println("Ellipse-constructor"); } public void draw(){ System.out.println("draw el");} public void erase(){ System.out.println("erase el");} public void move(){ System.out.println("move el");} } class Square extends Rectangle { Square(){ super(); //square parameters of course System.out.println("square constructor"); } //whatever extra methods } class Circle extends Ellipse { Circle(){ super(); //circ parameters of course System.out.println("circ constructor"); } //whatever extra methods } class ShapeTest { void main() { Shape s; s=new Rectangle();test(s); s=new Square(); test(s); s=new Ellipse(); test(s); s=new Circle(); test(s); s=new Triangle(); test(s); } void test(Shape s) { s.draw(); s.move(); s.erase(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.