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

add_home_work_advanced_oop #1781

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions src/main/java/classes_inheritors/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package classes_inheritors;

public class Circle extends Figure {
private double radius;
public Circle (Color color, double radius) {
super(color);
this.radius = radius;
}

@Override
public String toString() {
return "Circle";
}
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Circle + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("radius: " + this.radius + " sq. units" + ", ")
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
5 changes: 5 additions & 0 deletions src/main/java/classes_inheritors/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package classes_inheritors;

public enum Color {
RED, GREEN, BLUE, YELLOW, VIOLET, BLACK, WHITE;
}
11 changes: 11 additions & 0 deletions src/main/java/classes_inheritors/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package classes_inheritors;

import java.util.Random;

public class ColorSupplier {
public Color getRandomColor() {
Color[] colors = Color.values();
Random random = new Random();
return colors[random.nextInt(colors.length)];
}
}
5 changes: 5 additions & 0 deletions src/main/java/classes_inheritors/Drowable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package classes_inheritors;

public interface Drowable {
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
String draw();
}
19 changes: 19 additions & 0 deletions src/main/java/classes_inheritors/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package classes_inheritors;

public abstract class Figure implements Drowable {
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
private Color color;

Figure (Color color) {
this.color = color;
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

public abstract double calculateArea();
}
32 changes: 32 additions & 0 deletions src/main/java/classes_inheritors/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package classes_inheritors;

import java.util.Random;

public class FigureSupplier {
public static final double DEFAULT_CIRCLE_RADIUS = 10.0;
Random random = new Random();
ColorSupplier colorSupplier = new ColorSupplier();

private double getPositiveRandomNumber() {
return 1 + random.nextDouble(10);
}

public Figure getDefaultFigure() {
return new Circle(Color.WHITE, DEFAULT_CIRCLE_RADIUS);
}

public Figure getRandomFigure() {
Figures[] figures = Figures.values();
Color randomColor = colorSupplier.getRandomColor();
Figures randomFigureName = figures[random.nextInt(figures.length)];
double randomDouble = getPositiveRandomNumber();
return switch (randomFigureName) {
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
case Square -> new Square(randomColor, randomDouble);
case Rectangle -> new Rectangle(randomColor, randomDouble, randomDouble);
case Circle -> new Circle(randomColor, randomDouble);
case Right_triangle -> new RightTriangle(randomColor, randomDouble, randomDouble);
case Isosceles_Trapezoid ->
new IsoscelesTrapezoid(randomColor, randomDouble, randomDouble, randomDouble);
};
}
}
5 changes: 5 additions & 0 deletions src/main/java/classes_inheritors/Figures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package classes_inheritors;

public enum Figures {
Square, Rectangle, Right_triangle, Circle, Isosceles_Trapezoid;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using uppercase letters with underscores for enum constants (e.g., SQUARE, RECTANGLE) to follow Java naming conventions for enums.

}
36 changes: 36 additions & 0 deletions src/main/java/classes_inheritors/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package classes_inheritors;

public class IsoscelesTrapezoid extends Figure {

private double baseTop;
private double baseBottom;
private double height;

public IsoscelesTrapezoid(Color color, double baseTop, double baseBottom, double height) {
super(color);
this.baseTop = baseTop;
this.baseBottom = baseBottom;
this.height = height;
}

@Override
public String toString() {
return "Isosceles trapezoid";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Isosceles_Trapezoid + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("baseTop: " + this.baseTop + " sq. units" + ", ")
.append("baseBottom: " + this.baseBottom + " sq. units" + ", ")
.append("height: " + this.height + " sq. units" + ", ")
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return (baseTop + baseBottom) * 0.5 * height;
}
}
32 changes: 32 additions & 0 deletions src/main/java/classes_inheritors/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package classes_inheritors;

public class Rectangle extends Figure {
private double width;
private double height;

@Override
public String toString() {
return "Rectangle";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Rectangle + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("width: " + this.width + " sq. units" + ", ")
.append("height: " + this.height + " sq. units" + ", ")
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

public Rectangle(Color color, double width, double height) {
super(color);
this.height = height;
this.width = width;
}

@Override
public double calculateArea() {
return width * height;
}
}
31 changes: 31 additions & 0 deletions src/main/java/classes_inheritors/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package classes_inheritors;

public class RightTriangle extends Figure {
private double firstLeg;
private double secondLeg;
public RightTriangle(Color color, double firstLeg, double secondLeg) {
super(color);
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

@Override
public String toString() {
return "right triangle";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Right_triangle + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("firstLeg: " + this.firstLeg + " sq. units" + ", ")
.append("secondLeg: " + this.secondLeg + " sq. units" + ", ")
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return firstLeg * secondLeg * 0.5;
}
}
30 changes: 30 additions & 0 deletions src/main/java/classes_inheritors/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package classes_inheritors;

public class Square extends Figure {
double side;

@Override
public String toString() {
return "Square";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Square + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("side: " + this.side + " sq. units" + ", ")
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

public Square(Color color, double side) {
super(color);
this.setColor(color);
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
this.side = side;
}

@Override
public double calculateArea() {
return side * side;
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/FiguresApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

import classes_inheritors.*;

public class FiguresApp {
private static final int FIGURE_COUNT = 6;
private static final int HALF_COUNT = FIGURE_COUNT / 2;

public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figuresArray = new Figure[FIGURE_COUNT];
for (int elementIndex = 0; elementIndex < FIGURE_COUNT; elementIndex++){
if (elementIndex < HALF_COUNT) {
figuresArray[elementIndex] = figureSupplier.getRandomFigure();
} else {
figuresArray[elementIndex] = figureSupplier.getDefaultFigure();
}
}

for(Figure figure : figuresArray) {
System.out.println(figure.draw());
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

Loading