by CodeChum Admin
Since we are interested with drawing in this quiz (it seems), consider a Shape object. We know that for regular shapes (like Rectangle and Circle), they have properties like area and perimeter. Let's also assume that these Shapes have color (simply a String for the purpose of this quiz) and whether the shapes are filled with this color or not (boolean).
More info here, Important!
https://pastebin.com/hFgYeMFH
public abstract class Shape {
private String color;
private boolean filled;
public Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
public abstract double area();
public abstract double perimeter();
public String getColor() {
return color;
}
public boolean isFilled() {
return filled;
}
}
public class Rectangle extends Shape {
private int length;
private int width;
public Rectangle(int length, int width, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = length;
}
@Override
public double area() {
return width * length;
}
@Override
public double perimeter() {
return 2 * (width + length);
}
@Override
public String toString() {
return "Rectangle: length: " + length + ", width: " + width;
}
}
public class Circle extends Shape {
private int radius;
public Circle(int radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
@Override
public double perimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
return "Circle: radius: " + radius;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
switch (in.nextInt()) {
case 1:
Rectangle rectangle = new Rectangle(in.nextInt(), in.nextInt(), in.next(), in.nextBoolean());
System.out.println(rectangle);
System.out.printf("Area: %.2f\n", rectangle.area());
System.out.printf("Perimeter: %.2f\n", rectangle.perimeter());
if (rectangle.isFilled()) {
System.out.println("Color: " + rectangle.getColor());
}
break;
case 2:
Circle circle = new Circle(in.nextInt(), in.next(), in.nextBoolean());
System.out.println(circle);
System.out.printf("Area: %.2f\n", circle.area());
System.out.printf("Perimeter: %.2f\n", circle.perimeter());
if (circle.isFilled()) {
System.out.println("Color: " + circle.getColor());
}
break;
}
}
}
Comments
Leave a comment