calculating triangle perimeter

Report a typo

Create a base class called Polygon with methods to set and get the length of sides. Then create a derived class called Square that inherits from Polygon and adds a method to calculate the area of the square. Given the side length of a square, calculate its area and print the result. You need to scan a single integer value representing the side length of the square and print the area of the square.

Sample Input 1:

4

Sample Output 1:

16

Sample Input 2:

5

Sample Output 2:

25
Write a program in Java 17
import java.util.Scanner;

public class Main {

// Base class Polygon
static class Polygon {
private int sideLength;

// Method to set the length of sides
public void setSideLength(int sideLength) {
this.sideLength = sideLength;
}

// Method to get the length of sides
public int getSideLength() {
return sideLength;
}
}

// Derived class Square

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sideLength = scanner.nextInt();

// Create an instance of Square

// Set the side length

// Calculate and print the area
}
}
___

Create a free account to access the full topic