Unable to pass the test case 3 in Module 5 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Unable to pass the test case 3 in Module 5

Hey guys. I'm unable to pass the test case 3 It would be great if you could help me identify where i went wrong. Below is my 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; } public 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(); } }

24th Aug 2022, 12:27 PM
madhu spurthi
madhu spurthi - avatar
3 Answers
0
'width' is integer value. In arithmetic operations, first the operands are converted to higher types first. so int is converted to double double * int => results double int * int => result int value only. And also **rounded value for destination type. For ex: test with values 10, 11 : System.out.println(Math.PI* 10*10); System.out.println(Math.PI* 11*11); System.out.println(Math.PI* (10*10)); System.out.println(Math.PI* (11*11)); In first, case you get Math.PI*11 result is already a rounded value, then multiplied by 11 ,returns rounded result. Rounded both times. => it's like Math.PI*11 then rounded value multiplied by 11. It may not equal to multiply with 121. In second case : Math.PI multiplied by 121 then returns rounded value.. Hope it helps to understand..
24th Aug 2022, 1:38 PM
Jayakrishna 🇮🇳
+ 3
Thank you @Jayakrishna🇮🇳 I edited the code as u have suggested and it worked. But I didn't understand how it actually worked. when I tried both of them for other sums it gave me the same results. Can u tell me the difference between Math.PI * ( width*width) & Math.PI * width * width
24th Aug 2022, 1:06 PM
madhu spurthi
madhu spurthi - avatar
+ 2
Use Math.PI * width * width Instead of Math.PI * ( width*width) ; // remove braces, there you get a slite difference in praction part.
24th Aug 2022, 12:38 PM
Jayakrishna 🇮🇳