Luninariel

Rectangle.java

Oct 29th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. package com.noynaert.csc254;
  2.  
  3. public class Rectangle {
  4.     double width;
  5.     double length;
  6.  
  7.     //Constructors
  8.     public Rectangle(double width, double length){
  9.         setWidth(width);
  10.         setLength(length);
  11.     }
  12.     public Rectangle(double side){
  13.         this(side, side);
  14.     }
  15.     public Rectangle(){
  16.         this(1.0,1.0);
  17.     }
  18.  
  19.  
  20.     //setters
  21.     public void setLength(double length){
  22.         this.length = (length <= 0.0) ? 1.0 : length;
  23.         check();
  24.  
  25.     }
  26.  
  27.     public void setWidth(double width) {
  28.         this.width = width <=0 ? 1.0 : width;
  29.         check();
  30.     }
  31.  
  32.     //getters
  33.     public double getWidth(){
  34.         return width;
  35.     }
  36.  
  37.     public double getLength(){
  38.  
  39.         return length;
  40.     }
  41.  
  42.     private void check(){
  43.         if(length < width){
  44.             double temp = length;
  45.             length = width;
  46.             width = temp;
  47.         }
  48.     }
  49.     public double getArea(){
  50.         return width * length;
  51.     }
  52.  
  53.     public boolean isSquare(){
  54.         boolean square=length==width ? true : false;
  55.         return square;
  56.     }
  57.  
  58.     public double getPerimeter(){
  59.         return width + length + length + width;
  60.  
  61.     }
  62.     public String toString() {
  63.         String result = String.format("%1.4f x %1.4f", length, width);
  64.         return result;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment