-
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
Done #1775
base: master
Are you sure you want to change the base?
Done #1775
Changes from 3 commits
5f8db2f
c26debc
00af0f2
f453c23
5592940
1961220
b72ffb0
2a607bb
bb7e4f2
f57a1bf
022b7d6
e198d0b
9b0023b
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,24 @@ | ||
package core.basesyntax; | ||
|
||
import java.lang.Math; | ||
|
||
public class Circle extends Figure implements FigureMethods { | ||
private final float radius; | ||
|
||
public Circle(Colors color, float radius) { | ||
super(color, "Circle"); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return Math.PI * Math.pow(radius, 2); | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() + ", area: " | ||
+ area() + " sq. units, radius: " | ||
+ radius + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
private final static Random picker = new Random(); | ||
|
||
public static Colors getRandomColor() { | ||
return Colors.values()[picker.nextInt(Colors.values().length)]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,9 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
public enum Colors { | ||||||
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
|
||||||
RED, | ||||||
BLUE, | ||||||
GREEN, | ||||||
WHITE, | ||||||
BLACK | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure { | ||
private Colors color; | ||
private String name; | ||
|
||
public abstract void draw(); | ||
|
||
public Figure(Colors color, String name) { | ||
this.color = color; | ||
this.name = name; | ||
} | ||
|
||
public Colors getColor() { | ||
return this.color; | ||
} | ||
|
||
public void setColor(Colors color) { | ||
this.color = color; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FigureMethods { | ||
double area(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier extends 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. Avoid extending |
||
private final static Random picker = 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. Consider defining the number of figure types as a constant for better readability and maintainability. For example, |
||
|
||
public static Figure getRandomFigure() { | ||
return switch(picker.nextInt(4)) { | ||
case 0 -> new Circle(getRandomColor(), picker.nextInt(1, 50)); | ||
case 1 -> new Rectangle(getRandomColor(), picker.nextInt(1, 50), picker.nextInt(1, 50)); | ||
case 2 -> new RightTriangle(getRandomColor(), picker.nextInt(1, 50), picker.nextInt(1, 50)); | ||
case 3 -> new Square(getRandomColor(), picker.nextInt(1, 50)); | ||
default -> new Circle(Colors.WHITE, 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. The default case in the switch statement is redundant because the 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. IsoscelesTrapesoid missed |
||
}; | ||
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. Replace the magic number |
||
} | ||
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. Avoid using static methods. Instead, create an instance of |
||
|
||
public static Figure getDefaultFigure() { | ||
return new Circle(Colors.WHITE, 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. the same |
||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure implements FigureMethods { | ||
private final float firstBase; | ||
private final float secondBase; | ||
private final float height; | ||
|
||
public IsoscelesTrapezoid(Colors color, float firstBase, float secondBase, float height) { | ||
super(color, "IsoscelesTrapezoid"); | ||
this.firstBase = firstBase; | ||
this.secondBase = secondBase; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return (firstBase + secondBase) * height; | ||
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 formula for the area of an isosceles trapezoid is incorrect. It should be |
||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() | ||
+ ", area: " + area() + " sq. units, firstBase: " | ||
+ firstBase + " units, secondBase: " | ||
+ secondBase + " units, height: " | ||
+ height + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
public static void main(String args[]) { | ||
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. It's a common convention to declare the |
||
printArray(); | ||
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. redundant separate method, create Figure[] here, 6 - should be constant, use one for loop for this task |
||
} | ||
|
||
public static Figure[] listOfRandomFigures() { | ||
Figure[] array = new Figure[6]; | ||
|
||
for(int i = 0; i < 3; i++) { | ||
array[i] = FigureSupplier.getRandomFigure(); | ||
} | ||
|
||
for(int i = 3; i < 6; i++) { | ||
array[i] = FigureSupplier.getDefaultFigure(); | ||
} | ||
|
||
return array; | ||
} | ||
|
||
public static void printArray() { | ||
for(Figure i : listOfRandomFigures()) { | ||
i.draw(); | ||
} | ||
} | ||
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. Avoid using static methods for |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure implements FigureMethods { | ||
private final float firstSide; | ||
private final float secondSide; | ||
|
||
public Rectangle(Colors color, float firstSide, float secondSide) { | ||
super(color, "Rectangle"); | ||
this.firstSide = firstSide; | ||
this.secondSide = secondSide; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return firstSide * secondSide; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() + ", area: " | ||
+ area() + " sq. units, firstSide: " | ||
+ firstSide + " units, secondSide: " | ||
+ secondSide + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure implements FigureMethods { | ||
private final float firstLeg; | ||
private final float secondLeg; | ||
|
||
public RightTriangle(Colors color, float firstLeg, float secondLeg) { | ||
super(color, "RightTriangle"); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return (firstLeg * secondLeg) / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() | ||
+ ", area: " + area() + " sq. units, firstLeg: " | ||
+ firstLeg + " units, secondLeg: " | ||
+ secondLeg + " units, color: " + getColor()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure implements FigureMethods { | ||
private final float side; | ||
|
||
public Square(Colors color, float side) { | ||
super(color, "Square"); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double area() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + getName() + ", area: " | ||
+ area() + " sq. units, side: " | ||
+ side + " units, color: " + getColor()); | ||
} | ||
} |
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.