Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. /*************************************************
  2. *
  3. *File: TwoNumbers.java
  4. *
  5. *Author: Zach Barnett
  6. *
  7. *Description: Rectangles!
  8. *
  9. *Date 6/28/2017
  10. *
  11. *************************************************/
  12.  
  13. public class Rectangle extends Object
  14. {
  15.  
  16. /* INITIAL STATE */
  17.  
  18. private final int DEFAULT_LENGTH = 2;
  19. private final int DEFAULT_WIDTH = 1;
  20.  
  21. private int length = 0;
  22. private int width = 0;
  23.  
  24. /* BUILDERS */
  25.  
  26. public Rectangle ()
  27. {
  28. this.length = DEFAULT_LENGTH;
  29. this.width = DEFAULT_WIDTH;
  30.  
  31. }
  32. public Rectangle ( int length, int width )
  33. {
  34. this.setDimensions(length,width);
  35. }
  36.  
  37. /* GETTERS */
  38.  
  39. public int getLength ()
  40. {
  41. return this.length;
  42. }
  43.  
  44. public int getWidth ()
  45. {
  46. return this.width;
  47. }
  48.  
  49. public int getPerimeter()
  50. {
  51. int perimeter = 0;
  52. perimeter = 2*(this.length + this.width);
  53. return perimeter;
  54. }
  55.  
  56. public int getArea()
  57. {
  58. int area;
  59. area = this.length * this.width;
  60. return area;
  61. }
  62.  
  63. @Override
  64. public String toString ()
  65. {
  66. return "Length: " + this.getLength () + " " + "Width: " + this.getWidth();
  67. }
  68.  
  69.  
  70. /* CHANGERS */
  71.  
  72. public void setDimensions (int length, int width)
  73. {
  74. this.setLength(length);
  75. this.setWidth(width);
  76. }
  77.  
  78. public void setLength(int length)
  79. {
  80. this.length=length;
  81. }
  82.  
  83. public void setWidth (int width)
  84. {
  85. this.width=width;
  86. }
  87.  
  88. /* MAIN ROUTINE */
  89.  
  90. public static void main( String args[] )
  91. {
  92. Rectangle r1 = new Rectangle () ;
  93. Rectangle r2 = new Rectangle (4, 7) ;
  94. displayRectangle("1", r1);
  95. displayRectangle("2", r2);
  96. System.out.println();
  97. }
  98.  
  99. private static void displayRectangle(String id, Rectangle r)
  100. {
  101. final String STRING_FORMAT = "%15s ";
  102. final String STRING_INTEGER = STRING_FORMAT + "%7d ";
  103. final String EXPRESSION = "%5d %1s % 5d";
  104.  
  105. final String NL = "%n" ;
  106. final String AREA_EXPRESSION = STRING_INTEGER + "=" + EXPRESSION + NL;
  107. final String PERIMETER_EXPRESSION = STRING_INTEGER + "=" + EXPRESSION + "%1s" + EXPRESSION;
  108. final String STRING_STRING = STRING_FORMAT + "%s%n" ;
  109.  
  110. System.out.println();
  111.  
  112. System.out.printf(STRING_INTEGER + NL, "Length:", r.getLength() );
  113. System.out.printf(STRING_INTEGER + NL, "Width:", r.getWidth() );
  114.  
  115. System.out.println();
  116.  
  117. System.out.printf(PERIMETER_EXPRESSION,
  118. "Perimeter:",
  119. r.getPerimeter(),
  120. r.getLength(),
  121. "+",
  122. r.getWidth(),
  123. "+",
  124. r.getLength(),
  125. "+",
  126. r.getWidth()
  127. );
  128.  
  129. System.out.printf(AREA_EXPRESSION,
  130. "Area:",
  131. r.getArea(),
  132. r.getLength(),
  133. "*",
  134. r.getWidth()
  135. );
  136.  
  137. System.out.println();
  138.  
  139. System.out.printf( STRING_STRING, "toString ():", r.toString() );
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement