+ 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

10th Jun 2021, 9:30 AM
Sayantan Saha
Sayantan Saha - avatar
3 odpowiedzi
+ 2
Sayantan Saha Show your attempts.
10th Jun 2021, 10:04 AM
A͢J
A͢J - avatar
- 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; } }
12th Jun 2021, 3:07 AM
Juan Andres Vera Alvarez
Juan Andres Vera Alvarez - avatar
- 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()); } }
12th Jun 2021, 3:08 AM
Juan Andres Vera Alvarez
Juan Andres Vera Alvarez - avatar