+ 1

How to solve Shapes in java, am stucking here

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

29th Dec 2020, 12:31 AM
Jo$y ☑️
Jo$y ☑️ - avatar
7 ответов
+ 7
this work for me : import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape { Square(int x) { width = x; } void area() { System.out.println(width * width); } } class Circle extends Shape { Circle(int y){ width = y; } public 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(); } }
1st Apr 2021, 3:42 PM
BAIRRO DA MOITA
BAIRRO DA MOITA - avatar
0
Please post the code you have so far and indicate what specifically you are stuck on.
29th Dec 2020, 12:37 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
0
The width is to be initialized in the class constructor. You need to revise the class constructors to accept the variable that is passed when the Square object is initially created instantiated in main. Your constructor would be something like this: public Square(int width){ this.width = width;} Note that the constructor should not have ‘int’ in the method name. The methods called AreaCircle and AreaSquare are unnecessary and can be removed. You may simply place the area calculations directly in the System.out.println method call instead.
29th Dec 2020, 1:37 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
0
Area of the circle is PI*width*width, so it's Math.PI*y*y not 2*Math.PI*y
5th Feb 2021, 5:03 PM
Endo🐹
0
I could get the correct answer but didnt get correct in other three hidden cases, even after copied the code that sent here. Please help me with it!!!!
12th May 2021, 11:04 AM
Haryish
0
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape{ Square(int width){ this.width = width; } public void area (){ System.out.println(this.width * this.width); } } class Circle extends Shape{ double circul = Math.PI; Circle(int width){ this.width = width; } public void area(){ System.out.println(this.circul * this.width * this.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(); } }
16th Jul 2022, 1:53 AM
Cristian Riveros
Cristian Riveros  - avatar
- 3
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here public class Square extends Shape { public int Square(){ int width; } public int AreaSquare; public void area(int x){ AreaSquare = x * x; System.out.println(AreaSquare); } } public class Circle extends Shape { public int Circle(){ int width; } public double AreaCircle; public void area(int y){ AreaCircle = 2 * Math.PI * y; System.out.println(AreaCircle); } } 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(); } }
29th Dec 2020, 12:57 AM
Jo$y ☑️
Jo$y ☑️ - avatar