0

May I know the error in this program.

import java.util.Scanner; import java.lang.Math; abstract class Shape { int width; abstract void area(); } //your code goes here public Class Square extends Shape { Square(int z){ int width = z; } void area (){ int A= width * width; System.out.println(A ); } } public Class Circle extends Shape { Circle(int w){ int width = w; } void area (){ double b = Math.PI *(width *width); System.out.println(b); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }

7th May 2021, 5:06 PM
zaid
zaid - avatar
24 Respostas
+ 3
zaid in the area method in the circle class remove the parentheses, for some reason it causes a representation error because of the nature of doubles. Make it just: Math.PI*width*width Your math is correct, doubles are just prone to be a little off sometimes
7th May 2021, 6:05 PM
Odyel
Odyel - avatar
+ 2
Karthik Reddy Thotamgari Your 3rd code declares a local instance of width in the constructor and therefore won't assign the initializing value to the class instance of width. Only the local copy will be assigned and then dumped from the stack when leaving the constructor. In my previous response, I intentionally specified applying the change to your 2nd code to avoid confusion. I think that was overlooked because your 3rd code is slightly different from your 2nd code. To make this clearer, I copied your 2nd code and commented out lines 9 and 22. That's the equivalent of my original and subsequent answers. https://code.sololearn.com/ci6FbSzo1fcR/?ref=app I'm hoping you finally see my point. If you don't, I encourage you to revisit my original explanations and give it some time to sink in. Otherwise, the effort to explain something this simple has well exceeded the very little time I have to spare.
7th May 2021, 7:00 PM
David Carroll
David Carroll - avatar
+ 2
Odyel Good call on the floating point arithmetic causing the issue. It wasn't originally clear that this was for a code coach. So I simply focused on getting it to compile and run based on the existing code.
7th May 2021, 7:01 PM
David Carroll
David Carroll - avatar
+ 1
The keyword "class" should be with a lowercase "c". You have an uppercase "C". The Square and Circle classes refer to width variables that are declared as locally scoped to the constructor and area method. These need to refer to the class instance property: this.width
7th May 2021, 5:23 PM
David Carroll
David Carroll - avatar
+ 1
Odyel ohh yeah.. That works.. Thank you...
7th May 2021, 6:10 PM
zaid
zaid - avatar
+ 1
David Carroll yeah I haven't noticed that thanks
8th May 2021, 12:24 AM
kreddyt
kreddyt - 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 { int z; Square(int z){ this.z=z; } void area (){ int A= z * z; System.out.println(A); } } class Circle extends Shape { int w; Circle(int w){ this.w= w; } void area (){ double b = Math.PI *(w * w); System.out.println(b); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }
7th May 2021, 5:25 PM
kreddyt
kreddyt - avatar
0
David Carroll yeah that's not only the case when entered the input it shows 0 and 0.0 I think I can help with my code
7th May 2021, 5:26 PM
kreddyt
kreddyt - avatar
0
Karthik Reddy Thotamgari My recommendations would have resolved the issue. There's no need to declare int z; and int w; in their respective classes. int width; is already declared in the abstract base class. So... the code should actually refer to this.width in the constructor and area(int) method.
7th May 2021, 5:35 PM
David Carroll
David Carroll - avatar
0
David Carroll give a try bro ull get to know
7th May 2021, 5:42 PM
kreddyt
kreddyt - avatar
0
import java.util.Scanner; import java.lang.Math; abstract class Shape { int width; abstract void area(); } //your code goes here public class Square extends Shape { int width ; Square(int z){ width = z; } void area (){ int A= width * width; System.out.println(A ); } } public class Circle extends Shape { int width ; Circle(int w){ width = w; } void area (){ double b = Math.PI *(width *width); System.out.println(b); } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } } This works
7th May 2021, 5:42 PM
kreddyt
kreddyt - avatar
0
Bro getting error in test case 3..
7th May 2021, 5:46 PM
zaid
zaid - avatar
0
Karthik Reddy Thotamgari getting error in test case 3..
7th May 2021, 5:47 PM
zaid
zaid - avatar
0
Karthik Reddy Thotamgari I wasn't disputing your first code didn't work. Only that it was unnecessary to declare int z and int w in those sub classes. The point is to leverage the base property that is inherited from the parent abstract class. If this was implementing an interface, then it would make sense to declare int width in your sub classes as seen in your 2nd code example. Otherwise, my original answer is the correct answer in the context of this issue.
7th May 2021, 5:48 PM
David Carroll
David Carroll - avatar
0
David Carroll dude I'm just making it simple with the limited no of variables
7th May 2021, 5:49 PM
kreddyt
kreddyt - avatar
0
zaid bruh which code you've tried?
7th May 2021, 5:52 PM
kreddyt
kreddyt - avatar
0
Karthik Reddy Thotamgari yours same code..
7th May 2021, 5:53 PM
zaid
zaid - avatar
0
First or second?
7th May 2021, 5:53 PM
kreddyt
kreddyt - avatar
0
Karthik Reddy Thotamgari If you removed the line below: int width; From the Square and Circle classes in your 2nd code, then it would be simplified even further and consistent with my original answer. šŸ˜‰
7th May 2021, 5:55 PM
David Carroll
David Carroll - avatar
0
Karthik Reddy Thotamgari your both codes gives error in test 3..
7th May 2021, 5:57 PM
zaid
zaid - avatar