Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.noynaert.csc254;
- public class Rectangle {
- double width;
- double length;
- //Constructors
- public Rectangle(double width, double length){
- setWidth(width);
- setLength(length);
- }
- public Rectangle(double side){
- this(side, side);
- }
- public Rectangle(){
- this(1.0,1.0);
- }
- //setters
- public void setLength(double length){
- this.length = (length <= 0.0) ? 1.0 : length;
- check();
- }
- public void setWidth(double width) {
- this.width = width <=0 ? 1.0 : width;
- check();
- }
- //getters
- public double getWidth(){
- return width;
- }
- public double getLength(){
- return length;
- }
- private void check(){
- if(length < width){
- double temp = length;
- length = width;
- width = temp;
- }
- }
- public double getArea(){
- return width * length;
- }
- public boolean isSquare(){
- boolean square=length==width ? true : false;
- return square;
- }
- public double getPerimeter(){
- return width + length + length + width;
- }
- public String toString() {
- String result = String.format("%1.4f x %1.4f", length, width);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment