Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Length {
- private double cm;
- /**
- * Setter method for centimeters.
- * @param cm The new length in centimeters.
- */
- public void setCentimeters(double cm) {
- this.cm = cm;
- }
- /**
- * Getter method for centimeters.
- * @return The current length in centimeters.
- */
- public double getCentimeters() {
- return this.cm;
- }
- // First the conversion factor: to declare a field static means that it is
- // shared between all objects of this class, and to declare it final means
- // that it is a "named constant" whose value cannot be changed later. You
- // should prefer named constants to "magic numbers" hardwired in code.
- private static final double CM_PER_INCH = 2.54;
- /**
- * Setter method for inches.
- * @param inches The new length in inches.
- */
- public void setInches(double inches) {
- // Compute the length in inches
- this.cm = inches * CM_PER_INCH;
- }
- /**
- * Getter method for inches.
- * @return The current length in inches.
- */
- public double getInches() {
- return this.cm / CM_PER_INCH;
- }
- /**
- * The String representation of this Length object.
- * @return The length stored in this object in centimeters.
- */
- public String toString() {
- return this.cm + " cm";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement