+ 4

Java shapes only test case 3 failed

Hello. I leave here the URL to my code. Every test case is passed but the third one. What's wrong? I checked everything over and over again, but the third test case is always failing and I see nothing strange. https://code.sololearn.com/cA13a1977a18/#java

10th May 2021, 3:23 PM
José Carlos Sánchez Maestre
José Carlos Sánchez Maestre - avatar
4 Respostas
+ 6
Floating point error. Change the Circle's area print to: Math.PI*width*width Having those parentheses can cause a floating point error in this instance as doubles are prone to do that
10th May 2021, 4:08 PM
Odyel
Odyel - avatar
+ 1
Hey, maybe late but the problem is the order of the area calculation: Correct order would be like this: Math.PI*Width*Width Different distributions such as: Width*Width*Math.PI Width*Math.PI*Width And so... Would likely generate floating point inaccuracy.
20th Aug 2022, 3:59 PM
Sandy Rosso
0
Odyel Thank you! However, I don't get it. Both Math.PI and Math.pow are doubles. I wondered if that floating point error you say might be due to the mathematical aberration that doubles commit sometimes. Isn't it? By the way, I had to delete both Math.pow in Square and Circle classes. This way, the problem in Circle is out and at the Square class I can spare casting to int.
10th May 2021, 5:03 PM
José Carlos Sánchez Maestre
José Carlos Sánchez Maestre - avatar
0
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape{ public int area2; public Square(int x) { width=x; } public void area() { area2=width*width; System.out.println(area2); } } class Circle extends Shape { public double area1; public Circle(int y) { width=y; } public void area() { area1=Math.PI*width*width; System.out.println(area1); } } 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(); } }
24th Jun 2021, 3:47 PM
Dharmi Sri
Dharmi Sri - avatar