Skip to content
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
67 changes: 67 additions & 0 deletions Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.Objects;

/*
Написати клас Rectangle , в якому буде описано поля : довжина, ширина. Описати дані поля в конструкторі. Створити конструктор з параметрами і без параметрів. Написати методи, які будуть
розраховувати площу та периметр. В Main класі продемонструвати створення об’єктів використовуючи два конструктори. Вивести на консоль площу і периметр прямокутника. Вивід на консолі повинен
мати наступний вигляд: «Площа прямокутника = тут буде площа », «Периметр прямокутника = тут буде периметр».
*/
public class Rectangle {
private int hight = 0;
private int width = 0;

public Rectangle(int hight, int width) {
this.hight = hight;
this.width = width;
}

public Rectangle() {
//System.out.println("Увага! параметри не задані. Задайте параметри...");
}

public int getHight() {
return hight;
}

public void setHight(int hight) {
this.hight = hight;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Rectangle)) return false;
Rectangle rectangle = (Rectangle) o;
return getHight() == rectangle.getHight() &&
getWidth() == rectangle.getWidth();
}

@Override
public int hashCode() {
return Objects.hash(getHight(), getWidth());
}

@Override
public String toString() {
return "Rectangle{" +
"hight=" + hight +
", width=" + width +
'}';
}

/*
public void perimeter(int hight, int width) {
int p = (hight * width);
};

public void square(int hight, int width) {
int s = ((hight * 2) + (width * 2));
};*/
}
34 changes: 34 additions & 0 deletions RectangleExecute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class RectangleExecute {
public static void main(String[] args) {
int perimeter = 0;
int square = 0;

Rectangle rectangle = new Rectangle(6, 8);
Rectangle rectangle1 = new Rectangle();


System.out.println("Задані параметри довжини -> " + rectangle.getWidth() + " а ширини " + rectangle.getHight());
if (rectangle.getWidth() <= 0 || rectangle.getHight() <= 0) {
System.out.println("Увага! параметри не задані (не може бути 0). Задайте параметри...\n");
} else {
perimeter = rectangle.getHight() * rectangle.getWidth();
square = rectangle.getHight() * 2 + rectangle.getWidth() * 2;

System.out.println("Периметр прямокутника = тут буде периметр " + perimeter);
System.out.println("Площа прямокутника = тут буде площа " + square);
}

System.out.println("------------------------------------------------------------");

System.out.println("Задані параметри довжини -> " + rectangle1.getWidth() + " а ширини " + rectangle1.getHight());
if (rectangle1.getWidth() <= 0 || rectangle1.getHight() <= 0) {
System.out.println("Увага! параметри не задані (не може бути 0). Задайте параметри...\n");
} else {
perimeter = rectangle1.getHight() * rectangle1.getWidth();
square = rectangle1.getHight() * 2 + rectangle1.getWidth() * 2;

System.out.println("Периметр прямокутника = тут буде периметр " + perimeter);
System.out.println("Площа прямокутника = тут буде площа " + square);
}
}
}