+ 1
What will be the solution code for 'Shape' class be in Java course?
I tried many trials but couldn't succeed. Please help me
3 Respuestas
+ 2
Sayantan Saha
Show your attempts.
- 1
you create an abstract class called shape, and two new functions inherit the area from it, which are circle and square, in each of these classes, we create its constructor and a method that returns its area, either of the square or the circle, and finally in the main call these functions from data provided by the user.
the solution
import java.util.Scanner;
abstract class Shape {
int width;
abstract void area();
}
class Square{
int x;
public Square(int x){
this.x = x;
}
public int area(){
return x*x;
}
}
class Circle{
int x;
public Circle(int x){
this.x = x;
}
public double area(){
return Math.PI*x*x;
}
}
- 1
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);
System.out.println(a.area());
System.out.println(b.area());
}
}