-
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
Homework #1316
base: master
Are you sure you want to change the base?
Homework #1316
Changes from 2 commits
b200023
ae56bdf
2485123
515a8cd
15c3bc8
01ea44a
975432f
819bf35
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 Area { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private final double radius; | ||
|
||
public Circle(String color, double radius) { | ||
this.color = color; | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: circle, area: " | ||
+ | ||
getArea() | ||
+ | ||
" sq.units, radius: " | ||
+ | ||
radius | ||
+ | ||
" units, color: " | ||
+ | ||
color); | ||
} | ||
} |
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 static final String[] COLORS = {"red", "green", "blue", "yellow", "purple", "orange"}; | ||
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. Create |
||
private final Random random = new Random(); | ||
|
||
public String getRandomColor() { | ||
return COLORS[random.nextInt(COLORS.length)]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,5 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
public interface Drawer { | ||||||
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
|
||||||
void draw(); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements Area, Drawer { | ||
protected String color; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
import java.util.Random; | ||||||
|
||||||
public class FigureSupplier { | ||||||
private 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
should be same as color supplier |
||||||
|
||||||
public Figure getRandomFigure() { | ||||||
int choice = random.nextInt(5); | ||||||
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. |
||||||
String color = new ColorSupplier().getRandomColor(); | ||||||
|
||||||
switch (choice) { | ||||||
case 0: | ||||||
return new Square(color, random.nextDouble() * 10 + 1); | ||||||
case 1: | ||||||
return new Rectangle(color, random.nextDouble() * 10 + 1, | ||||||
random.nextDouble() * 10 + 1); | ||||||
case 2: | ||||||
return new RightTriangle(color, random.nextDouble() * 10 + 1, | ||||||
random.nextDouble() * 10 + 1); | ||||||
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. extract random.nextDouble() * 10 + 1 to a private method, make 10 a constant |
||||||
case 3: | ||||||
return new Circle(color, random.nextDouble() * 10 + 1); | ||||||
case 4: | ||||||
return new IsoscelesTrapezoid(color, random.nextDouble() * 10 + 1, | ||||||
random.nextDouble() * 10 + 1, | ||||||
random.nextDouble() * 10 + 1); | ||||||
default: | ||||||
return getDefaultFigure(); | ||||||
} | ||||||
} | ||||||
|
||||||
public Figure getDefaultFigure() { | ||||||
return new Circle("white", 10); | ||||||
} | ||||||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private final double upperBase; | ||
private final double lowerBase; | ||
private final double height; | ||
|
||
public IsoscelesTrapezoid(String color, double upperBase, double lowerBase, double height) { | ||
this.color = color; | ||
this.upperBase = upperBase; | ||
this.lowerBase = lowerBase; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * (upperBase + lowerBase) * height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: isosceles trapezoid, area: " | ||
+ getArea() | ||
+ " sq.units, upperBase: " | ||
+ upperBase | ||
+ " units, lowerBase: " | ||
+ lowerBase + " units, height: " | ||
+ height | ||
+ " units, color: " | ||
+ color); | ||
} | ||
} |
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) { | ||
final int N = 6; | ||
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. make it a class constant with a more meaningful name :) |
||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
Figure[] figures = new Figure[N]; | ||
|
||
for (int i = 0; i < 3; i++) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} | ||
|
||
for (int i = 3; i < 6; i++) { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
|
||
for (Figure figure : figures) { | ||
figure.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. can be done in one loop. use length/2 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private final double length; | ||
private final double width; | ||
|
||
public Rectangle(String color, double length, double width) { | ||
this.color = color; | ||
this.length = length; | ||
this.width = width; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return length * width; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: rectangle, area: " | ||
+ getArea() | ||
+ " sq.units, length: " | ||
+ length | ||
+ " units, width: " | ||
+ width + " units, color: " | ||
+ color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private final double firstLeg; | ||
private final double secondLeg; | ||
|
||
public RightTriangle(String color, double firstLeg, double secondLeg) { | ||
this.color = color; | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * firstLeg * secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: right triangle, area: " | ||
+ getArea() | ||
+ " sq.units, firstLeg: " | ||
+ firstLeg | ||
+ " units, secondLeg: " | ||
+ secondLeg + " units, color: " | ||
+ color); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core.basesyntax; | ||
|
||
class Square extends Figure { | ||
private final double side; | ||
|
||
public Square(String color, double side) { | ||
this.color = color; | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: square, area: " | ||
+ getArea() | ||
+ " sq.units, side: " | ||
+ side | ||
+ " units, color: " | ||
+ color); | ||
} | ||
} |
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.