+ 1

Shapes -

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.

12th Jan 2021, 5:15 AM
Sri Laya
Sri Laya - avatar
2 Réponses
+ 1
i am not getting the output with this import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape{ int x; Square(int width){ this.width=x; } void area() { System.out.println(width*width); } } class Circle extends Shape{ int y; Circle(double width) { this.width=y; } void area(){ System.out.println(Math.PI*width*width); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }
19th Mar 2021, 6:19 AM
Suraj Chintu
Suraj Chintu - avatar
0
System.out.println(Math.PI* (double)width*width // * Math.PI // not at the end );
16th Apr 2021, 8:16 AM
Dmitry Lezin
Dmitry Lezin - avatar