+ 1
What should I put in my constructor to make this work?
import java.util.Scanner; abstract class Shape { int width; abstract void area(); } //your code goes here class Square extends Shape{ public void area(int width){ System.out.print(width*width); } } 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(); } }
1 ответ
+ 2
You didn't create any constructor:
public class Square extends Shape
{
public Square(int w){
this.width = w;
}
@Override
public void area() {
System.out.println(width * width);
}
}