Posted By

joshnunn on 08/12/09


Tagged

basic class command constructor query rectangle learning area private perimeter


Versions (?)

Rectangle.java


 / Published in: Java
 

URL: http://nunnone.com

This is a program to implement a Rectangle. Private fields record the length and width and methods calculate the area and perimeter. It sets a default length and width if one is not given.

  1. public class Rectangle {
  2. private double length;
  3. private double width;
  4. private double area;
  5. private double perimeter;
  6.  
  7. public Rectangle(){
  8. this.length = 1.0;
  9. this.width = 1.0;
  10. }
  11.  
  12. public Rectangle(double theLength, double theWidth){
  13. length = theLength;
  14. width = theWidth;
  15. }
  16.  
  17. public double getArea() {
  18. area = width * length;
  19. return area;
  20. }
  21.  
  22. public double getPerimeter(){
  23. perimeter = (width * 2) + (length * 2);
  24. return perimeter;
  25. }
  26. }

Report this snippet  

You need to login to post a comment.