- 1
I can’t find the error!!! Exercise Shape in Java course
When I execute the code I can’t pass the 3rd test import java.util.Scanner; abstract class Shape{ int width; abstract void area(); } //your code goes here class Square extends Shape{ public Square(int side){ this.width=side; } public void area(){ System.out.println((int)Math.pow(width,2)); } } class Circle extends Shape{ public Circle(int radius){ this.width=radius; } public void area(){ System.out.println(Math.pow(width,2)*Math.PI); } }
12 ответов
+ 6
Yes. Precision difference exists for other way..
You're welcome..
+ 2
If I don’t cast in the square the output will be double while I’m searching an integer output. I tried to change in Circle area but it doesn’t work
+ 2
This is the exercise Shape in the java course; when I execute the code the expected output is an integer for the square while for the circle there isn’t any limit as output
+ 2
For square area() both ways are same. No difference. Both works.
For, circle area() I just checked in coach, it is working fine as I mentioned. Do check again..
System.out.println(Math.PI*width*width);
If not work, then post your update code with saving playground and share link...
+ 1
public class Program{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int side=sc.nextInt();
int radius=sc.nextInt();
Square square=new Square(side);
Circle circle=new Circle(radius);
square.area();
circle.area();
}
}
That’s the main part
+ 1
Why you printing integer cast value only..? Don't cast.
For Circle area: try
System.out.print( Math.PI*width*width) ;
+ 1
Oh. Ok fine it is. I only find change need in Circle area() output as I mentioned. There will exist a slight precision value difference. Try again my statement.
+ 1
I alteady tried to change in the way you suggested me but it doesn’t work; I tried also change in the square area side*side instead of (int)Math.pow(side,2) but it doesn’t work
+ 1
Ok now it’s work in the form Math.PI*width*width but not in the form width*width*MathPI
Thank you ☺️
0
Damn rounding!!! 😂
0
Hi
0
🦍🦍