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

31st Oct 2022, 12:19 PM
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–†
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–† - avatar
12 Antworten
+ 6
Yes. Precision difference exists for other way.. You're welcome..
31st Oct 2022, 1:13 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 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
31st Oct 2022, 12:34 PM
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–†
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–† - avatar
+ 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
31st Oct 2022, 12:48 PM
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–†
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–† - avatar
+ 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...
31st Oct 2022, 1:01 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 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
31st Oct 2022, 12:21 PM
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–†
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–† - avatar
+ 1
Why you printing integer cast value only..? Don't cast. For Circle area: try System.out.print( Math.PI*width*width) ;
31st Oct 2022, 12:27 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 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.
31st Oct 2022, 12:52 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 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
31st Oct 2022, 12:56 PM
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–†
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–† - avatar
+ 1
Ok now itโ€™s work in the form Math.PI*width*width but not in the form width*width*MathPI Thank you โ˜บ๏ธ
31st Oct 2022, 1:10 PM
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–†
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–† - avatar
0
Damn rounding!!! ๐Ÿ˜‚
31st Oct 2022, 1:16 PM
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–†
๐•ฌ๐–‘๐–Š๐–˜๐–˜๐–†๐–“๐–‰๐–—๐–” ๐•น๐–”๐–ˆ๐–Š๐–—๐–† - avatar
0
Hi
1st Nov 2022, 7:53 AM
Jonayed Ahmed Tamim
Jonayed Ahmed Tamim - avatar
0
๐Ÿฆ๐Ÿฆ
2nd Nov 2022, 11:38 AM
Diego ๐Ÿ‡ฆ๐Ÿ‡ถ
Diego ๐Ÿ‡ฆ๐Ÿ‡ถ - avatar