Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Rectangle {
- /** Поле ширина */
- private double width;
- /** Поле висота */
- private double height;
- /**
- * Конструктор - створення нового об'єкту
- */
- Rectangle() {
- height = 0;
- width = 0;
- }
- /**
- * Конструктор - створення нового об'єкту з певними значеннями
- * @param width - ширина
- * @param height - вистоа
- */
- Rectangle(double width, double height) {
- this.setWidth(width);
- this.setHeight(height);
- }
- /**
- * Функція отримання значення поля {@link Rectangle#width}
- * @return повертає ширину прямокутника
- */
- public double getWidth() {
- return width;
- }
- /**
- * Функція отримання значення поля {@link Rectangle#height}
- * @return повертає висоту прямокутника
- */
- public double getHeight() {
- return height;
- }
- /**
- * Процедура визначення висоти прямокутника {@link Rectangle#width}
- * @param width - висота прямокутника
- */
- public void setWidth(double width) {
- if (isValid(width)) {
- this.width = width;
- } else {
- this.width = 0;
- }
- }
- /**
- * Процедура визначення висоти прямокутника {@link Rectangle#height}
- * @param height - висота прямокутника
- */
- public void setHeight(double height) {
- if (isValid(height)) {
- this.height = height;
- } else {
- this.height = 0;
- }
- }
- /**
- * Функція для перевірки корректності поля
- * @return повертає {@code true} у випадку якщо значення коректне;
- * {@code false} в іншому випадку.
- */
- private boolean isValid(double value) {
- if (value > 0) {
- return true;
- } else {
- System.out.println("Параметр не може бути відємним");
- return false;
- }
- }
- /**
- * Функція визначення площі прямокутника
- */
- public double square() {
- return height * width;
- }
- /**
- * Функція визначення периметра прямокутника
- */
- public double perimeter() {
- return (height * 2) + (width * 2);
- }
- /**
- * Процедура для друку всіх значень прямокутника
- */
- public void print() {
- System.out.println(this.toString());
- }
- /**
- * Функція що повертає опис об'єкту класа
- * @return повертає опис об'єкту класа
- */
- @Override
- public String toString() {
- return "Rectangle{" +
- "height='" + height + '\'' +
- ", color='" + width + '\'' +
- ", square ='" + square() + '\'' +
- ", perimeter='" + perimeter() + '\'' +
- '}';
- }
- /**
- * Функція для порівняння об'єктів класу
- * @return {@code true} якщо цей об'єкт однаковий з аргументом;
- * {@code false} в іншому випадку.
- */
- @Override
- public boolean equals(Object object) {
- if (this == object) return true;
- if (object == null || getClass() != object.getClass()) return false;
- Rectangle rectangle = (Rectangle) object;
- return Double.compare(rectangle.height, height) == 0 &&
- Double.compare(rectangle.width, width) == 0;
- }
- /**
- * Функція для повернення хешкоду значення об'єкту
- * @return хеш значення цього об'єкту
- */
- @Override
- public int hashCode() {
- return Objects.hash(width, height);
- }
- /**
- * Функція створює і повертає копію об'єкту
- * @return повертає копію об'єкту
- */
- @Override
- protected Object clone() throws CloneNotSupportedException {
- return super.clone();
- }
- }
- public class Main {
- public static void main(String[] args) throws CloneNotSupportedException {
- Rectangle rectangle = new Rectangle();
- rectangle.print();
- rectangle.setHeight(-10);
- rectangle.setWidth(4);
- rectangle.print();
- rectangle.setHeight(10);
- rectangle.print();
- Rectangle clone = (Rectangle) rectangle.clone();
- clone.setHeight(3);
- rectangle.print();
- clone.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement