Skip to content

Commit

Permalink
sfvxjf vcvsvvd cc
Browse files Browse the repository at this point in the history
  • Loading branch information
FaisByan committed Nov 12, 2024
1 parent b1b0658 commit 14da53a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 76 deletions.
12 changes: 7 additions & 5 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package core.basesyntax;

public class Circle extends Figure {
private final double radius;
class Circle extends Figure {
private double radius;

public Circle(String color, double radius) {
super(color);
Expand All @@ -15,7 +15,9 @@ public double getArea() {

@Override
public void draw() {
System.out.println("Figure: circle, area: " + getArea() + " sq. units, radius: "
+ radius + " units, color: " + color);
System.out.println("Circle [color=" +
color + ", radius=" +
radius + ", area=" +
getArea() + "]");
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.util.Random;

public class ColorSupplier {
private static final String[] COLORS = {"red", "green", "blue", "yellow", "purple", "orange"};
class ColorSupplier {
private static final String[] COLORS = {"Red",
"Green", "Blue", "Yellow",
"Purple", "Black", "White"};

public String getRandomColor() {
Random random = new Random();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package core.basesyntax;

public abstract class Figure {
abstract class Figure {
protected String color;

public Figure(String color) {
this.color = color;
}

public abstract double getArea();

public abstract void draw();
}
48 changes: 24 additions & 24 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

import java.util.Random;

public class FigureSupplier {

private static final int FIGURE_TYPES_COUNT = 5; // Number of figure types
private static final String[] COLORS = {"Red", "Blue", "Green", "Yellow", "Orange", "Purple", "Pink"}; // List of possible colors
private final Random random = new Random();

// Method to get a random color
public String getRandomColor() {
int index = random.nextInt(COLORS.length); // Get a random index from the COLORS array
return COLORS[index];
}
class FigureSupplier {
private ColorSupplier colorSupplier = new ColorSupplier();
private Random random = new Random();

public Figure getRandomFigure() {
int index = random.nextInt(FIGURE_TYPES_COUNT); // Randomly select a figure type

return switch (index) {
case 0 -> new Square(getRandomColor(), random.nextDouble() * 10); // Use getRandomColor here
case 1 -> new Rectangle(getRandomColor(), random.nextDouble() * 10, random.nextDouble() * 10);
case 2 -> new RightTriangle(getRandomColor(), random.nextDouble() * 10, random.nextDouble() * 10);
case 3 -> new Circle(getRandomColor(), random.nextDouble() * 10);
case 4 ->
new IsoscelesTrapezoid(getRandomColor(), random.nextDouble() * 10, random.nextDouble() * 10, random.nextDouble() * 10);
default -> null;
};
int figureType = random.nextInt(5);
String color = colorSupplier.getRandomColor();
switch (figureType) {
case 0:
return new Square(color, random.nextDouble() *
10 + 1);
case 1:
return new Rectangle(color, random.nextDouble() *
10 + 1, random.nextDouble() * 10 + 1);
case 2:
return new RightTriangle(color, random.nextDouble() *
10 + 1, random.nextDouble() * 10 + 1);
case 3:
return new Circle(color, random.nextDouble() * 10 + 1);
default:
return new IsoscelesTrapezoid(color,
random.nextDouble() *
10 + 1, random.nextDouble() *
10 + 1, random.nextDouble() * 10 + 1);
}
}

// Method to return a default figure (white circle with radius 10)
public Figure getDefaultFigure() {
return new Circle("White", 10); // Default color is white, default radius is 10
return new Circle("White", 10);
}
}
27 changes: 14 additions & 13 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package core.basesyntax;

public class IsoscelesTrapezoid extends Figure {
private final double base1;
private final double base2;
private final double height;
class IsoscelesTrapezoid extends Figure {
private double topBase;
private double bottomBase;
private double height;

public IsoscelesTrapezoid(String color, double base1, double base2, double height) {
public IsoscelesTrapezoid(String color, double topBase, double bottomBase, double height) {
super(color);
this.base1 = base1;
this.base2 = base2;
this.topBase = topBase;
this.bottomBase = bottomBase;
this.height = height;
}

@Override
public double getArea() {
return 0.5 * (base1 + base2) * height;
return 0.5 * (topBase + bottomBase) * height;
}

@Override
public void draw() {
System.out.println("Figure: trapezoid, area: " + getArea() + " sq. units, base1: " + base1
+ " units, base2: " + base2 + " units, height: "
+ height + " units, color: " + color);

System.out.println("IsoscelesTrapezoid [color=" +
color + ", topBase=" +
topBase + ", bottomBase=" +
bottomBase + ", height=" +
height + ", area=" + getArea() + "]");
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[6];

// First half with random parameters
// Generate random figures for the first half of the array
for (int i = 0; i < figures.length / 2; i++) {
figures[i] = figureSupplier.getRandomFigure();
}

// Second half with default parameters
// Use default figure for the second half of the array
for (int i = figures.length / 2; i < figures.length; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}

// Display all figures
// Draw all figures
for (Figure figure : figures) {
figure.draw();
}
}
}
}
21 changes: 11 additions & 10 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package core.basesyntax;

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

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

@Override
public double getArea() {
return length * width;
return width * height;
}

@Override
public void draw() {
System.out.println("Figure: rectangle, area: " + getArea()
+ " sq. units, length: " + length + " units, width: "
+ width + " units, color: " + color);
System.out.println("Rectangle [color=" +
color + ", width=" + width +
", height=" + height +
", area=" + getArea() + "]");
}
}
}
15 changes: 8 additions & 7 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private final double firstLeg;
private final double secondLeg;
class RightTriangle extends Figure {
private double firstLeg;
private double secondLeg;

// Add an empty line before constructor
public RightTriangle(String color, double firstLeg, double secondLeg) {
super(color);
this.firstLeg = firstLeg;
Expand All @@ -18,7 +17,9 @@ public double getArea() {

@Override
public void draw() {
System.out.println("Figure: right triangle, area: " + getArea() + " sq. units, firstLeg: "
+ firstLeg + " units, secondLeg: " + secondLeg + " units, color: " + color);
System.out.println("RightTriangle [color=" +
color + ", firstLeg=" + firstLeg +
", secondLeg=" + secondLeg + ", area=" +
getArea() + "]");
}
}
}
18 changes: 9 additions & 9 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package core.basesyntax;

public class Square extends Figure {
private final double side;
class Square extends Figure {
private double sideLength;

public Square(String color, double side) {
public Square(String color, double sideLength) {
super(color);
this.side = side;
this.sideLength = sideLength;
}

@Override
public double getArea() {
return side * side;
return sideLength * sideLength;
}

@Override
public void draw() {
System.out.println("Figure: square, area: " + getArea() + " sq. units, side: " + side
+ " units, color: " + color);

System.out.println("Square [color=" +
color + ", sideLength=" +
sideLength + ", area=" +
getArea() + "]");
}
}

0 comments on commit 14da53a

Please sign in to comment.