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

created several Figure and color classes and their suppliers #1729

Open
wants to merge 9 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
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public interface AreaCalculator {

int calculateArea();

}
34 changes: 34 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package core.basesyntax;

public class Circle extends Figure implements AreaCalculator {

private int radius;

public Circle(String color, String name, int radius) {
super(color, name);
this.radius = radius;
}


@Override
public int calculateArea() {
return radius * radius * (int) Math.PI;
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved

}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "radius: "
+ radius
+ ", "
+ "color: "
+ super.getColor();
}
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax;

public enum Color {

YELLOW,
BLUE,
BLACK,
WHITE,
GREEN,
RED

}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {

public String getRandomColor(){
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
Color[] values = Color.values();
int randomIndex = new Random().nextInt(values.length);
return values[randomIndex].toString().toLowerCase();
}

}
48 changes: 48 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package core.basesyntax;

public class Figure {

private String color;
private String name;

public void draw() {
System.out.println(this);
}

public Figure() {
}

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

public String getColor() {
return color;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Figure{"
+ "color='"
+ color
+ ", "
+ "name='"
+ name
+ '\''
+ '}';
}

}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/FigureName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax;

public enum FigureName {

CIRCLE,
ISOSCELES_TRAPEZOID,
RECTANGLE,
SQUARE,
RIGHT_TRIANGLE
}
46 changes: 46 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package core.basesyntax;

import java.util.Locale;
import java.util.Random;

public class FigureSupplier {


public Figure getRandomFigure(){
Figure randomFigure = new Figure();
int randomIndex = new Random().nextInt(5);
switch (randomIndex) {
case 0 -> randomFigure = getRandomCircle();
case 1 -> randomFigure = getIsoscelesTrapezoid();
case 2 -> randomFigure = getRandomRectangle();
case 3 -> randomFigure = getRightTriangle();
case 4 -> randomFigure = getRandomSquare();
}
return randomFigure;
}

public Figure getDefaultFigure(){
return new Circle(Color.WHITE.toString().toLowerCase(), FigureName.CIRCLE.toString().toLowerCase(), 10);
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
}

private Circle getRandomCircle(){
return new Circle(new ColorSupplier().getRandomColor() ,FigureName.CIRCLE.toString().toLowerCase(), new Random().nextInt(101));
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
}

private IsoscelesTrapezoid getIsoscelesTrapezoid(){
return new IsoscelesTrapezoid(new ColorSupplier().getRandomColor() ,FigureName.ISOSCELES_TRAPEZOID.toString().toLowerCase(), new Random().nextInt(101), new Random().nextInt(101), new Random().nextInt(101));
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
}

private Rectangle getRandomRectangle(){
return new Rectangle(new ColorSupplier().getRandomColor() ,FigureName.RECTANGLE.toString().toLowerCase(), new Random().nextInt(101), new Random().nextInt(101));
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
}

private RightTriangle getRightTriangle(){
return new RightTriangle(new ColorSupplier().getRandomColor() ,FigureName.RIGHT_TRIANGLE.toString().toLowerCase(), new Random().nextInt(101));
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
}

private Square getRandomSquare(){
return new Square(new ColorSupplier().getRandomColor() ,FigureName.SQUARE.toString().toLowerCase(), new Random().nextInt(101));
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
}

}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure implements AreaCalculator {

int bottomBase;
int topBase;
int side;

public IsoscelesTrapezoid(String color, String name, int bottomBase, int topBase, int side) {
super(color, name);
this.bottomBase = bottomBase;
this.topBase = topBase;
this.side = side;
}

@Override
public int calculateArea() {
return ((bottomBase + topBase) / 2) * ((int)Math.sqrt((Math.pow(side, 2)) - Math.pow((((Math.pow((bottomBase - topBase), 2)) + (Math.pow(side, 2)) - (Math.pow(side, 2))) / (2 * (bottomBase - topBase))), 2)));
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved

}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "bottomBase: "
+ bottomBase
+ ", "
+ "topBase: "
+ topBase
+ ", "
+ "side: "
+ side
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ ", "
Comment on lines +41 to +42

Choose a reason for hiding this comment

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

There is an extra comma before the 'color' field in the toString method. Consider removing it for better formatting.

+ "color: "
+ super.getColor();
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax;

import java.util.Arrays;

public class Main {

public static void main(String[] args) {
Figure[] figures = new Figure[6];
for (int i = 0; i < 3; i++) {
figures[i] = new FigureSupplier().getRandomFigure();

Choose a reason for hiding this comment

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

Ensure that FigureSupplier.getRandomFigure() does not return a direct instance of Figure if Figure is intended to be a base class or abstract. This aligns with the suggestion to make Figure an abstract class.

}
for (int i = 3; i < 6; i++) {
figures[i] = new FigureSupplier().getDefaultFigure();
}
for (Figure figure : figures) {
figure.draw();
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package core.basesyntax;

public class Rectangle extends Figure implements AreaCalculator {

private int firstLag;
private int secondLag;
Comment on lines +4 to +5

Choose a reason for hiding this comment

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

The variable names firstLag and secondLag might be confusing. Consider renaming them to width and height or length and breadth for better clarity and understanding.


public Rectangle(String color, String name, int firstLag, int secondLag) {
super(color, name);
this.firstLag = firstLag;
this.secondLag = secondLag;
}

@Override
public int calculateArea() {
return firstLag * secondLag;
}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "firstLag: "
+ firstLag
+ ", "
+ "secondLag: "
+ secondLag
+ ", "
+ "color: "
+ super.getColor();
}

}
33 changes: 33 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core.basesyntax;

public class RightTriangle extends Figure implements AreaCalculator{

private int firstLag;

Choose a reason for hiding this comment

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

The variable name firstLag might be confusing. Consider renaming it to base or height for better clarity and understanding, especially since this is a right triangle.


public RightTriangle(String color, String name, int firstLag) {
super(color, name);
this.firstLag = firstLag;
}

@Override
public int calculateArea() {
return (int)(Math.pow(firstLag, 2)*(Math.sqrt(3))/4);
VasylAzarov marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "firstLag: "
+ firstLag
+ ", "
+ "color: "
+ super.getColor();
}

}
33 changes: 33 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core.basesyntax;

public class Square extends Figure implements AreaCalculator {

private int firstLag;

Choose a reason for hiding this comment

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

The variable name firstLag might be confusing. Consider renaming it to side for better clarity and understanding, as it represents the side length of the square.


public Square(String color, String name, int firstLag) {
super(color, name);
this.firstLag = firstLag;
}

@Override
public int calculateArea() {
return firstLag * firstLag;
}

@Override
public String toString() {
return "Figure:"
+ "name: "
+ super.getName()
+ ", "
+ "area: "
+ calculateArea()
+ " sq. units, "
+ "firstLag: "
+ firstLag
+ ", "
+ "color: "
+ super.getColor();
}

}
Loading