+ 1

Why, when I input 42 to the circle constructor the area gives me 5541.769440932395 while the correct answer is 5541.769440932396

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

27th Dec 2022, 10:34 PM
Genniell Vergaray
Genniell Vergaray - avatar
1 ответ
+ 3
I wouldnt be too worried about the 16th decimal of the area of a circle unless you sre building a rocket ship however this most likely has to do with how doubles work since it is limited by 16 deciamal digits and might not be able to round it correctly
27th Dec 2022, 11:11 PM
Vanessa Nilsson