What is the problem with this java code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the problem with this java code?

import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here public class Square extends Shape{ Square(int width){ this.width =width; } void area() { System.out.println(width*width); } } public class Circle extends Shape{ Circle(int width){ this.width =width; } void area() { System.out.println(width*width*Math.PI); } } 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(); } }

26th Feb 2021, 1:51 PM
Abdelghani
Abdelghani - avatar
8 Answers
+ 2
In void area() { System.out.println(width*width*Math.PI); } Try System.out.println(Math.PI*width*width);
26th Feb 2021, 3:28 PM
Jayakrishna 🇮🇳
+ 2
There's nothing wrong with this code, it runs and does its job. What is the problem?
26th Feb 2021, 2:15 PM
OrHy3
OrHy3 - avatar
+ 2
Add full description or link here. .
26th Feb 2021, 3:21 PM
Jayakrishna 🇮🇳
+ 1
Abdelghani what the problem you are facing...?
26th Feb 2021, 3:02 PM
Jayakrishna 🇮🇳
+ 1
Thank you, I solved it
26th Feb 2021, 3:45 PM
Abdelghani
Abdelghani - avatar
0
When i run it into the quiz "shapes" of java course, it indicate an error for the third test
26th Feb 2021, 3:03 PM
Abdelghani
Abdelghani - avatar
0
You are working on a graphical app, which includes multiple different shapes. The given code declares a base Shape class with an abstract area() method and a width attribute. You need to create two Shape subclasses, Square and Circle, which initialize the width attribute using their constructor, and define their area() methods. The area() for the Square class should output the area of the square (the square of the width), while for the Circle, it should output the area of the given circle (PI*width*width). The code in main creates two objects with the given user input and calls the area() methods. Sample Input: 5 2 Sample Output: 25 12.566370614359172
26th Feb 2021, 3:23 PM
Abdelghani
Abdelghani - avatar
0
For this task, 3 test cases are true and 2 cases are false.. i don't know why it gives false output
26th Feb 2021, 3:25 PM
Abdelghani
Abdelghani - avatar