generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 a program that generate the array with random/default figures #1771
Open
yaroslav-pryshchepa
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
yaroslav-pryshchepa:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius, String color) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Circle, area: " + getArea() | ||
+ "sq. units, radius: " + radius + " units, color: " + getColor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
BLUE, | ||
YELLOW, | ||
BLACK, | ||
BROWN, | ||
PINK, | ||
GREEN, | ||
WHITE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
private final Random random = new Random(); | ||
|
||
public String getRandomColor() { | ||
int index = random.nextInt(Color.values().length); | ||
return Color.values()[index].name(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Ellipse extends Figure { | ||
private double lengthA; | ||
private double lengthB; | ||
|
||
public Ellipse(double sideA, double lengthB, String color) { | ||
super(color); | ||
this.lengthA = sideA; | ||
this.lengthB = lengthB; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * lengthA * lengthB; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Ellipse, area: " + getArea() | ||
+ "sq. units, lengthA: " + lengthA + " units, lengthB: " | ||
+ lengthB + " units, color: " + getColor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements FigureSquare, FigureInformation { | ||
private String color; | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
|
||
public abstract double getArea(); | ||
|
||
public abstract String printInformation(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureInformation { | ||
String printInformation(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureSquare { | ||
double getArea(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final int FIRST_MAX_NUMBER = 5; | ||
private static final int SECOND_MAX_NUMBER = 100; | ||
private static final int RADIUS = 10; | ||
private final Random random; | ||
private final ColorSupplier colorSupplier; | ||
|
||
public FigureSupplier(Random random, ColorSupplier colorSupplier) { | ||
this.random = random; | ||
this.colorSupplier = colorSupplier; | ||
} | ||
|
||
public Figure getRandomFigure() { | ||
int max1 = random.nextInt(SECOND_MAX_NUMBER) + 1; | ||
int max2 = random.nextInt(SECOND_MAX_NUMBER) + 1; | ||
String randomColor = colorSupplier.getRandomColor(); | ||
Figure figure = switch (random.nextInt(FIRST_MAX_NUMBER) + 1) { | ||
case 1 -> new Rectangle(max1, max2, randomColor); | ||
case 2 -> new RightTriangle(max1, max2, randomColor); | ||
case 3 -> new Ellipse(max1, max2, randomColor); | ||
case 4 -> new Square(max1, randomColor); | ||
default -> new IsoscelesTrapezoid(max1, max2, max2, randomColor); | ||
}; | ||
return figure; | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(RADIUS, "WHITE"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private double sideA; | ||
private double sideB; | ||
private double height; | ||
|
||
public IsoscelesTrapezoid(double sideA, double sideB, double height, String color) { | ||
super(color); | ||
this.sideA = sideA; | ||
this.sideB = sideB; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (sideA + sideB) / 2 * height; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: IsoscelesTrapezoid, area: " + getArea() | ||
+ "sq. units, sideA: " + sideA + " units, sideB: " + sideB + " units, height: " | ||
+ height + " units, color: " + getColor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class MainApp { | ||
public static void main(String[] args) { | ||
ColorSupplier colorSupplier = new ColorSupplier(); | ||
Random random = new Random(); | ||
|
||
FigureSupplier figureSupplier = new FigureSupplier(random, colorSupplier); | ||
Figure[] figures = new Figure[6]; | ||
|
||
for (int i = 0; i < figures.length; i++) { | ||
if (i < figures.length / 2) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} else { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
System.out.println(figures[i].printInformation()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double sideA; | ||
private double sideB; | ||
|
||
public Rectangle(double sideA, double sideB, String color) { | ||
super(color); | ||
this.sideA = sideA; | ||
this.sideB = sideB; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return sideA * sideB; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Rectangle, area: " + getArea() | ||
+ "sq. units, sideA: " + sideA + " units, sideB: " | ||
+ sideB + " units, color: " + getColor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private double base; | ||
private double height; | ||
|
||
public RightTriangle(double base, double height, String color) { | ||
super(color); | ||
this.base = base; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return base * height / 2; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: RightTriangle, area: " + getArea() | ||
+ "sq. units, base: " + base + " units, height: " | ||
+ height + " units, color: " + getColor(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private int side; | ||
|
||
public Square(int side, String color) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public String printInformation() { | ||
return "Figure: Square, area: " + getArea() | ||
+ "sq. units, side: " + side + " units, color: " + getColor(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider creating a constructor for color and calling super() in subclasses