+ 1

Why one test case is not passing?

I'm doing JAVA Course on sololearn and there is task named Shape. Here is problem statement: 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 The code I have written is in comment box

10th Sep 2022, 1:12 PM
Avijit Pal
Avijit Pal - avatar
5 Antworten
+ 2
Don't use braces around (width*width) Just use System.out.print(Math.PI*width*width)
10th Sep 2022, 1:16 PM
Jayakrishna 🇮🇳
+ 1
Here is my complete code: 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; } 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(); } }
10th Sep 2022, 1:13 PM
Avijit Pal
Avijit Pal - avatar
+ 1
Thanks, now it's working. But what is the reason???
10th Sep 2022, 2:04 PM
Avijit Pal
Avijit Pal - avatar
+ 1
There could exists a slite rouding difference... Hope this helps... See https://www.sololearn.com/discuss/3076671/?ref=app
10th Sep 2022, 2:19 PM
Jayakrishna 🇮🇳
+ 1
Thanks buddy
14th Sep 2022, 7:13 AM
Avijit Pal
Avijit Pal - avatar