May I know the error in this program. | SoloLearn: Learn to code for FREE!

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

5/7/2021 5:06:12 PM

zaid

24 Answers

New Answer

+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

+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.

+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.

+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

+1

Odyel ohh yeah.. That works.. Thank you...

+1

David Carroll yeah I haven't noticed that thanks

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

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

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.

0

David Carroll give a try bro ull get to know

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

0

Bro getting error in test case 3..

0

Karthik Reddy Thotamgari getting error in test case 3..

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.

0

David Carroll dude I'm just making it simple with the limited no of variables

0

zaid bruh which code you've tried?

0

Karthik Reddy Thotamgari yours same code..

0

First or second?

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. 😉

0

Karthik Reddy Thotamgari your both codes gives error in test 3..