Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Models.java:
- package com.company;
- abstract class Shape
- {
- String color;
- Boolean filled;
- public Shape(String color, Boolean filled)
- {
- this.color = color;
- this.filled = filled;
- }
- public Shape() { }
- String getColor()
- {
- return color;
- }
- void setColor(String color)
- {
- this.color = color;
- }
- Boolean isFilled()
- {
- return filled;
- }
- void setFilled(Boolean isFilled)
- {
- filled = isFilled;
- }
- abstract double getArea();
- abstract double getPerimeter();
- }
- class Circle extends Shape
- {
- double radius;
- public Circle() { }
- public Circle(double radius)
- {
- this.radius = radius;
- }
- public Circle(double radius, String color, Boolean isFilled)
- {
- this.radius = radius;
- super.color = color;
- super.filled = isFilled;
- }
- @Override
- double getArea()
- {
- return Math.PI * radius * radius;
- }
- @Override
- double getPerimeter()
- {
- return 2 * Math.PI * radius;
- }
- @Override
- public String toString()
- {
- return String.format("Circle:\nS=%f\nP=%f\nColor=%s\nIsFilled?=%s",
- getArea(), getPerimeter(), getColor(), isFilled());
- }
- }
- class Rectangle extends Shape
- {
- double width;
- double length;
- public Rectangle() { }
- public Rectangle(double width, double length)
- {
- this.width = width;
- this.length = length;
- }
- public Rectangle(double width, double length, String color, Boolean isFilled)
- {
- this.width = width;
- this.length = length;
- super.color = color;
- super.filled = isFilled;
- }
- 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
- double getArea() {
- return width * length;
- }
- @Override
- double getPerimeter() {
- return 2 * width + 2 * length;
- }
- @Override
- public String toString()
- {
- return String.format("Rectangle:\nS=%f\nP=%f\nColor=%s\nIsFilled?=%s",
- getArea(), getPerimeter(), getColor(), isFilled());
- }
- }
- // Main.java:
- package com.company;
- class Main
- {
- public static void main(String[] args)
- {
- var shapes = new Shape[10];
- for (int i = 0; i < 10; i++)
- {
- if (i % 2 == 0)
- {
- shapes[i] = new Circle(Math.random() * 100, "Black", i % 3 == 0);
- }
- else
- {
- shapes[i] = new Rectangle(Math.random() * 100, Math.random() * 100, "Black", i % 3 == 0);
- }
- }
- for (var current : shapes)
- {
- System.out.println(current.toString());
- System.out.println();
- }
- }
- }
- // Output:
- Circle:
- S=21150,609484
- P=515,544758
- Color=Black
- IsFilled?=true
- Rectangle:
- S=593,755608
- P=100,585965
- Color=Black
- IsFilled?=false
- Circle:
- S=24750,825957
- P=557,698890
- Color=Black
- IsFilled?=false
- Rectangle:
- S=1559,316118
- P=166,709883
- Color=Black
- IsFilled?=true
- Circle:
- S=3810,251887
- P=218,817361
- Color=Black
- IsFilled?=false
- Rectangle:
- S=7992,962780
- P=357,641006
- Color=Black
- IsFilled?=false
- Circle:
- S=10894,135395
- P=369,999653
- Color=Black
- IsFilled?=true
- Rectangle:
- S=664,407538
- P=212,718715
- Color=Black
- IsFilled?=false
- Circle:
- S=8592,151006
- P=328,591165
- Color=Black
- IsFilled?=false
- Rectangle:
- S=516,969441
- P=139,738379
- Color=Black
- IsFilled?=true
- Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement