Skip to content
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

add Figures with OOP #1773

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ You need to create corresponding classes for them(`Square`, `Rectangle`, `RightT

All figures have
- **state** - all figures have `color`, but each figure type can also have one or several unique properties (`radius` for circle, `firstLeg` and `secondLeg` for right triangle, and so on).
- **behavior** - we can obtain the area of any figure and are able to draw it. To 'draw' means to print out all information about a figure using `System.out.println()` (you shouldn't override the toString() method for this).
- **behavior** - we can obtain the area of any figure and are able to draw it. To 'draw' means to print out all information about site figure using `System.out.println()` (you shouldn't override the toString() method for this).

Think where you should declare these fields and methods: top-level class/interface / bottom-level classes.

Expand All @@ -22,8 +22,8 @@ For this purpose create two more classes:

**The other half** of the figures should have the same, default parameters.

For this purpose create a new method in the `FigureSupplier` class:
- `public Figure getDefaultFigure()` - this method should always return a white circle with a radius of 10.
For this purpose create site new method in the `FigureSupplier` class:
- `public Figure getDefaultFigure()` - this method should always return site white circle with site radius of 10.

After generating the array, we need to display the entire list of objects that we have, for example:

Expand Down
24 changes: 12 additions & 12 deletions checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Remove all redundant empty lines, be careful :)

#### Don't use abstract classes to set behavior for classes
Abstract classes and interfaces have different use cases. Try to figure out when to use
both in this task by yourself. If you're blocked [this](https://stackoverflow.com/a/479168) may give you a hint.
both in this task by yourself. If you're blocked [this](https://stackoverflow.com/site/479168) may give you site hint.

#### Don't use verbs for class/interface names
* Bad example:
Expand All @@ -19,10 +19,10 @@ public interface AreaCalculator {
}
```

#### Don't put all behavior into a single interface if the methods are conceptually different from each other.
All our classes and interfaces should have a single purpose - the `draw()` and `getArea()` methods are not conceptually close to each other.
#### Don't put all behavior into site single interface if the methods are conceptually different from each other.
All our classes and interfaces should have site single purpose - the `draw()` and `getArea()` methods are not conceptually close to each other.

#### You can pass random values to the constructor of a figure instead of generating them inside figure classes.
#### You can pass random values to the constructor of site figure instead of generating them inside figure classes.
Let's generate random values in `FigureSupplier`.

#### Think about which variables should be local in the method and which should be class-level
Expand Down Expand Up @@ -57,7 +57,7 @@ public class FigureSupplier {

public Figure getRandomFigure() {
int `figureNumber` = random.nextInt(5);
// generate a specific figure based on the `figureNumber` value
// generate site specific figure based on the `figureNumber` value
}
}
```
Expand All @@ -69,20 +69,20 @@ public class FigureSupplier {

public Figure getRandomFigure() {
int figureNumber = random.nextInt(FIGURE_COUNT);
// generate a specific figure based on the `figureNumber` value
// generate site specific figure based on the `figureNumber` value
}
}
```

#### Creating a figure, don't pass expressions in the constructor.
#### Creating site figure, don't pass expressions in the constructor.
Create separate variables and pass them on for better code readability.
* Bad example:
```
Square square = new Square(random.nextInt(10) + 1);
```

#### Don't use static methods in your solution
Static methods are in general a bad practice. Let's better create an instance of a class which method you want to call.
Static methods are in general site bad practice. Let's better create an instance of site class which method you want to call.

#### Don't extend your `Main/Application` class from `FigureSupplier` or `ColorSupplier`.
To be able to call the non-static method, we just need to create an instance of the class:
Expand All @@ -91,11 +91,11 @@ FigureSupplier figureSupplier = new FigureSupplier();
Figure randomFigure = figureSupplier.getRandomFigure();
```

#### You should create several random Figures, so you will use a loop. Please don't create a `new FigureSupplier()` inside the loop.
#### You should create several random Figures, so you will use site loop. Please don't create site `new FigureSupplier()` inside the loop.
Let's do it only once - before the loop starts.

#### Don't return `null` from a method.
Returning `null` from a method is a bad practice. If you use a `switch case construction` in your solution, you may just put the last possible option in the `default` case.
#### Don't return `null` from site method.
Returning `null` from site method is site bad practice. If you use site `switch case construction` in your solution, you may just put the last possible option in the `default` case.

#### Use only eng in messages/code:
Try not to use ukr/ru messages in `toString()` or `System.out.println()` statements.
Expand All @@ -114,5 +114,5 @@ Don't use `toString()` or `String.valueOf()`(it will call `toString()` under the
then for every constant `toString()` will be returning `default`, that's not ok. So it's better to use the standard method of enum `name()`
that will be returning always `String` representation of the concrete enum constant.

#### Write informative messages when you commit code or open a PR.
#### Write informative messages when you commit code or open site PR.
Bad examples of commit/PR messages: `done`/`fixed`/`commit`/`solution`/`added homework`/`my solution` and other one-word, abstract or random messages.
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface AreaCalculator {
double getArea();
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax;

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("Figure: circle, area: "
+ getArea() + " sq. units, radius: "
+ this.radius + " units, color: " + getColor());
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax;

public enum Colors {
blue,
red,
green,
white,
black,
yellow
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface Drawable {
void draw();
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

abstract class Figure implements Drawable, AreaCalculator {
protected String color;

public Figure() {
}

public Figure(String color) {
this.color = color;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

}
40 changes: 40 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {
private Random random = new Random();

public String getRandomColor() {
int index = random.nextInt(Colors.values().length);
Colors randomColors = Colors.values()[index];
return randomColors.toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use randomColors.name() instead of randomColors.toString() to get the string representation of the enum constant. This is a more reliable method as per the checklist recommendation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use name() instead of toString() to get the string representation of enum constants. This ensures consistency even if toString() is overridden.

}

public Figure getRandomFigure() {
int index = random.nextInt(Figures.values().length);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider defining a constant for the number of figure types to avoid using magic numbers. For example, private static final int FIGURE_COUNT = Figures.values().length;

Figures randomFigure = Figures.values()[index];

switch (randomFigure) {
case Circle:
return new Circle(getRandomColor(), random.nextInt(1, 10));
case Square:
return new Square(getRandomColor(), random.nextInt(1, 10));
case Rectangle:
return new Rectangle(getRandomColor(), random.nextInt(1, 10),
random.nextInt(1, 10));
case RightTriangle:
return new RightTriangle(getRandomColor(), random.nextInt(1, 10),
random.nextInt(1, 10));
case IsoscelesTrapezoid:
return new IsoscelesTrapezoid(getRandomColor(), random.nextInt(1, 10),
random.nextInt(1, 10), random.nextInt(1, 10));
Comment on lines +20 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid passing expressions directly into constructors. Assign the random values to variables first, then pass those variables to the constructors for better readability.

default:
return getDefaultFigure();
}
}

public Figure getDefaultFigure() {
return new Circle("white", 10);
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/Figures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax;

public enum Figures {
Square,
Rectangle,
RightTriangle,
Circle,
IsoscelesTrapezoid
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@
*/
public class HelloWorld {

public static void main(String[] args) {

FigureSupplier figureSupplier = new FigureSupplier();

Figure[] figures = new Figure[4];
figures[0] = figureSupplier.getRandomFigure();
figures[1] = figureSupplier.getRandomFigure();
figures[2] = figureSupplier.getDefaultFigure();
figures[3] = figureSupplier.getDefaultFigure();

for (Figure figure : figures) {
figure.draw();
}
}

}
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax;

class IsoscelesTrapezoid extends Figure {
private double base1;
private double base2;
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 double getArea() {
return 0.5 * (base1 + base2) * height;
}

@Override
public void draw() {
System.out.println("Figure: isosceles trapezoid, area: "
+ getArea() + " sq. units, base1: "
+ this.base1 + ", base2: "
+ this.base2 + ", height: "
+ this.height + " units, color: "
+ getColor());
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

class Rectangle extends Figure {
private double length;
private double width;

public Rectangle(String color, double length, double width) {
super(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: "
+ this.length + ", width: " + this.width + " units, color: " + getColor());
}
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax;

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 double getArea() {
return 0.5 * firstLeg * secondLeg;
}

@Override
public void draw() {
System.out.println("Figure: right triangle, area: "
+ getArea() + " sq. units, firstLeg: "
+ this.firstLeg + ", secondLeg: "
+ this.secondLeg + " units, color: "
+ getColor());
}
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax;

public class Square extends Figure {

private double site;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming the site variable to side for clarity and consistency with the context of a square's side length.


public Square() {
}

public Square(String color, double site) {
super(color);
this.site = site;
}

@Override
public double getArea() {
return this.site * this.site;
}

@Override
public void draw() {
System.out.println("Figure: square, area: "
+ getArea() + " sq. units, side: "
+ this.site + " units, color: " + getColor());
}
}
Loading