[SOLVED]why error is occurring?? | SoloLearn: Learn to code for FREE!

+12

[SOLVED]why error is occurring??

I am solving a code coach solution in Java course named as Shapes. In this 3rd case is showing error so what is that error this is the code import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int sq= x*x; double c = y * y * Math.PI; System.out.println(sq); System.out.println(c); } } https://code.sololearn.com/cmFhBEprH323/?ref=app

16 Answers

New Answer

+10

See If you are having a problem in making this code using methods and functions I have a simpler way for you SL code coach accepts any way you do the challenge But don't do like this in every problem Just try to make your concepts more clear https://code.sololearn.com/ckzFrsM74Qe7/?ref=app happy coding😄

+10

https://code.sololearn.com/cMNi1OSLxqhS/?ref=app You have to make a second class to extend the abstract class as abstract can't be instantiated so for instantiation we have to make a second class which extends that abstract class and we have to make exactly same method otherwise it will give error See this code this would help you

+6

I've solved all but one Java course code coach. I can not find one called Shapes. Please check name. If PRO, you can not publish details. Message me instead.

+4

Pls add description.. Why there you have abstract class,.. Which is not implemented anywhere..? 💞D€\/!L ||©0de®💞 Add a sample input and output for code you are looking.. Description - what this code is for implemented? Or link ? Edit : for input 8 5 Its output : 64 78.5398133974483 like this. Is it need to cutoff some decimal digits?

+4

The erorr is coming becoz of the classes you need to define a function inside class that return an area And you have leave it blank that whu error is coming

+4

The description of the problem is that you have to build two class of Square and Circle that inherit abstract Shape class which has width variable. In the main method there: Square a = new Square(x) Circle b = new Circle(y) a.area() -> print the area of square b.area() -> print the area of circle So you should code classes inherit Shape class, for example: class Square extends Shape { // build constructor to give width a value Square(x){ width = x } // implement method of area void area(){ System.out.print(width*width) } You can build Circle class with the same concept and print the result of area with Math.PI*width*width May the code help you solve all test cases.

+4

Here's my solution. Please ensure you have attempted the problem before reading it. Thanks and happy coding! https://code.sololearn.com/cSR3ZTnW4E84/?ref=app

+3

I had the same issue where i passed tests 1, 2, 4 & 5 but not 3. Turns out it fails the circle if you put: (width*width*Math.PI) but passes if you put: (Math.PI*width*width) I don't know why it would do that though, order of multiplication doesn't have any effect in real maths.

+1

Samy utomo :Not working bro.

+1

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape{ Square(int x){ width =x; } void area(){ System.out.println(width*width); } } Class Circle extends Shape{ Circle(int y){ width=y; } void area(){ System.out.println(Math.PI*y*y); } } 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(); } } Where is the error in my code‌... Any one help to find the output?

+1

Keith Cooper - - > Exactly, I discovered the same thing.

+1

Integer math and double math are not the same. Putting PI first performs double math which is not as precise.

+1

I failed 3rd test too. But i copied my code to online compiler, executed with negative numbers and it worked with (Math.PI*width*width) and with (width*width*Math.PI) exact the same way. Some bug with inner SoloLearn compiler :<

+1

Since some Test Cases are locked, then it would be great if SoloLearn would provide us with some similar input values that would fail one or more Test Cases, but they should only be provided when a Test Case fails. That would be a great help in solving tasks in a correct way. That would also prevent people from passing a failed Test Case with an "if else statement" as if they were all unlocked.

+1

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape { Square(int x) { width= x; } void area() { System.out.println(width*width); } } class Circle extends Shape { Circle(int x) { width= x; } void area() { System.out.println(Math.PI * width * width); } } public class test { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); sc.close(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }

0

https://en.meming.world/images/en/8/87/Math_Is_Math.jpg