abstract public class Shape {
int size;
public Shape(int size) {
this.size = size;
}
abstract public void show();
}
import static java.lang.System.out;
public class Square extends Shape {
public Square(int size) {
super(size);
}
@Override
public void show() {
out.print(" ");
for (int i = 1; i < size; i++) {
out.print("--");
}
out.println();
for (int row = 1; row < size - 1; row++) {
out.print("|");
for (int i = 1; i < size; i++) {
out.print(" ");
}
out.print("|");
out.println();
}
out.print(" ");
for (int i = 1; i < size; i++) {
out.print("--");
}
out.println();
}
}
import static java.lang.System.out;
public class Triangle extends Shape {
public Triangle(int size) {
super(size);
}
@Override
public void show() {
for (int row = 0; row < size; row++) {
for (int i = 0; i < size - row; i++) {
out.print(" ");
}
out.print("/");
for (int i = size - row; i < size + row; i++) {
out.print(" ");
}
out.print("\\");
out.println();
}
out.print(" ");
for (int i = 0; i < size * 2; i++) {
out.print("-");
}
out.println();
}
}
public class Main {
public static void main(String[] args) {
var square1 = new Square(5);
square1.show();
var triangle1 = new Triangle(7);
triangle1.show();
var square2 = new Square(3);
square2.show();
var triangle2 = new Triangle(4);
triangle2.show();
var triangle3 = new Triangle(2);
triangle3.show();
}
}