+ 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(); } }
8 Respuestas
+ 2
In
void area() {
System.out.println(width*width*Math.PI);
}
Try
System.out.println(Math.PI*width*width);
+ 2
There's nothing wrong with this code, it runs and does its job. What is the problem?
+ 2
Add full description or link here. .
+ 1
Abdelghani what the problem you are facing...?
+ 1
Thank you, I solved it
0
When i run it into the quiz "shapes" of java course, it indicate an error for the third test
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
0
For this task, 3 test cases are true and 2 cases are false.. i don't know why it gives false output