Advertisement
Vendrick-Xander

Chapter 3 Test Program

Dec 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. Here is the object class:
  2. package com.Suarez;
  3. /* object class
  4.  
  5. */
  6. public class Box
  7. {
  8. private int W;
  9. private int L;
  10. private int H;
  11.  
  12. public Box()//default constructor
  13. {
  14. W=1;
  15. L=1;
  16. H=1;
  17. }
  18. public Box(int W1, int L1,int H1) // lets you set the value for the W & L & H of the box
  19. {
  20. W=W1;
  21. L=L1;
  22. H=H1;
  23. }
  24. public void setW(int W1)//mutator for W
  25. {
  26. W = W1;
  27. }
  28. public void setL(int L1)//mutator for L
  29. {
  30. L = L1;
  31. }
  32. public void setH(int H1) // mutator for H
  33. {
  34. H = H1;
  35. }
  36. public int getW()//assessor for W
  37. {
  38. return W;
  39. }
  40. public int getL()//assessor for L
  41. {
  42. return L;
  43. }
  44. public int getH() // assessor for H
  45. {
  46. return H;
  47. }
  48. public void translateSize(int dW,int dL, int dH)
  49. {
  50. W = W + dW;
  51. L = L + dL;
  52. H = H + dH;
  53. }
  54. public int calculateArea()
  55. {
  56. int area = W * L;
  57. return area;
  58. }
  59. public int calculateVolume()
  60. {
  61. int volume = W * L * H;
  62. return volume;
  63. }
  64. public String boxToString()
  65. {
  66. String box = ("The width of the box is " + W + ", the length is " + L +", and the height is " + H);
  67. return box;
  68. }
  69. }
  70.  
  71. Here is the client class:
  72. package com.Suarez;
  73.  
  74. public class ClientBox {
  75. public static void main(String [] args)
  76. {
  77. Box b1 = new Box(2, 4, 3);
  78. Box b2 = new Box(5, 2, 12);
  79.  
  80. //the code below will output the information for box b1
  81. System.out.println("Box b1 has the following dimensions and values");
  82. System.out.println(b1.boxToString());
  83. System.out.println("The box has an area of " + b1.calculateArea());
  84. System.out.println("The box has a volume of " + b1.calculateVolume());
  85.  
  86. // this is so that the two boxes will be seperated
  87. System.out.println();
  88.  
  89. // the code below will output the information for box b2
  90. System.out.println("Box b2 has the following dimensions and values");
  91. System.out.println(b2.boxToString());
  92. System.out.println("The box has an area of " + b2.calculateArea());
  93. System.out.println("The box has a volume of " + b2.calculateVolume());
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement