Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1. class Rectangle {
  2.  
  3.     /** Поле ширина */
  4.     private double width;
  5.  
  6.     /** Поле висота */
  7.     private double height;
  8.  
  9.     /**
  10.      * Конструктор - створення нового об'єкту
  11.      */
  12.     Rectangle() {
  13.         height = 0;
  14.         width = 0;
  15.     }
  16.  
  17.     /**
  18.      * Конструктор - створення нового об'єкту з певними значеннями
  19.      * @param width - ширина
  20.      * @param height - вистоа
  21.      */
  22.     Rectangle(double width, double height) {
  23.         this.setWidth(width);
  24.         this.setHeight(height);
  25.     }
  26.  
  27.     /**
  28.      * Функція отримання значення поля {@link Rectangle#width}
  29.      * @return повертає ширину прямокутника
  30.      */
  31.  
  32.     public double getWidth() {
  33.         return width;
  34.     }
  35.  
  36.     /**
  37.      * Функція отримання значення поля {@link Rectangle#height}
  38.      * @return повертає висоту прямокутника
  39.      */
  40.  
  41.     public double getHeight() {
  42.         return height;
  43.     }
  44.  
  45.     /**
  46.      * Процедура визначення висоти прямокутника {@link Rectangle#width}
  47.      * @param width  - висота прямокутника
  48.      */
  49.  
  50.     public void setWidth(double width) {
  51.         if (isValid(width)) {
  52.             this.width = width;
  53.         } else {
  54.             this.width = 0;
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Процедура визначення висоти прямокутника {@link Rectangle#height}
  60.      * @param height - висота прямокутника
  61.      */
  62.  
  63.     public void setHeight(double height) {
  64.         if (isValid(height)) {
  65.             this.height = height;
  66.         } else {
  67.             this.height = 0;
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Функція для перевірки корректності поля
  73.      * @return повертає {@code true} у випадку якщо значення коректне;
  74.      *                  {@code false} в іншому випадку.
  75.      */
  76.  
  77.     private boolean isValid(double value) {
  78.         if (value > 0) {
  79.             return true;
  80.         } else {
  81.             System.out.println("Параметр не може бути відємним");
  82.             return  false;
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Функція визначення площі прямокутника
  88.      */
  89.  
  90.     public double square() {
  91.         return  height * width;
  92.     }
  93.  
  94.     /**
  95.      * Функція визначення периметра прямокутника
  96.      */
  97.  
  98.     public double perimeter() {
  99.         return  (height * 2) + (width * 2);
  100.     }
  101.  
  102.     /**
  103.      * Процедура для друку всіх значень прямокутника
  104.      */
  105.  
  106.     public void print() {
  107.         System.out.println(this.toString());
  108.     }
  109.  
  110.     /**
  111.      * Функція що повертає опис об'єкту класа
  112.      * @return повертає опис об'єкту класа
  113.      */
  114.  
  115.     @Override
  116.     public String toString() {
  117.         return "Rectangle{" +
  118.                 "height='" + height + '\'' +
  119.                 ", color='" + width + '\'' +
  120.                 ", square ='" + square() + '\'' +
  121.                 ", perimeter='" + perimeter() + '\'' +
  122.                 '}';
  123.     }
  124.  
  125.     /**
  126.      * Функція для порівняння об'єктів класу
  127.      * @return {@code true} якщо цей об'єкт однаковий з аргументом;
  128.      *         {@code false} в іншому випадку.
  129.      */
  130.  
  131.     @Override
  132.     public boolean equals(Object object) {
  133.         if (this == object) return true;
  134.         if (object == null || getClass() != object.getClass()) return false;
  135.         Rectangle rectangle = (Rectangle) object;
  136.         return Double.compare(rectangle.height, height) == 0 &&
  137.                 Double.compare(rectangle.width, width) == 0;
  138.     }
  139.  
  140.     /**
  141.      * Функція для повернення хешкоду значення об'єкту
  142.      * @return хеш значення цього об'єкту
  143.      */
  144.  
  145.     @Override
  146.     public int hashCode() {
  147.         return Objects.hash(width, height);
  148.     }
  149.  
  150.     /**
  151.      * Функція створює і повертає копію об'єкту
  152.      * @return повертає копію об'єкту
  153.      */
  154.  
  155.     @Override
  156.     protected Object clone() throws CloneNotSupportedException {
  157.         return super.clone();
  158.     }
  159. }
  160.  
  161. public class Main {
  162.  
  163.     public static void main(String[] args) throws CloneNotSupportedException {
  164.         Rectangle rectangle = new Rectangle();
  165.         rectangle.print();
  166.  
  167.         rectangle.setHeight(-10);
  168.         rectangle.setWidth(4);
  169.         rectangle.print();
  170.  
  171.         rectangle.setHeight(10);
  172.         rectangle.print();
  173.  
  174.         Rectangle clone = (Rectangle) rectangle.clone();
  175.         clone.setHeight(3);
  176.  
  177.         rectangle.print();
  178.         clone.print();
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement