From a58aa623d45dc42337604c5cfde42e0e67cdbec1 Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Sat, 28 Dec 2024 16:51:01 +0200 Subject: [PATCH 1/2] add Figures with OOP --- README.md | 6 +-- checklist.md | 24 +++++------ .../java/core/basesyntax/AreaCalculator.java | 5 +++ src/main/java/core/basesyntax/Circle.java | 22 ++++++++++ src/main/java/core/basesyntax/Colors.java | 10 +++++ src/main/java/core/basesyntax/Drawable.java | 5 +++ src/main/java/core/basesyntax/Figure.java | 21 ++++++++++ .../java/core/basesyntax/FigureSupplier.java | 40 +++++++++++++++++++ src/main/java/core/basesyntax/Figures.java | 9 +++++ src/main/java/core/basesyntax/HelloWorld.java | 15 +++++++ .../core/basesyntax/IsoscelesTrapezoid.java | 29 ++++++++++++++ src/main/java/core/basesyntax/Rectangle.java | 24 +++++++++++ .../java/core/basesyntax/RightTriangle.java | 26 ++++++++++++ src/main/java/core/basesyntax/Square.java | 26 ++++++++++++ 14 files changed, 247 insertions(+), 15 deletions(-) create mode 100644 src/main/java/core/basesyntax/AreaCalculator.java create mode 100644 src/main/java/core/basesyntax/Circle.java create mode 100644 src/main/java/core/basesyntax/Colors.java create mode 100644 src/main/java/core/basesyntax/Drawable.java create mode 100644 src/main/java/core/basesyntax/Figure.java create mode 100644 src/main/java/core/basesyntax/FigureSupplier.java create mode 100644 src/main/java/core/basesyntax/Figures.java create mode 100644 src/main/java/core/basesyntax/IsoscelesTrapezoid.java create mode 100644 src/main/java/core/basesyntax/Rectangle.java create mode 100644 src/main/java/core/basesyntax/RightTriangle.java create mode 100644 src/main/java/core/basesyntax/Square.java diff --git a/README.md b/README.md index b106b551be..49c5bedf5b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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: diff --git a/checklist.md b/checklist.md index a0b2492755..d327ad2c51 100644 --- a/checklist.md +++ b/checklist.md @@ -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: @@ -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 @@ -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 } } ``` @@ -69,12 +69,12 @@ 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: ``` @@ -82,7 +82,7 @@ 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: @@ -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. @@ -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. diff --git a/src/main/java/core/basesyntax/AreaCalculator.java b/src/main/java/core/basesyntax/AreaCalculator.java new file mode 100644 index 0000000000..673262c690 --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalculator.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface AreaCalculator { + double getArea(); +} diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..cbef46d9e3 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -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: " + this.color); + } +} diff --git a/src/main/java/core/basesyntax/Colors.java b/src/main/java/core/basesyntax/Colors.java new file mode 100644 index 0000000000..0eb4c78044 --- /dev/null +++ b/src/main/java/core/basesyntax/Colors.java @@ -0,0 +1,10 @@ +package core.basesyntax; + +public enum Colors { + blue, + red, + green, + white, + black, + yellow +} diff --git a/src/main/java/core/basesyntax/Drawable.java b/src/main/java/core/basesyntax/Drawable.java new file mode 100644 index 0000000000..d045270178 --- /dev/null +++ b/src/main/java/core/basesyntax/Drawable.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Drawable { + void draw(); +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..39463944c3 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -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; + } + +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..7a39b5c9c1 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -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(); + } + + public Figure getRandomFigure() { + int index = random.nextInt(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)); + default: + return null; + } + } + + public Figure getDefaultFigure() { + return new Circle("white", 10); + } +} diff --git a/src/main/java/core/basesyntax/Figures.java b/src/main/java/core/basesyntax/Figures.java new file mode 100644 index 0000000000..314d559996 --- /dev/null +++ b/src/main/java/core/basesyntax/Figures.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +public enum Figures { + Square, + Rectangle, + RightTriangle, + Circle, + IsoscelesTrapezoid +} diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java index 97db782bf7..8ba3ff0523 100644 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ b/src/main/java/core/basesyntax/HelloWorld.java @@ -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(); + } + } + } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java new file mode 100644 index 0000000000..0a5f669a1c --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -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: " + + this.color); + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..283d1e1d73 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -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: " + this.color); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..8b1efd6fc4 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -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: " + + this.color); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..4d15e91eb5 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Square extends Figure { + + private double site; + + public Square() { + } + + public Square(String color, int 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: " + this.color); + } +} From c58bfac5de12f454e23ef49ee75deb636a6adb03 Mon Sep 17 00:00:00 2001 From: FroGitHub Date: Sat, 28 Dec 2024 17:03:06 +0200 Subject: [PATCH 2/2] change this.color to getColor() and null to default figure --- src/main/java/core/basesyntax/Circle.java | 2 +- src/main/java/core/basesyntax/FigureSupplier.java | 2 +- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 2 +- src/main/java/core/basesyntax/Rectangle.java | 2 +- src/main/java/core/basesyntax/RightTriangle.java | 2 +- src/main/java/core/basesyntax/Square.java | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index cbef46d9e3..cd38d663fc 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -17,6 +17,6 @@ public double getArea() { public void draw() { System.out.println("Figure: circle, area: " + getArea() + " sq. units, radius: " - + this.radius + " units, color: " + this.color); + + this.radius + " units, color: " + getColor()); } } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 7a39b5c9c1..3f362ae3b8 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -30,7 +30,7 @@ public Figure getRandomFigure() { return new IsoscelesTrapezoid(getRandomColor(), random.nextInt(1, 10), random.nextInt(1, 10), random.nextInt(1, 10)); default: - return null; + return getDefaultFigure(); } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 0a5f669a1c..30dfbb00e2 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -24,6 +24,6 @@ public void draw() { + this.base1 + ", base2: " + this.base2 + ", height: " + this.height + " units, color: " - + this.color); + + getColor()); } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 283d1e1d73..2980de15b5 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -19,6 +19,6 @@ public double getArea() { public void draw() { System.out.println("Figure: rectangle, area: " + getArea() + " sq. units, length: " - + this.length + ", width: " + this.width + " units, color: " + this.color); + + this.length + ", width: " + this.width + " units, color: " + getColor()); } } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 8b1efd6fc4..c1df68fde9 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -21,6 +21,6 @@ public void draw() { + getArea() + " sq. units, firstLeg: " + this.firstLeg + ", secondLeg: " + this.secondLeg + " units, color: " - + this.color); + + getColor()); } } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 4d15e91eb5..517e47ece5 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -7,7 +7,7 @@ public class Square extends Figure { public Square() { } - public Square(String color, int site) { + public Square(String color, double site) { super(color); this.site = site; } @@ -21,6 +21,6 @@ public double getArea() { public void draw() { System.out.println("Figure: square, area: " + getArea() + " sq. units, side: " - + this.site + " units, color: " + this.color); + + this.site + " units, color: " + getColor()); } }