+ 3
Problem with code : Shapes in Java lesson
I can resolve the problem. I got the output expected but it says that the hidden test case 3 is not the same as my result. Help
8 ответов
+ 5
Post your code here so that we can help ...
+ 5
May be u missing something read instructions properly and mention your code here so it will be easy for community to debug your code
+ 2
I fixed it. For some reason just writing:
this.width * Math.PI * this.width
Instead of :
this.width*this.width * Math.PI
And adding @Override before the area method fixed it lol
+ 1
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();//almacena datos para Square
int y = sc.nextInt();// almacena datos para circle
Square a = new Square(x); //debe generar el area del cuadrado (de la anchura)
Circle b = new Circle(y);//area del circulo dado
a.area();
b.area();
}
}
public class Square extends Shape{
public Square(int width){
this.width=width;
}
@Override
public void area(){
System.out.println(this.width = this.width* this.width);
}
}
public class Circle extends Shape{
public Circle(int a){
this.width=a;
}
@Override
public void area(){
System.out.println(Math.PI*(double) width*width);
}
}
0
This is the code. Thanks
https://code.sololearn.com/cdsrW4a53OO9/?ref=app
0
Same here. Did you solve it?
0
System.out.println(Math.PI*
(double)width*width
// * Math.PI // not at the end
);
0
import java.util.Scanner;
abstract class Shape {
int width;
abstract void area();
}
class Square extends Shape{
public int area2;
public Square(int x)
{
width=x;
}
public void area()
{
area2=width*width;
System.out.println(area2);
}
}
class Circle extends Shape {
public double area1;
public Circle(int y)
{
width=y;
}
public void area()
{
area1=Math.PI*width*width;
System.out.println(area1);
}
}
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();
}
}