Java Shapes Project | Sololearn: Learn to code for FREE!

+2

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(); } }

12/31/2020 1:16:34 PM

Efrat Harel

14 Answers

New Answer

+3

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.

+4

This will help you: https://code.sololearn.com/cmBCBDKUi9oG/?ref=app

+4

AHA!! the ORDER of the multiplication! That was my issue on the 3rd test case. Thank you for the tip.

+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.

+2

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.

+2

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(); } }

+1

Thanks a lot for the explanation :)

+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

0

System.out.println(Math.PI* (double)width*width // * Math.PI // not at the end );

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.

0

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(); } }

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)

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); } }

-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