Advertisement
JackHoughton00

Rectangle Question

Apr 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. package question4;
  2.  
  3. public class Question4 {
  4.  
  5. public static void main(String[] args) {
  6. Rectangle spot = new Rectangle();
  7. Rectangle spot2 = new Rectangle();
  8. spot.setLength(3);
  9. spot.lengthwidth();
  10. spot2.lengthwidth(4, 7);
  11. spot.area();
  12. System.out.println(spot);
  13.  
  14. }
  15.  
  16. }
  17. Rectangle Class
  18. package question4;
  19.  
  20. public class Rectangle {
  21. private double length;
  22. private double width;
  23. private double area;
  24.  
  25. public Rectangle() {
  26. length = 1;
  27. width = 1;
  28. }
  29. public Rectangle(double l, double w) {
  30. length = l;
  31. width = w;
  32. }
  33. public double getLength(){
  34. return(length);
  35. }
  36. public double getWidth() {
  37. return(width);
  38. }
  39. public void setLength(double newLength) {
  40. length = newLength;
  41. }
  42. public void setWidth(double newWidth) {
  43. width = newWidth;
  44. }
  45.  
  46. public void lengthwidth() {
  47. System.out.println("The length is "+length+" and the width is "+width);
  48.  
  49. }
  50. public void lengthwidth(double l, double w) {
  51. System.out.println("The length is "+l+"and the width is "+w);
  52. }
  53. public void area() {
  54. area = (length * width);
  55. System.out.println("The area of the rectange is "+area);
  56. }
  57. public void area(double l,double w) {
  58. area = (l*w);
  59. System.out.println("The area of the rectange is "+ area);
  60. }
  61. public boolean equals(Object r) {
  62. Rectangle testObj = (Rectangle)r;
  63. if (testObj.getLength() == length )
  64. return(true);
  65. else
  66. return(false);
  67. }
  68. public String toString() {
  69. String rectangleString;
  70. rectangleString = "Rectangle has length " + length+" width "+width;
  71. return(rectangleString);
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement