How to solve Shapes in java, am stucking here | Sololearn: Learn to code for FREE!

0

# 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

12/29/2020 12:31:34 AM

Jo\$\$y ☑️

+2

Area of the circle is PI*width*width, so it's Math.PI*y*y not 2*Math.PI*y

+2

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(); } }

+1

Please post the code you have so far and indicate what specifically you are stuck on.

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.

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!!!!

-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(); } }