You are working on a graphical app, which includes multiple different shapes. The given code declares a base Shape class with an abstract area() method and a width attribute. You need to create two Shape subclasses, Square and Circle, which initialize the width attribute using their constructor, and define their area() methods. The area() for the Square class should output the area of the square (the square of the width), while for the Circle, it should output the area of the given circle (PI*width*width). The code in main creates two objects with the given user input and calls the area() methods.

Sample Input:
5
2

Sample Output:
25
12.566370614359172

The area of the square is 5*5=25, while the area of the circle is PI*2*2=12.566370614359172

Respuesta :

A class can have multiple subclasses, where each of the subclasses function independently. The extend keyword will be used to pass attributes from the Shape class to the Square and Circle subclasses

To answer this question, I will make the following assumptions

  • The Shape class is already defined
  • The area() method of the Shape class is also defined
  • Lines that begin with // are comments, and these lines will be used to explain the code segment

//This creates the Square class

class Square extends Shape {

//The creates a constructor for width variable

Square(int length) {  this.width = length;  }

//This defines the area() method from the Square class

void area() {  

//This calculates and prints the area of the square

System.out.print(Math.pow(width,2));  } }

//This creates the Circle class

class Circle extends Shape {

//The creates a constructor for width variable

Circle(int radius){  this.width = radius;  }

//This defines the area() method from the Square class

void area(){

//This calculates and prints the area of the circle

System.out.println(Math.PI*Math.pow(width,2));

} }

Read more about abstract class at:

https://brainly.com/question/20261938