I can’t find the error!!! Exercise Shape in Java course | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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 Answers
+ 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