Develop the ‘Shape’ application such that: ‘Rectangle’, ‘Ellipse’, and ‘Triangle
ID: 3824546 • Letter: D
Question
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
Remember to make sure that there is only a single class per file.
I don't know how to start
what does single class per file mean??
Explanation / Answer
1. Shape Class-
public class Shape {
public Shape() {
System.out.println("Shape construtor called");
}
public void draw() {
System.out.println("Shape - draw method");
}
public void move() {
System.out.println("Shape - move method");
}
public void erase() {
System.out.println("Shape - erase method");
}
}
2. Rectangle class -
public class Rectangle extends Shape {
public Rectangle() {
System.out.println("Rectangle construtor called");
}
public void draw() {
System.out.println("Rectangle - draw method");
}
public void move() {
System.out.println("Rectangle - move method");
}
public void erase() {
System.out.println("Rectangle - erase method");
}
}
3. Ellipse class -
public class Ellipse extends Shape {
public Ellipse() {
System.out.println("Ellipse construtor called");
}
public void draw() {
System.out.println("Ellipse - draw method");
}
public void move() {
System.out.println("Ellipse - move method");
}
public void erase() {
System.out.println("Ellipse - erase method");
}
}
4. Triangle class -
public class Triangle extends Shape {
public Triangle() {
System.out.println("Triangle construtor called");
}
public void draw() {
System.out.println("Triangle - draw method");
}
public void move() {
System.out.println("Triangle - move method");
}
public void erase() {
System.out.println("Triangle - erase method");
}
}
5. Square class -
public class Square extends Rectangle {
public Square() {
System.out.println("Square construtor called");
}
public void draw() {
System.out.println("Square - draw method");
}
public void move() {
System.out.println("Square - move method");
}
public void erase() {
System.out.println("Square - erase method");
}
}
6. Circle class -
public class Circle extends Ellipse {
public Circle() {
System.out.println("Circle construtor called");
}
public void draw() {
System.out.println("Circle - draw method");
}
public void move() {
System.out.println("Circle - move method");
}
public void erase() {
System.out.println("Circle - erase method");
}
}
7. ShapeTest class -
public class ShapeTest {
public static void main(String[] args) {
Shape shape = new Shape();
shape.draw();
shape.move();
shape.erase();
Rectangle rectangle = new Rectangle();
rectangle.draw();
rectangle.move();
rectangle.erase();
Triangle triangle = new Triangle();
triangle.draw();
triangle.move();
triangle.erase();
Ellipse ellipse = new Ellipse();
ellipse.draw();
ellipse.move();
ellipse.erase();
Square square = new Square();
square.draw();
square.move();
square.erase();
Circle circle = new Circle();
circle.move();
circle.draw();
circle.erase();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.