-
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 9 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,26 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius) { | ||
this.radius = radius; | ||
} | ||
|
||
public Circle(int radius, String color) { | ||
this.radius = radius; | ||
super.color = color; | ||
} | ||
|
||
@Override | ||
public double getFigureArea() { | ||
return Math.round(Math.PI * radius * radius); | ||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: Circle, area: " + getFigureArea() + " sq. units, radius: " | ||
+ radius + " units, color: " + super.color; | ||
} | ||
/* Wzór na pole koła pi*r*r */ | ||
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 and might not be understood by all developers. Consider translating it to English for consistency and clarity. |
||
} |
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 { | ||||||||||||||||||
|
||||||||||||||||||
static final Random RANDOM = new Random(); | ||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
public String getRandomColor() { | ||||||||||||||||||
int index = RANDOM.nextInt(Color.values().length); | ||||||||||||||||||
return Color.values()[index].toString(); | ||||||||||||||||||
} | ||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
} |
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,12 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements FigureArea { | ||
protected String 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. You've created an enum for colors - why you didn't use it here?? |
||
|
||
public Figure() { | ||
ColorSupplier color = new ColorSupplier(); | ||
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. You're not supposed to set a random color here, but if you did, the |
||
this.color = color.getRandomColor(); | ||
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. Why are you assigning random color by default here? Use constructor to allow passing the value while creating an object. |
||
} | ||
|
||
abstract String getFigureInfo(); | ||
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 for a THIRD time! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureArea { | ||
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. This is not a valid name for interface in Java. It should be an adjective or a meaningful noun - your figure is not a figure area. |
||
double getFigureArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package core.basesyntax; | ||
|
||
import static core.basesyntax.ColorSupplier.RANDOM; | ||
|
||
public class FigureSupplier { | ||
|
||
public Figure getRandomFigure() { | ||
int ran = RANDOM.nextInt(5); | ||
int a = RANDOM.nextInt(10) + 2; | ||
int b = RANDOM.nextInt(10) + 2; | ||
int c = RANDOM.nextInt(10) + 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. Don't create these values ahead - most them won't be always used. Do it on figures constructors calls. Also, put numeric values in constants (e.g. MAX_LENGTH) |
||
switch (ran) { | ||
case (0): | ||
return new Square(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. In all cases you should pass random size and random color, so create and use private static final ColorSupplier colorSupplier = new ColorSupplier();
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; |
||
case (1): | ||
return new Rectangle(a,b); | ||
case (2): | ||
return new RightTriangle(a,b); | ||
case (3): | ||
return new Circle(a); | ||
default: | ||
return new IsoscelesTrapezoid(a,b,c); | ||
} | ||
|
||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(10, "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. You've created an enum for colors, why not use it here? |
||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||
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) { | ||||||
this.firstBase = firstBase; | ||||||
this.secondBase = secondBase; | ||||||
this.heigh = heigh; | ||||||
} | ||||||
|
||||||
public IsoscelesTrapezoid(int firstBase, int secondBase, int heigh, String color) { | ||||||
this.firstBase = firstBase; | ||||||
this.secondBase = secondBase; | ||||||
this.heigh = heigh; | ||||||
super.color = color; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public double getFigureArea() { | ||||||
return (firstBase * secondBase) * 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.
Suggested change
|
||||||
} | ||||||
|
||||||
@Override | ||||||
public String getFigureInfo() { | ||||||
return "Figure: IsoscelesTrapezoid, area: " + getFigureArea() + " sq. units, firstBase: " | ||||||
+ firstBase + " secondBase " | ||||||
+ secondBase + " heigh " + heigh + " units, color: " + super.color; | ||||||
} | ||||||
/* 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,15 @@ | ||
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 < 6; i++) { | ||
figures[i] = figureIndex.getRandomFigure(); | ||
|
||
} | ||
for (int i = 0; i < 6; i++) { | ||
System.out.println(figures[i].getFigureInfo()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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) { | ||
this.base = base; | ||
this.side = 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. This constructor is redundant, please remove it from all classes that |
||
|
||
public Rectangle(int base, int side, String color) { | ||
this.base = base; | ||
this.side = side; | ||
super.color = color; | ||
} | ||
|
||
@Override | ||
public double getFigureArea() { | ||
return base * side; | ||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: Rectangle, area: " + getFigureArea() + " sq. units, base: " + base | ||
+ " side " + side + " units, color: " + super.color; | ||
} | ||
/* 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,29 @@ | ||
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) { | ||
this.base = base; | ||
this.heigh = heigh; | ||
} | ||
|
||
public RightTriangle(int base, int heigh, String color) { | ||
this.base = base; | ||
this.heigh = heigh; | ||
super.color = color; | ||
} | ||
|
||
@Override | ||
public double getFigureArea() { | ||
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: " + getFigureArea() + " sq. units, base: " | ||
+ base + " heigh " + heigh + " units, color: " + super.color; | ||
} | ||
/* 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,27 @@ | ||
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) { | ||
this.side = side; | ||
} | ||
|
||
public Square(int side, String color) { | ||
this.side = side; | ||
super.color = color; | ||
} | ||
|
||
@Override | ||
public double getFigureArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public String getFigureInfo() { | ||
return "Figure: Square, area: " + getFigureArea() + " sq. units, side: " | ||
+ side + " units, color: " + super.color; | ||
} | ||
/* 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 color is supposed to be an enum, for all figures.