+ 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!
12 Answers
+ 6
Kamil can you save your code on SL Playground and link here ,please.
+ 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.
+ 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.
+ 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.
+ 2
Sure thing!
https://code.sololearn.com/cXCDtBMxc3KI/?ref=app
+ 2
Kamil PoniedziaĆek
Do
Math.PI * width * width
Instead of
width * width * Math.PI
+ 2
JaScript wow, I don't know what's the case, but this worked xD thank you very much!!
+ 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.
+ 2
Got it.
+ 1
Thanks for answer! I've worked with this.value in operators, but I've tried your hint. Unfortunately, problem is still here. :/
+ 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.
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();
}
}