diff --git a/QUIZ/Circle.java b/QUIZ/Circle.java new file mode 100644 index 0000000..433e341 --- /dev/null +++ b/QUIZ/Circle.java @@ -0,0 +1,38 @@ +package QUIZ; + +public class Circle extends Shape { + + protected double radius; + + public Circle() { + super(); + radius = 1.0; + } + public Circle(double radius) { + super(); + this.radius = radius; + } + public Circle(double radius, String color, boolean filled) { + super(color, filled); + this.radius = radius; + } + public double getRadius() { + return radius; + } + public void setRadius(double radius) { + this.radius = radius; + } + @Override + public double getArea() { + return radius*radius*Math.PI; + } + @Override + public double getPerimeter() { + return 2 * radius * Math.PI; + } + @Override + public String toString() { + return String.format("Circle dengan radius = %f, merupakan subclas dari %s" + , radius, super.toString()); + } +} \ No newline at end of file diff --git a/QUIZ/Rectangle.java b/QUIZ/Rectangle.java new file mode 100644 index 0000000..f89a3ee --- /dev/null +++ b/QUIZ/Rectangle.java @@ -0,0 +1,49 @@ + +package QUIZ; + +public class Rectangle extends Shape{ + + protected double width; + protected double length; + + public Rectangle() { + super(); + width = 1.0; + length = 1.0; + } + public Rectangle(double width, double length) { + super(); + this.width = width; + this.length = length; + } + public Rectangle(double width, double length, String color, boolean filled){ + super(color, filled); + this.width = width; + this.length = length; + } + public double getWidth() { + return width; + } + public void setWidth(double width) { + this.width = width; + } + public double getLength() { + return length; + } + public void setLength(double length) { + this.length = length; + } + @Override + public double getArea() { + return width * length; + } + @Override + public double getPerimeter() { + return 2 * (width+length); + } + @Override + public String toString() { + return String.format("Rectangle dengan lebar = %f dan panjang = %f, merupakan subclass dari %s" + , width, length,super.toString()); + } +} diff --git a/QUIZ/Shape.java b/QUIZ/Shape.java new file mode 100644 index 0000000..05c6738 --- /dev/null +++ b/QUIZ/Shape.java @@ -0,0 +1,40 @@ +package QUIZ; + +public abstract class Shape { + + protected String color; + protected boolean filled; + + public Shape() { + color = "red"; + filled =true; + } + + public Shape(String color, boolean filled) { + this.color = color; + this.filled = filled; + } + + public String getColor() { + return color; + } + public void setColor(String color) { + this.color = color; + } + + public boolean isFilled(){ + return filled; + } + public void setFilled(boolean filled) { + this.filled = filled; + } + + abstract public double getArea(); + abstract public double getPerimeter(); + + @Override + public String toString() { + return String.format("Shape dengan warna %s dan %s", + color, (filled ? "terisi" : "tidak terisi")); + } +} \ No newline at end of file diff --git a/QUIZ/Square.java b/QUIZ/Square.java new file mode 100644 index 0000000..512edf4 --- /dev/null +++ b/QUIZ/Square.java @@ -0,0 +1,37 @@ + +package QUIZ; + +public class Square extends Rectangle { + + public Square() { + super(); + } + public Square(double side) { + super(side, side); + } + public Square(double side, String color, boolean filled) { + super(side, side, color, filled); + } + public double getSide() { + return getWidth(); + } + public void setSide(double side) { + setWidth(side); + setLength(side); + } + @Override + public void setWidth(double side) { + super.setWidth(side); + super.setLength(side); + } + @Override + public void setLength(double side) { + super.setWidth(side); + super.setLength(side); + } + @Override + public String toString() { + return String.format("Square dengan sisi = %f , merupakan subclass dari %s" + , width, super.toString()); + } +} diff --git a/QUIZ/Test.java b/QUIZ/Test.java new file mode 100644 index 0000000..12cf96c --- /dev/null +++ b/QUIZ/Test.java @@ -0,0 +1,59 @@ + +package QUIZ; + +public class Test { + public static void main(String[] args) { + + Shape shape1 = new Circle(5.5, "RED", true); // Upcast Circle ke Shape + System.out.println(shape1); + System.out.println(shape1.getArea()); + System.out.println(shape1.getPerimeter()); + System.out.println(shape1.getColor()); + System.out.println(shape1.isFilled()); + System.out.println("\n"); + + Circle circle1 = (Circle)shape1; // Downcast back ke Circle + System.out.println(circle1); + System.out.println(circle1.getArea()); + System.out.println(circle1.getPerimeter()); + System.out.println(circle1.getColor()); + System.out.println(circle1.isFilled()); + System.out.println(circle1.getRadius()); + System.out.println("\n"); + + Shape shape2 = new Rectangle(1.0, 2.0, "RED", true); // Upcast + System.out.println(shape2); + System.out.println(shape2.getArea()); + System.out.println(shape2.getPerimeter()); + System.out.println(shape2.getColor()); + System.out.println("\n"); + + Rectangle rectangle1 = (Rectangle)shape2; // downcast + System.out.println(rectangle1); + System.out.println(rectangle1.getArea()); + System.out.println(rectangle1.getColor()); + System.out.println(rectangle1.getLength()); + System.out.println("\n"); + + Shape shape3 = new Square(6.6); // Upcast + System.out.println(shape3); + System.out.println(shape3.getArea()); + System.out.println(shape3.getColor()); + System.out.println("\n"); + + Rectangle rectangle2 = (Rectangle)shape3; //downcast + System.out.println(rectangle2); + System.out.println(rectangle2.getArea()); + System.out.println(rectangle2.getColor()); + System.out.println(rectangle2.getLength()); + System.out.println("\n"); + + Square square1 = (Square)rectangle2; //downcast rectangle2 ke square + System.out.println(square1); + System.out.println(square1.getArea()); + System.out.println(square1.getColor()); + System.out.println(square1.getSide()); + System.out.println(square1.getLength()); + + } +} \ No newline at end of file