Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Apply the following then mention and discuss the output class Shape { void draw(

ID: 3621017 • Letter: A

Question

Apply the following then mention and discuss the output
class Shape {
void draw() {}
void erase() {}
}
class Circle extends Shape {
void draw() {
System.out.println("Circle.draw()");
}
void erase() {
System.out.println("Circle.erase()");
} }
class Square extends Shape {
void draw() {
System.out.println("Square.draw()");
}
void erase() {
System.out.println("Square.erase()");
}}
class Triangle extends Shape {
void draw() {
System.out.println("Triangle.draw()");
}
void erase() {
System.out.println("Triangle.erase()");
}}
A. Build the main class Shapes that contains a static method randShape that produces a reference to a randomly-selected Shape object each time you call it.
The main should contains an array of ten Shape references filled through randShape . through this array call draw for each one
B. Add a new type of Shape and verify in main that polymorphism works for your new type as it does in the old types.
C. Add a new method in the base class that prints a message, but don’t override it in the derived classes, call it in the main as you did with draw(),.Explain what happens. Now override it in one of the derived classes but not the others, and see what happens. Finally, override it in all the derived classes.
D. Add a new method in the Square class that prints its name, call it in the main as you did with draw(),explain what happens, explain how we can use it correctly?

*********************************************
i do it
import java.util.*;
public class Main {


public Main() {
}


public static void main(String[] args) {

Shape []array=new Shape[10];

for(int i=0;i<10;i++)
{
array[i].randShape();


}




Shape cir = new Circle();
cir.draw();
cir.erase();
cir.massege(); // mathod in base class
cir.massege2(); // method in base class and circle class
cir.massege3(); //method in all sub class (onerriding)

System.out.println("*****************");
Shape sqr=new Square();

sqr.erase();
sqr.erase();
sqr.massege();
sqr.massege2();
sqr.massege3();
Square sq=new Square();
// sq=(Square)sqr;
sq.sqrname();
System.out.println("*****************");
Shape tria = new Triangle();
tria.draw();
tria.erase();
tria.massege();
tria.massege2();
tria.massege3();

}

}
class Shape {

void massege(){
System.out.println("massege");

}
void massege2(){
System.out.println("massege 2 super class");

}
void massege3(){
System.out.println("massege 3 super class");

}

void draw() {}

void erase() {}

static void randShape()
{
Shape sh = new Shape();
}


}
class Circle extends Shape {

void draw() {

System.out.println("Circle.draw()");

}

void erase() {

System.out.println("Circle.erase()");

}
void massege2(){
System.out.println("massege 2 in circle class");

}
void massege3(){
System.out.println("massege 3 in circle class");


}
}
class Square extends Shape {

void draw() {

System.out.println("Square.draw()");

}

void erase() {

System.out.println("Square.erase()");

}
void massege3(){
System.out.println("massege 3 in square class");

}
void sqrname(){
System.out.println("Square Name");
}
}
class Triangle extends Shape {

void draw() {

System.out.println("Triangle.draw()");

}

void erase() {

System.out.println("Triangle.erase()");

}
void massege3(){
System.out.println("massege 3 in triangle class");
}

}

Explanation / Answer

Dear, a) public class inher//Shapes { public static Shape randShape() { switch((int)(Math.random() * 3)) { default: case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); } } public static void main(String[] args) { Shape[] s = new Shape[9]; // Fill up the array with shapes: for(int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote