+ 5

Hello everyone. I need help. I write a program it can't answer for all inputs and I can't solve the problem

You are working on a graphical app, which includes multiple different shapes. The given code declares a base Shape class with an abstract area() method and a width attribute. You need to create two Shape subclasses, Square and Circle, which initialize the width attribute using their constructor, and define their area() methods. The area() for the Square class should output the area of the square (the square of the width), while for the Circle, it should output the area of the given circle (PI*width*width). The code in main creates two objects with the given user input and calls the area() methods. Sample Input: 5 2 Sample Output: 25 12.566370614359172 And it is my solve 👇 https://code.sololearn.com/cOnWr5xJswhA/?ref=app

28th Jan 2021, 6:25 PM
Ghezal Shefa
Ghezal Shefa - avatar
5 odpowiedzi
+ 7
Thanks for your help I find my problem and now it is working for all number 🤩🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
28th Jan 2021, 8:01 PM
Ghezal Shefa
Ghezal Shefa - avatar
28th Jan 2021, 8:01 PM
Ghezal Shefa
Ghezal Shefa - avatar
+ 5
Abol thanks again
28th Jan 2021, 8:01 PM
Ghezal Shefa
Ghezal Shefa - avatar
+ 3
The problem is not in your side! Just reverse order for area of circle to pi*width*width and it passes all tests. The challenge doesn't do proper comparison of floating point numbers. A different order of operations leads to slightly different rounding. So your numbers are off at the last digit probably. Differences can also arise from the different type of variables, combined with the order of calculation
28th Jan 2021, 7:23 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 3
This article describes the problem in more detail: https://howtodoinjava.com/java-examples/correctly-compare-float-double/ Also Java has a solution: java.lang.Float.compare()
28th Jan 2021, 7:30 PM
Benjamin Jürgens
Benjamin Jürgens - avatar