0

test case hidden and false

import java.awt.*; import java.util.Scanner; abstract class Shape{ int width; abstract void area(); } //your code goes here class Square extends Shape{ int s; Square(int xx){ s = xx; } public void area(){ s =(int)(s * s); System.out.println(s); } } class Circle extends Shape{ double c; Circle(int yy){ c =(double) yy; } public void area(){ c =(double)(c * c * (Math.PI)); System.out.println(c); } } 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(); } } ......2 test cases are true , the third failed and hidden ,how can I solve it?

3rd Nov 2022, 8:24 AM
Eng-Rehab Abdo
Eng-Rehab Abdo - avatar
5 Respostas
3rd Nov 2022, 10:32 AM
Jayakrishna 🇮🇳
+ 2
When doing floating point arithmetic, minor rounding differences can result even from changing the order of the operands. Because if you multiply int by double, the integer is also implicitly cast to double. It is not really fair from the Sololearn test case validation to break from this sort of error. When otherwise your logic was perfectly fine. Normally when we compare floats, we allow a certain tiny margin of error, because of these rounding issues.
3rd Nov 2022, 11:33 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Don't declare separate int/double fields in your child classes. Just use the inherited width variable. That is the whole point of inheritance.
3rd Nov 2022, 9:11 AM
Tibor Santa
Tibor Santa - avatar
+ 1
i try this and it works c = Math.PI * c * c * ; I don't know why 😄
3rd Nov 2022, 9:54 AM
Eng-Rehab Abdo
Eng-Rehab Abdo - avatar
0
I try it now , it gives same result.
3rd Nov 2022, 9:31 AM
Eng-Rehab Abdo
Eng-Rehab Abdo - avatar