+ 4

Java Shapes Project

Hi! I'm learning Java and I've been sitting on the Shapes project in Java for several hours now. I pass all the test cases, beside #3, which is hidden. I thought that it was because of integer overflow, but even after dealing with it, it wouldn't pass. I'm adding my code below, and if someone could help me out I'd really appreciate it. Thanks! --------------------------- abstract class Shape { int width; abstract void area(); Shape (int width){ this.width = width; } } //your code goes here class Square extends Shape { Square(int x){ super(x); } @Override public void area(){ int calcArea = this.width * this.width; if (calcArea >= 0){ System.out.println(calcArea); } else { System.out.println(Integer.MAX_VALUE * Integer.MAX_VALUE); } } } class Circle extends Shape { Circle(int y){ super(y); } public void area(){ int calcArea = this.width * this.width; double circleArea = calcArea * Math.PI; if (calcArea >= 0){ System.out.println(circleArea); } else { System.out.println(Integer.MAX_VALUE * Integer.MAX_VALUE * 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(); } }

31st Dec 2020, 1:16 PM
Efrat Harel
16 odpowiedzi
+ 6
You are welcome. First you have changed the abstract class which was not required. In addition your constructors are not allowed in this case. Second in extremely cases of double numbers, here in circle, the order of multiplications has influence on the result.
31st Dec 2020, 5:33 PM
JaScript
JaScript - avatar
+ 5
AHA!! the ORDER of the multiplication! That was my issue on the 3rd test case. Thank you for the tip.
20th Jan 2021, 8:30 PM
Adrian Ciuciui
Adrian Ciuciui - avatar
31st Dec 2020, 1:34 PM
JaScript
JaScript - avatar
+ 4
Mírian Fonkam I did get this certificate about one year ago. From time to time coming some changes or mistakes into the app. But at the end the most important think is that you find out the next solution yourself, without looking in another code before. Of course if you like you can compare your solution with another and analyse the differences to learn more. Happy coding.
21st Mar 2021, 10:40 PM
JaScript
JaScript - avatar
+ 4
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape{ public int area2; public Square(int x) { width=x; } public void area() { area2=width*width; System.out.println(area2); } } class Circle extends Shape { public double area1; public Circle(int y) { width=y; } public void area() { area1=Math.PI*width*width; System.out.println(area1); } } 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 Jun 2021, 3:42 PM
Dharmi Sri
Dharmi Sri - avatar
+ 3
JaScript I think this Code Question should be modified in order to validate results that are approximate. I had to play around with the order of multiplication many times before my code passed all the tests.
21st Mar 2021, 8:37 PM
Mírian Fonkam
Mírian Fonkam - avatar
+ 2
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Circle { int x; public Circle(int x){this.x=x;} public void area(){ System.out.println(Math.PI*x*x); } } class Square { int x; public Square(int x){this.x=x;} public void area(){ System.out.println(x*x); } } 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 Feb 2022, 8:44 AM
Vaibhav
Vaibhav - avatar
+ 1
This trick can be used to achieve better calculation accuracy in certain cases. For example, the difference between the following calculations: x = c / b * a; y = c * a / b; https://code.sololearn.com/clX0vbEhjGuM/?ref=app
20th Jan 2021, 9:10 PM
JaScript
JaScript - avatar
+ 1
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(){ int a = width * width; System.out.println(a); } } class Circle extends Shape{ Circle(int y){ width = y; } public void area(){ double d = Math.PI * (double) width * width; System.out.println(d); } } 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(); } }
15th Jun 2021, 4:39 AM
黄海登
黄海登 - avatar
0
Thanks a lot for the explanation :)
5th Jan 2021, 11:41 AM
Efrat Harel
0
System.out.println(Math.PI* (double)width*width // * Math.PI // not at the end );
16th Apr 2021, 8:16 AM
Dmitry Lezin
Dmitry Lezin - avatar
0
The failed test cases can be possible maybe bcoz of all below reasons, * You may not mentioned "this" keyword to variables that are used in abstract classes. * You need not to bring math functions into the formula for circle or square. * Ensure that you declare proper data type casting. If you follow this, the test case will be correct, still stuck with one or other, can message me here, I can try to help you out in sharing my code.
18th May 2021, 10:41 AM
Haryish
0
Truth is simple, double numbers make troubles. This is exact example of it - they said you have to use "pi * r * r", where r is int -> changing order of variables change last digit of result -> that's all, just swap variables (and devs of sololearn could make it check just few digits after comma)
1st Aug 2021, 1:28 PM
nicolas turek
nicolas turek - avatar
0
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here 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(); } } class Square extends Shape { Square (int x) { this.width = x; } public void area() { System.out.println(this.width*width); } } class Circle extends Shape { Circle (int y) { this.width = y; } public void area() { float S = (float)width; System.out.println(Math.PI*this.width*this.width); } }
16th Sep 2021, 1:29 PM
Linh Phạm
Linh Phạm - avatar
0
I had the same problem. Now I know it was the order of product, but I yet get why it changes the results. After all, the order of factores doesn't affect the result, right?
9th Apr 2022, 12:40 PM
João Lucas Mayrinck D'Oliveira
- 1
Thanks, it did pass it, but I don't understand why my code didn't work. It doesn't seem that the differences are major. Can you explain to me what was wrong with my code? Thanks
31st Dec 2020, 2:05 PM
Efrat Harel