-
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
Added necessary Classes/Test passed #1508
base: master
Are you sure you want to change the base?
Changes from all commits
33c5e7a
12ba226
00b27bf
bbea4bc
b199840
43bc474
6825857
c5b8314
7442331
438ef1d
457884c
b3e928b
fabe0aa
999412e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalculator { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius, Color color) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.round(Math.PI * radius * radius); | ||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: Circle, area: " + getArea() + " sq. units, radius: " | ||
+ radius + " units, color: " + getColor(); | ||
} | ||
/* Wzór na pole koła pi*r*r */ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a method to get a random color from the enum. This would be useful for the ColorSupplier class to generate random colors. |
||
RED, | ||
GREEN, | ||
BLUE, | ||
YELLOW, | ||
ORANGE, | ||
CYAN, | ||
WHITE, | ||
BLACK, | ||
LIME | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
|
||
private static final Random RANDOM = new Random(); | ||
|
||
public Color getRandomColor() { | ||
int index = RANDOM.nextInt(Color.values().length); | ||
return Color.values()[index]; | ||
} | ||
|
||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's refactor this class to this: public abstract class Figure implements FigureInfo, AreaCalculator {
private Color color;
public Figure(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements FigureInfo, AreaCalculator { | ||
private Color color; | ||
|
||
public Figure(Color color) { | ||
this.color = color; | ||
} | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureInfo { | ||
String getFigureInfo(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final Random RANDOM = new Random(); | ||
private static final int FIGURE_COUNT = 5; | ||
private static final int MIN_LENGTH = 2; | ||
private static final int MAX_LENGTH = 10; | ||
private static final int DEAFULT_FIGURE_RADIUS = 10; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the constant name 'DEAFULT_FIGURE_RADIUS'. It should be 'DEFAULT_FIGURE_RADIUS'. |
||
private static final ColorSupplier colorSupplier = new ColorSupplier(); | ||
|
||
public Figure getRandomFigure() { | ||
int ran = RANDOM.nextInt(FIGURE_COUNT); | ||
int a = RANDOM.nextInt(MAX_LENGTH - MIN_LENGTH + 1) + MIN_LENGTH; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what is happening here - why not just use RANDOM.nextInt(MAX_LENGTH + 1)? Do we need MIN_LENGTH? |
||
int b = RANDOM.nextInt(MAX_LENGTH - MIN_LENGTH + 1) + MIN_LENGTH; | ||
int c = RANDOM.nextInt(MAX_LENGTH - MIN_LENGTH + 1) + MIN_LENGTH; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not fixed: #1508 (comment) |
||
return switch (ran) { | ||
case (0) -> new Square(a, colorSupplier.getRandomColor()); | ||
case (1) -> new Rectangle(a, b, colorSupplier.getRandomColor()); | ||
case (2) -> new RightTriangle(a, b, colorSupplier.getRandomColor()); | ||
case (3) -> new Circle(a, Color.WHITE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The color for the Circle in the random figure generation should be obtained from the ColorSupplier instead of being hardcoded to Color.WHITE. |
||
default -> new IsoscelesTrapezoid(a, b, c, colorSupplier.getRandomColor()); | ||
}; | ||
|
||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(DEAFULT_FIGURE_RADIUS, Color.WHITE); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private int firstBase; | ||
private int secondBase; | ||
private int heigh; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the variable name 'heigh'. It should be 'height'. This will improve code readability and prevent potential confusion. |
||
|
||
public IsoscelesTrapezoid(int firstBase, int secondBase, int heigh, Color color) { | ||
super(color); | ||
this.firstBase = firstBase; | ||
this.secondBase = secondBase; | ||
this.heigh = heigh; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return ((firstBase + secondBase) * heigh) / 2.0; | ||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: IsoscelesTrapezoid, area: " + getArea() + " sq. units, firstBase: " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method 'getFigureInfo' should use consistent formatting for the output string. Consider adding commas or semicolons between the attributes for better readability. |
||
+ firstBase + " secondBase " | ||
+ secondBase + " heigh " + heigh + " units, color: " + getColor(); | ||
} | ||
/* Wzór na pole trapezu równoramiennego (a+b)*h /2 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment on line 26 is in Polish. It should be translated to English to maintain consistency and ensure that all developers can understand it. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
FigureSupplier figureIndex = new FigureSupplier(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable name 'figureIndex' is misleading. It suggests an index or position rather than a supplier. Consider renaming it to 'figureSupplier' for clarity. |
||
Figure[] figures = new Figure[6]; | ||
|
||
for (int i = 0; i < figures.length; i++) { | ||
if (i < figures.length / 2) { | ||
figures[i] = figureIndex.getRandomFigure(); | ||
} else { | ||
figures[i] = figureIndex.getDefaultFigure(); | ||
} | ||
} | ||
|
||
for (Figure figure : figures) { | ||
System.out.println(figure.getFigureInfo()); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private int base; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable names 'base' and 'side' are not very descriptive for a rectangle. Consider using 'width' and 'height' for clarity. |
||
private int side; | ||
|
||
public Rectangle(int base, int side, Color color) { | ||
super(color); | ||
this.base = base; | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return base * side; | ||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: Rectangle, area: " + getArea() + " sq. units, base: " + base | ||
+ " side " + side + " units, color: " + getColor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a missing colon after 'side'. It should be 'side: ' to maintain consistency in the output format. |
||
} | ||
/* Wzór na pole Prostokąta a*b */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment is in Polish, which might not be understandable to all developers. Consider translating it to English. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private int base; | ||
private int heigh; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the variable name 'heigh'. It should be 'height'. This typo is repeated in the constructor and other parts of the code. |
||
|
||
public RightTriangle(int base, int heigh, Color color) { | ||
super(color); | ||
this.base = base; | ||
this.heigh = heigh; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (base * heigh) / 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The calculation for the area should be cast to double to ensure precision, as integer division may lead to loss of precision. Consider using '(double) base * heigh / 2' to ensure the result is a double. |
||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: RightTriangle, area: " + getArea() + " sq. units, base: " | ||
+ base + " heigh " + heigh + " units, color: " + getColor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a missing colon after 'heigh' in the string concatenation. It should be 'height: ' instead of 'heigh '. |
||
} | ||
/* Wzór na pole trójkąta prostokątnego (a*h)/2 */ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private int side; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 'side' attribute should be validated to ensure it is a positive integer. Consider adding a check in the constructor to throw an IllegalArgumentException if 'side' is less than or equal to zero. |
||
|
||
public Square(int side, Color color) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: Square, area: " + getArea() + " sq. units, side: " | ||
+ side + " units, color: " + getColor(); | ||
} | ||
/* Wzór na pole kwadratu a*a */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment on line 21 is in Polish. Consider translating it to English for consistency and to ensure that all developers can understand it. |
||
|
||
} |
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.
The comment is in Polish and might not be understood by all developers. Consider translating it to English for consistency and clarity.