+ 2

Any one who can identify the bug?? Am unable to pass the 3rd test. It is the second last project in java lesson(more on classes)

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here public class Square extends Shape{ Square(int a){ width=a; } public void area(){ System.out.println(width*width); } } public class Circle extends Shape{ Circle(int b){ width=b; } 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(); } }

18th Jul 2022, 6:28 PM
socka
socka - avatar
4 Answers
+ 3
Don't use braces around , just use Math.PI*width*width ;
18th Jul 2022, 6:30 PM
Jayakrishna 🇼🇳
+ 2
In calculations, when you have double first then next all values are upcasted to double so double * int produce double value, and double * double produce double value If you use braces, then it's just int*int=int ,next it's double * int=double, without braces it's do like double * double * double = double so first one loss some precision value but later give you accurate precision value of double value... edit: socka check both values when width=11, 17 is both ways same output ? No.
18th Jul 2022, 6:47 PM
Jayakrishna 🇼🇳
+ 1
Thanks it worked...could u explain why that was a problem
18th Jul 2022, 6:35 PM
socka
socka - avatar
+ 1
Thanks u...I now understand why...and no they are not producing the same output
18th Jul 2022, 7:12 PM
socka
socka - avatar