Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QUIZ2-2041723004-M. Shafriza ladiyan #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions QUIZ/Circle.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
49 changes: 49 additions & 0 deletions QUIZ/Rectangle.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
40 changes: 40 additions & 0 deletions QUIZ/Shape.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
37 changes: 37 additions & 0 deletions QUIZ/Square.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
59 changes: 59 additions & 0 deletions QUIZ/Test.java
Original file line number Diff line number Diff line change
@@ -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());

}
}