generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make figure clases and implementations for them
- Loading branch information
Mykola
committed
Dec 25, 2024
1 parent
3b03b22
commit 5078789
Showing
12 changed files
with
263 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class Circle extends Figure implements FindArea { | ||
private int radius; | ||
|
||
public Circle() { | ||
super(); | ||
this.radius = new Random().nextInt(1, 10); | ||
} | ||
|
||
public Circle(int radius, String color) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return 2 * Math.PI * Math.pow(radius, 2); | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("\nCircle " | ||
+ "color: " + this.getColor() | ||
+ " area: " + this.getArea() | ||
+ " radius: " + radius); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, | ||
YELLOW, | ||
BLUE, | ||
GREEN, | ||
BROWN, | ||
WHITE, | ||
BLACK | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
public String getRandomColor() { | ||
int index = new Random().nextInt(Color.values().length); | ||
return Color.values()[index].name(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure { | ||
private String color; | ||
private int area; | ||
|
||
public Figure() { | ||
color = new ColorSupplier().getRandomColor(); | ||
} | ||
|
||
public Figure(String color) { | ||
this.color = color; | ||
} | ||
|
||
public double getArea() { | ||
return area; | ||
} | ||
|
||
public void setArea(int area) { | ||
this.area = area; | ||
} | ||
|
||
public abstract void draw(); | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(String color) { | ||
this.color = color; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
import java.lang.invoke.SwitchPoint; | ||
import java.util.Random; | ||
|
||
public class FigureSuplier { | ||
public Figure getRandomFigure() { | ||
int index = new Random().nextInt(5); | ||
return switch (index) { | ||
case 1 -> new Circle(); | ||
case 2 -> new IsoscelesTrapezoid(); | ||
case 3 -> new Rectangle(); | ||
case 4 -> new RightTriangle(); | ||
case 5 -> new Square(); | ||
default -> null; | ||
}; | ||
} | ||
|
||
public Figure getDeafoltFigure() { | ||
return new Circle(10, "WHITE"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface FindArea { | ||
double getArea(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class IsoscelesTrapezoid extends Figure implements FindArea { | ||
private int biggerBaseSide; | ||
private int smallerBaseSide; | ||
private double side; | ||
private int height; | ||
|
||
public IsoscelesTrapezoid() { | ||
super(); | ||
int firstRandomInt = new Random().nextInt(1, 10); | ||
int secondRandomInt = new Random().nextInt(1, 10); | ||
if (firstRandomInt > secondRandomInt) { | ||
biggerBaseSide = firstRandomInt; | ||
smallerBaseSide = secondRandomInt; | ||
} else { | ||
smallerBaseSide = firstRandomInt; | ||
biggerBaseSide = secondRandomInt; | ||
} | ||
this.height = 10; | ||
this.side = getSide(); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (double) (biggerBaseSide + smallerBaseSide) * height / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("\nIsosceles trapezoid " | ||
+ "color: " + this.getColor() | ||
+ " area: " + this.getArea() | ||
+ " bigger base: " + biggerBaseSide | ||
+ " smaller base: " + smallerBaseSide | ||
+ " side: " + side | ||
+ " height: " + height); | ||
} | ||
|
||
private double getSide() { | ||
if (biggerBaseSide == smallerBaseSide) { | ||
return height; | ||
} | ||
|
||
return Math.sqrt(Math.pow(((double) (biggerBaseSide - smallerBaseSide) / 2), 2) | ||
+ Math.pow(height, 2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
public static void main(String[] arg) { | ||
Figure[] figures = new Figure[6]; | ||
|
||
for (int i = 0; i < figures.length/2; i++) { | ||
figures[i] = new FigureSuplier().getRandomFigure(); | ||
} | ||
|
||
for (int i = 3; i < figures.length; i++) { | ||
figures[i] = new FigureSuplier().getDeafoltFigure(); | ||
} | ||
|
||
for (Figure figure: figures) { | ||
figure.draw(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class Rectangle extends Figure implements FindArea { | ||
private int firstSide; | ||
private int secondSide; | ||
|
||
public Rectangle() { | ||
super(); | ||
this.firstSide = new Random().nextInt(1, 10); | ||
this.secondSide = new Random().nextInt(1, 10); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return firstSide * secondSide; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("\nRectangle " | ||
+ "color: " + this.getColor() | ||
+ " area: " + this.getArea() | ||
+ " first side: " + firstSide | ||
+ " first side: " + secondSide); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class RightTriangle extends Figure implements FindArea { | ||
private int firsLeg; | ||
private int secondLeg; | ||
private double thirdLeg; | ||
|
||
public RightTriangle() { | ||
super(); | ||
this.firsLeg = new Random().nextInt(1, 10); | ||
this.secondLeg = new Random().nextInt(1, 10); | ||
this.thirdLeg = Math.sqrt(Math.pow(firsLeg, 2) + Math.pow(secondLeg, 2)); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (double) firsLeg * secondLeg / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("\nRight triangle " | ||
+ "color: " + this.getColor() | ||
+ " area: " + this.getArea() | ||
+ " first leg: " + firsLeg | ||
+ " second leg: " + secondLeg | ||
+ " third leg: " + thirdLeg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class Square extends Figure implements FindArea { | ||
private int side; | ||
|
||
public Square() { | ||
super(); | ||
this.side = new Random().nextInt(1, 10); | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.pow(side, 2); | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("\nSquere " | ||
+ "color: " + this.getColor() | ||
+ " area: " + this.getArea() | ||
+ " side: " + side); | ||
} | ||
} |