JAVA: The Rectangle Class
Tarih: 29 Aralık 2013 Yorum: 0

(The Rectangle class) Design a class named Rectangle to represent a rectangle.  The class contains:

  • Two double data fields named width and height that specify the width and height of the rectangle.  The default values are 1 for both width and height.
  • A no-arg constructor that creates a rectangle with default values.
  • A constructor that creates a rectangle with the specified width and height.
  • A method named getArea() that returns the area of this rectangle.
  • A method named getPerimeter() that returns the perimeter.
  • The toString() method that returns a string representation of a Rectangle object
  • Write a test program, TestRectangle, that creates two Rectangle objects – one with width 4 and height 40 and the other with width 3.5 and height 35.9.  Display the width, height, area, and the perimeter of each rectangle in this order.

 

Rectangle.java

/**
 * Rectangle Area and Perimeter. 
 */
public class Rectangle {
    // Declare and initialize default values.
   double width		= 4.0;
   double height	= 4.0;

   	// Create constructor with default values.
   	public Rectangle (){

   	}

   	// Create constructor with new width and height values.
   	public Rectangle (double width, double height){
   		if(width > 0 && height > 0){
   			this.width 	= width;
   			this.height	= height;
   		}else{
   			this.width 	= 0;
   			this.height	= 0;
   		} 	
   	}

   	// Calculate Area.
   	public double getArea(){
   		return this.width * this.height;
   	}

   	// Calculate Perimeter.
   	public double getPerimeter(){
   		return 2 * (this.width + this.height);
   	}

   	// Pepresentation method of Rectangle object.
   	public String toString(){
   		String result;
   		if(this.width > 0 && height > 0){
   			result = "The Rectangle Width: " + this.width + ", Height: " + this.height + ", Area: " + this.getArea() + ", and Perimeter: " + this.getPerimeter();
   		}else if(this.width > 0 && height <= 0){
   			result = "The height must be greater than zero.";
   		}else if(this.width <= 0 && height > 0){
   			result = "The width must be greater than zero.";
   		}else{
   			result = "The width and height must be greater than zero.";
   		}
   		return result;
   	}

} // End of Rectangle class.

 

TestRectangle.java

/**
 * Rectangle Area and Perimeter. 
 */
public class TestRectangle{

	/** main Method */
	public static void main(String [] args){

		// Create Rectangle object.
		Rectangle rect1	= new Rectangle(-4,-40);
		Rectangle rect2	= new Rectangle(3.5,35.9);

			// Print Rectangle object values.
			System.out.println(rect1);
			System.out.println(rect2);

	} // End of main method

} // End of TestRectangle class.



Yorum Yok:


Yorum Yap:

Yorum yapabilmek için giriş yapmalısınız.




tema yapımcısı wordpress alexa bilgileri Webmaster Creative Commons v3 ile Lisanslanmıştır!


Akif ARSLAN 2012 - 2014 © Hiçbir Hakkı Saklı Değildir.
Sitede bulunan istediğiniz cümleyi veya içeriği, istediğiniz gibi, istediğiniz yerde, istediğiniz zaman ve istediğiniz kişilerle paylaşabilirsiniz.