Java Programming Exercise: Polygon Structure Explained

Опубликовано: 25 Июнь 2026
на канале: NET SAMBATH Rean IT
2
1

*This's file Class Polygon (Main Class)
package inheritanceDemo;

import java.util.Scanner;

//import javax.swing.plaf.synth.SynthOptionPaneUI;

public class Polygon {
Scanner scan = new Scanner(System.in);
double width, height;
void showWidth (double w) {

}
void showHeight(double h) {

}
}
class Rectangle extends Polygon{
void rectInput() {
System.out.print("Input rect width :");
width = scan.nextDouble();
System.out.print("Input rect hieght :");
height = scan.nextDouble();
}

void rectShow() {
System.out.print("\nRect info : \n");
System.out.println("Rect width is : " + width + " metres");
System.out.println("Rect height is : " + height + " metres");
}
}
class Triangle extends Polygon {
void triInput() {
System.out.print("Input tri width :");
width = scan.nextDouble();
System.out.print("Input tri hieght :");
height = scan.nextDouble();
}
void triShow() {
System.out.print("\nTri info : \n");
System.out.println("Tri info width is :" + width + " metres");
System.out.println("Tri height is : " + height + " metres");
}
}

*This's file Class PolygonsDemo (sub class)
package inheritanceDemo;

public class PolygonsDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle rect = new Rectangle();
Triangle tri = new Triangle(); // can tri by class file Polygon
rect.rectInput();
tri.triInput();
rect.rectShow();
tri.triShow();

}

}

Thanks for support me!