Monty374

Untitled

Nov 3rd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. package rectangle;
  2.  
  3. public class Rectangle {
  4. private int length, width;
  5.  
  6. public Rectangle() {
  7. length = 2;
  8. width = 1;
  9. }
  10.  
  11. public Rectangle(int l, int w) {
  12. length = l;
  13. width = w;
  14.  
  15. }
  16.  
  17. public int perimeter() {
  18. int perimeter;
  19. perimeter = 2 * length + 2 * width;
  20. return(perimeter);
  21. }
  22.  
  23. public int area() {
  24. int area;
  25. area = length * width;
  26. return(area);
  27. }
  28.  
  29. public static void formula(){
  30. System.out.println("Area formula: a = l * w");
  31. }
  32.  
  33. public int getLength(){
  34. return(length);
  35. }
  36.  
  37. public int getWidth(){
  38. return(width);
  39. }
  40.  
  41.  
  42. public boolean equals(Object c){
  43. Rectangle testObj = (Rectangle)c;
  44.  
  45. if(testObj.getLength() == length){
  46. if(testObj.getWidth() == width){
  47. return(true);
  48. } else {
  49. return(false);
  50. }
  51. } else {
  52. return(false);
  53. }
  54. }
  55.  
  56. public String toString(){
  57. String rectString;
  58.  
  59. rectString = "Rectangle has length " + length + " and width " + width;
  60. return(rectString);
  61. }
  62.  
  63. public static void main(String[] args) {
  64. Rectangle box1 = new Rectangle(2, 3);
  65. Rectangle box2 = new Rectangle(4, 5);
  66.  
  67. System.out.println("Perimeter: " + box.perimeter());
  68. System.out.println("Area: " + box.area());
  69. Rectangle3.formula();
  70.  
  71. if (box1.equals(box2)){
  72. System.out.println("Objects are equal.");
  73. } else {
  74. System.out.println("Objects are not equal.");
  75. }
  76.  
  77. System.out.println(box1);
  78. System.out.println(box2);
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment