-
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 1.0 #1769
base: master
Are you sure you want to change the base?
done 1.0 #1769
Changes from all commits
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,13 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class AbstractFigure implements Figure,Print { | ||
private String color; | ||
|
||
public AbstractFigure(String color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends AbstractFigure { | ||
private double radius; | ||
|
||
public Circle(String color, double radius) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Circle " + getColor() + " radius: " + radius + " area: " + getArea(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
public String getRandomColor() { | ||
String[] colors = {"Red","Green","Blue","Yellow","Black","Purple"}; | ||
Random random = new Random(); | ||
String randomColor = colors[random.nextInt(colors.length)]; | ||
return randomColor; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Figure { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
public AbstractFigure getRandomFigure() { | ||
Random random = new Random(); | ||
ColorSupplier colorSupplier = new ColorSupplier(); | ||
String color = colorSupplier.getRandomColor(); | ||
int figureType = random.nextInt(5); | ||
switch (figureType) { | ||
case 0: //Circle | ||
double radius = 1 + random.nextInt(10); | ||
return new Circle(color,radius); | ||
case 1: //RightTriangle | ||
double base = 1 + random.nextInt(10); | ||
double heigth = 1 + random.nextInt(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 variable |
||
return new RightTriangle(color,base,heigth); | ||
case 2: //Square | ||
double side = 1 + random.nextInt(10); | ||
return new Square(color,side); | ||
case 3: //IsoscelesTrapezoid | ||
double side1 = 1 + random.nextInt(10); | ||
double side2 = 1 + random.nextInt(10); | ||
heigth = 1 + random.nextInt(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 variable 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 |
||
return new IsoscelesTrapezoid(color,side1,side2,heigth); | ||
case 4: //Rectangle | ||
double length = 1 + random.nextInt(10); | ||
double width = 1 + random.nextInt(10); | ||
return new Rectangle(color,length,width); | ||
Comment on lines
+13
to
+30
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 using constants for the magic numbers (e.g., |
||
default: | ||
throw new IllegalStateException("Unexpected value: " + figureType); | ||
} | ||
} | ||
|
||
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,24 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends AbstractFigure { | ||
private double base1; | ||
private double base2; | ||
private double heigth; | ||
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 |
||
|
||
public IsoscelesTrapezoid(String color, double base1, double base2, double height) { | ||
super(color); | ||
this.base1 = base1; | ||
this.base2 = base2; | ||
this.heigth = 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 variable |
||
} | ||
|
||
public double getArea() { | ||
return 0.5 * (base1 + base2) * heigth; | ||
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 |
||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "IsoscelesTrapezoid " + getColor() + " base1: " + base1 + " base2: " + base2 | ||
+ " height: " + heigth + " area: " + getArea(); | ||
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 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax; | ||
|
||
public class MainApp { | ||
public static void main(String[] args) { | ||
AbstractFigure[] figures = new AbstractFigure[6]; | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
for (int i = 0; i < 3;i++) { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
System.out.println(figures[i].describe()); | ||
} | ||
for (int i = 3; i < 6;i++) { | ||
figures[i] = (AbstractFigure) figureSupplier.getDefaultFigure(); | ||
System.out.println(figures[i].describe()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Print extends Figure { | ||
String describe(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends AbstractFigure { | ||
private double length; | ||
private double width; | ||
|
||
public Rectangle(String color, double length, double width) { | ||
super(color); | ||
this.length = length; | ||
this.width = width; | ||
} | ||
|
||
public double getArea() { | ||
return length * width; | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Rectangle " + getColor() + " length: " + length + " width: " | ||
+ width + " area: " + getArea(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends AbstractFigure { | ||
private double base; | ||
private double heigth; | ||
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 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 |
||
|
||
public RightTriangle(String color,double base, double heigth) { | ||
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 |
||
super(color); | ||
this.base = base; | ||
this.heigth = heigth; | ||
Comment on lines
+7
to
+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 variable 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 |
||
} | ||
|
||
public double getArea() { | ||
return 0.5 * base * heigth; | ||
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 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 |
||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "RightTriange " + getColor() + " base: " + base + " heigth: " | ||
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 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 |
||
+ heigth + " area: " + getArea(); | ||
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 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends AbstractFigure { | ||
private double side; | ||
|
||
public Square(String color, double side) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Square " + getColor() + " side: " + side + " area: " + getArea(); | ||
} | ||
} |
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 variable
heigth
is misspelled. It should beheight
.