Skip to content

Instantly share code, notes, and snippets.

@zapkub
Created March 27, 2021 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zapkub/888084652cafbae969cb1d13e951c992 to your computer and use it in GitHub Desktop.
Save zapkub/888084652cafbae969cb1d13e951c992 to your computer and use it in GitHub Desktop.
package com.company;
// สร้างโปรแกรมที่สามารถ วาด * ออกมาเป็นรูป เลขาคณิต 3 แบบ
// - สี่เหลี่ยมจตุรัส
// - สามเหลี่ยม
// - ข้าวหลามตัด
// โดยที่มี input เป็น int ที่ 0 < n < 20
class Shape {
public String output = "";
}
class DotShape extends Shape {
public void draw(int size) {
int centerpos = (size - 1 ) / 2;
for (int i = 0; i < size; i ++) {
if (i == centerpos) {
this.output = this.output + "*";
} else {
this.output = this.output + "_";
}
}
}
}
class Renderer {
public void Render(Shape shape) {
if (shape == null ) {
System.out.println("ERROR SHAPE IS NULL");
} else {
System.out.println(shape.output);
}
}
}
public class Main {
public static void main(String[] args) {
int input = 5; // Odd number only
String shapeType = "DIAMOND"; // DOT, RECT, TRI, DIAMOND
Renderer renderer = new Renderer();
Shape shape = null;
if (shapeType == "DOT") {
DotShape dotshape = new DotShape();
dotshape.draw(input);
shape = dotshape;
}
renderer.Render(shape);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment