Can't solve Shapes in Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can't solve Shapes in Java

Hello dear Sololearners! In Java course I really can't solve penultimate task named 'Shapes'. 4/5 tests resulting positive, 3rd one is negative and hidden, so I don't really know what is the problem here. In comment I will post my code Big thanks for any help with this. Greetings!

15th Jul 2021, 11:26 AM
Kamil Poniedziałek
Kamil Poniedziałek - avatar
12 Answers
+ 6
Kamil can you save your code on SL Playground and link here ,please.
15th Jul 2021, 11:30 AM
JaScript
JaScript - avatar
+ 5
I think for circle better will be: Math.PI * this.width * this.width; And in general you should work in the class with this.value … . Please read the lession again, to understand the difference.
15th Jul 2021, 11:44 AM
JaScript
JaScript - avatar
+ 5
Mathematics is nothing general. Mathematics emerges from definitions and concrete proof. I am so sorry and it is nothing personal I ᴀᴍ "Tɪᴍᴇ" . Happy coding continues for you.
15th Jul 2021, 5:44 PM
JaScript
JaScript - avatar
+ 3
Sorry I have to straighten something here. The area of circle is (PI*r*r) and is (r*r* PI) and also is (r*PI*r). This will be learned in basic school. Ok? Please look in your mathe books before you write here such as nonsense thesis. Here are we in digital world. And here are two integer numbers and one double (PI) number. The question is how respectivelly when we convert the numbers with which will be calculated to double type. Why arises the difference is too long to explain. An answer can be found online. The thing is that SoloLearn simply programmed in this order in the task, so it must be followed.
15th Jul 2021, 4:44 PM
JaScript
JaScript - avatar
15th Jul 2021, 11:32 AM
Kamil Poniedziałek
Kamil Poniedziałek - avatar
+ 2
Kamil Poniedziałek Do Math.PI * width * width Instead of width * width * Math.PI
15th Jul 2021, 1:10 PM
A͢J
A͢J - avatar
+ 2
JaScript wow, I don't know what's the case, but this worked xD thank you very much!!
15th Jul 2021, 1:19 PM
Kamil Poniedziałek
Kamil Poniedziałek - avatar
+ 2
Kamil Poniedziałek Previous Statement - In general the area of circle is (PI * r * r) not (r * r * PI). Current Statement - In General we write PI * r * r not r * r * PI Though both will give exact result but there will be minor difference after decimal places. That's why we get test case failed. I have seen this question many times and each time people do same mistake. Edited: because some people didn't understand what I wanted to say.
15th Jul 2021, 2:02 PM
A͢J
A͢J - avatar
+ 2
Got it.
15th Jul 2021, 2:26 PM
Kamil Poniedziałek
Kamil Poniedziałek - avatar
+ 1
Thanks for answer! I've worked with this.value in operators, but I've tried your hint. Unfortunately, problem is still here. :/
15th Jul 2021, 12:11 PM
Kamil Poniedziałek
Kamil Poniedziałek - avatar
+ 1
JaScript I said "In general". It is upto you how you want to write. I have learnt in basic school also and also on the internet. Everywhere PI * r * r is written.
15th Jul 2021, 4:53 PM
A͢J
A͢J - avatar
0
import java.util.Scanner; import java.lang.Math.*; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape { Square(int width){ this.width = width; } void area(){ int area = width*width; System.out.println(area); } } class Circle extends Shape { Circle(int width){ this.width = width; } void area(){ double area = width * width * Math.PI; System.out.println(area); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); sc.close(); Square square = new Square(x); Circle circle = new Circle(y); square.area(); circle.area(); } }
15th Jul 2021, 11:26 AM
Kamil Poniedziałek
Kamil Poniedziałek - avatar