-
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
hw-1-jv-oop-advanced #1741
base: master
Are you sure you want to change the base?
hw-1-jv-oop-advanced #1741
Changes from 9 commits
640268c
fb52199
77f6840
21d4614
50ed5af
7869b7d
696a001
c9b1896
880f82b
f40894a
7b63e17
c09aec3
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,20 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private double radius; | ||
|
||
public Circle(String color, double radius) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("radius: " + radius + " units"); | ||
} | ||
} |
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", "black", "white"}; | ||
private 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 Drawable { | ||
void draw(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.Drawable; | ||
import core.basesyntax.Measurable; | ||
|
||
public abstract class Figure implements Drawable, Measurable { | ||
protected String color; | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: " + this.getClass().getSimpleName() + ", area: " + getArea() + " sq. units, color: " + color); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
class FigureSupplier { | ||
private static final int FIGURE_TYPES_COUNT = 5; | ||
private Random random = new Random(); | ||
private ColorSupplier colorSupplier = new ColorSupplier(); | ||
|
||
public Figure getRandomFigure() { | ||
String color = colorSupplier.getRandomColor(); | ||
switch (random.nextInt(FIGURE_TYPES_COUNT)) { | ||
case 0: { | ||
int side = random.nextInt(10) + 1; | ||
return new Square(color, side); | ||
} | ||
case 1: { | ||
int width = random.nextInt(10) + 1; | ||
int height = random.nextInt(10) + 1; | ||
return new Rectangle(color, width, height); | ||
} | ||
case 2: { | ||
int base = random.nextInt(10) + 1; | ||
int height = random.nextInt(10) + 1; | ||
return new RightTriangle(color, base, height); | ||
} | ||
case 3: { | ||
int radius = random.nextInt(10) + 1; | ||
return new Circle(color, radius); | ||
} | ||
default: { | ||
int base1 = random.nextInt(10) + 1; | ||
int base2 = random.nextInt(10) + 1; | ||
int height = random.nextInt(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. magic numbers |
||
return new IsoscelesTrapezoid(color, base1, base2, height); | ||
} | ||
} | ||
} | ||
|
||
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,25 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private double base1; | ||
private double base2; | ||
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. numbers in naming are usually bad, let's name them topBase bottomBase |
||
private double height; | ||
|
||
public IsoscelesTrapezoid(String color, double base1, double base2, double height) { | ||
super(color); | ||
this.base1 = base1; | ||
this.base2 = base2; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
super.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. Ensure that the |
||
System.out.println("base1: " + base1 + " units, base2: " + base2 + " units, height: " + height + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * (base1 + base2) * height; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
Figure[] figures = new Figure[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. Magic number |
||
|
||
// Generate random figures for the first half | ||
for (int i = 0; i < figures.length / 2; i++) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} | ||
|
||
// Generate default figures for the second half | ||
for (int i = figures.length / 2; i < figures.length; i++) { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} | ||
|
||
// Display all figures | ||
for (Figure figure : figures) { | ||
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. use one for loop to implement main method 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 |
||
figure.draw(); | ||
System.out.println(); | ||
} | ||
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 is possible to make everything in one loop |
||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,5 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
public interface Measurable { | ||||||
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
|
||||||
double getArea(); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double width; | ||
private double height; | ||
|
||
public Rectangle(String color, double width, double height) { | ||
super(color); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("width: " + width + " units, height: " + height + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return width * height; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private double firstLeg; | ||
private double secondLeg; | ||
|
||
public RightTriangle(String color, double firstLeg, double secondLeg) { | ||
super(color); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 0.5 * firstLeg * secondLeg; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private double side; | ||
|
||
public Square(String color, double side) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
super.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. Ensure that 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. Agree |
||
System.out.println("side: " + side + " units"); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
} |
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.
Better to create Color enum, and use it in all figures as field. It would be easier and more clear to extend code. And also you would avoid issues when someone made a mistake writing some color