0

Why this code is failing at Test 3...?? Pls help

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

2nd May 2021, 6:43 AM
Ashok Kumar
Ashok Kumar - avatar
2 odpowiedzi
+ 2
In the Circle class at the area method, remove the parentheses around the this.width*this.width so its just Math.PI*this.width*this.width. Dunno why it didn't work the other way, doubles are prone to accuracy problems so that might be the reason; order of operations might've made it bug out, not too sure
2nd May 2021, 7:29 AM
Odyel
Odyel - avatar
0
Tq Very much Bro😘 , I really got no idea , that Parentheses can create that mach vary in the code... Tq again 🤗
2nd May 2021, 10:37 AM
Ashok Kumar
Ashok Kumar - avatar