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.
Computer scienceProgramming languagesJavaCode organizationObject-oriented programmingClass hierarchiesBuilding class hierarchies
Inheritance
calculating triangle perimeter
Report a typo
Sample Input 1:
4Sample Output 1:
16Sample Input 2:
5Sample Output 2:
25Write 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
}
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.