+ 1
Anybody fix this code ?
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } class Square extends Shape{ void area (){ width= x; System.out.println(width*width); } } class Circle extends Shape{ void area(){ width = y; System.out.println(Math.PI* width * width ); } } //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(); Square a = new Square(x); Circle b = new Circle(y); a.area(); b.area(); } }
2 Answers
+ 2
Please feel free to tag me if you got any doubts about how I fixed your code (I used constructors BTW) =)
https://code.sololearn.com/c2f9npXhtxud/?ref=app
0
abstract class Shape {
int width;
double widt;
abstract void area();
}
//вводиŃĐľ кОд ŃŃĐ´Đ°
class Square extends Shape{
Square(int width){
width *= width;
this.width = width;
}
void area(){
System.out.println(width);
}
}
class Circle extends Shape{
Circle(double width){
width *= width * Math.PI;
this.widt = width;
}
void area(){
System.out.println(widt);
}
}