Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public class Length {
  2.  
  3. private double cm;
  4.  
  5. /**
  6. * Setter method for centimeters.
  7. * @param cm The new length in centimeters.
  8. */
  9. public void setCentimeters(double cm) {
  10. this.cm = cm;
  11. }
  12.  
  13. /**
  14. * Getter method for centimeters.
  15. * @return The current length in centimeters.
  16. */
  17. public double getCentimeters() {
  18. return this.cm;
  19. }
  20.  
  21. // First the conversion factor: to declare a field static means that it is
  22. // shared between all objects of this class, and to declare it final means
  23. // that it is a "named constant" whose value cannot be changed later. You
  24. // should prefer named constants to "magic numbers" hardwired in code.
  25. private static final double CM_PER_INCH = 2.54;
  26.  
  27. /**
  28. * Setter method for inches.
  29. * @param inches The new length in inches.
  30. */
  31. public void setInches(double inches) {
  32. // Compute the length in inches
  33. this.cm = inches * CM_PER_INCH;
  34. }
  35.  
  36. /**
  37. * Getter method for inches.
  38. * @return The current length in inches.
  39. */
  40. public double getInches() {
  41. return this.cm / CM_PER_INCH;
  42. }
  43.  
  44. /**
  45. * The String representation of this Length object.
  46. * @return The length stored in this object in centimeters.
  47. */
  48. public String toString() {
  49. return this.cm + " cm";
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement