Define a class, mcmLength, to represent a length measured in meters, centimeters, and millimeters, each stored as integers. Include methods to add and subtract objects, to multiply and divide an object by an integer value, to calculate an area resulting from the product of two objects, and to compare objects. Include constructors that accept three arguments—meters, centimeters, and millimeters; one integer argument in millimeters; one double argument in centimeters; and no arguments, which creates an object with the length set to zero. Check the class by creating some objects and testing the class operations.
Output of Above Java Program
Length 0 is 2m 74cm 7mm
Length 1 is 0m 27cm 4mm
Length 2 is 274m 2cm 3mm
Length 3 is 0m 0cm 0mm
Addition: 2m 74cm 7mm plus 0m 27cm 4mm is 3m 2cm 1mm
Subtraction: 2m 74cm 7mm minus 0m 27cm 4mm is 2m 47cm 3mm
Multiplication: 2m 74cm 7mm times 10 is 27m 47cm 0mm
Division: 2m 74cm 7mm divided by 10 is 0m 27cm 4mm
Area: 2m 74cm 7mm by 0m 27cm 4mm is 752678 square mm
Length 2m 74cm 7mm is greater than length 0m 27cm 4mm
Length 2m 74cm 7mm is greater than length 0m 27cm 4mm
Length 0m 27cm 4mm is less than length 274m 2cm 3mm
Length 274m 2cm 3mm is greater than length 0m 0cm 0mm
public class mcmLength { public static final int CM_PER_M = 100; public static final int MM_PER_CM = 10; public static final int MM_PER_M = MM_PER_CM*CM_PER_M; // Private member variables: private int meters = 0; private int centimeters = 0; private int millimeters = 0; // Constructors: public mcmLength(double cm) { this((int)Math.round(cm*MM_PER_CM)); } public mcmLength(int mm) { meters = mm/MM_PER_M; centimeters = (mm - meters*MM_PER_M)/MM_PER_CM; millimeters = mm - meters*MM_PER_M - centimeters*MM_PER_CM; } // If we were to just store the argument values, we could // end up with invalid mm and cm values in the object if the // values passed as arguments are not valid. // With the approach here we guarantee all values are valid // in the object that is created. public mcmLength(int m, int cm, int mm) { this(m*MM_PER_M + cm*MM_PER_CM + mm); } public mcmLength(){} // Methods // Replaces the default toString method in Object: public String toString() { return Integer.toString(meters) + "m " + centimeters + "cm " + millimeters + "mm"; } public int toMM() { return meters*MM_PER_M + centimeters*MM_PER_CM + millimeters; } public double toMeters() { return meters + ((double)(centimeters))/CM_PER_M + ((double)(millimeters))/MM_PER_M; } // All of the following methods use the toMM(): public mcmLength add(mcmLength length) { return new mcmLength(toMM()+length.toMM()); } public mcmLength subtract(mcmLength length) { return new mcmLength(toMM()-length.toMM()); } public mcmLength multiply(int n) { return new mcmLength(n*toMM()); } public mcmLength divide(int y) { return new mcmLength(toMM()/y); } //Calculate area in square mm public long area(mcmLength length) { return (toMM()*length.toMM()); } // Compare two lengths // Return value is 1 if current greater than arg // 0 if current equal to arg // -1 if current less than arg public int compare(mcmLength length) { return greaterThan(length) ? 1 : (equals(length) ? 0 : -1); } public boolean equals(mcmLength length) { return toMM() == length.toMM(); } public boolean lessThan(mcmLength length) { return toMM() < length.toMM(); } public boolean greaterThan(mcmLength length) { return toMM() > length.toMM(); } }This is the main Class to test mcmLength Class.
public class TestLengths { public static void main(String args[]) { mcmLength[] lengths = new mcmLength[4]; // Test the constructors: lengths[0] = new mcmLength(274.65); lengths[1] = new mcmLength(274); lengths[2] = new mcmLength(274,2,3); lengths[3] = new mcmLength(); //Display the figures: for(int i = 0 ; i < lengths.length ; ++i) { System.out.println("Length " + i + " is " + lengths[i]); } // Test the arithmetic and area operations System.out.println("Addition: " + lengths[0] + " plus " + lengths[1] + " is " + lengths[0].add(lengths[1])); System.out.println("Subtraction: " + lengths[0] + " minus " + lengths[1]+ " is " + lengths[0].subtract(lengths[1])); System.out.println("Multiplication: " + lengths[0] + " times 10 is " + lengths[0].multiply(10)); System.out.println("Division: " + lengths[0] + " divided by 10 is " + lengths[0].divide(10)); System.out.println("Area: " + lengths[0] + " by " + lengths[1] + " is " + lengths[0].area(lengths[1]) + " square mm"); // Test comparison methods if(lengths[0].greaterThan(lengths[1])) { System.out.println("Length "+ lengths[0] + " is greater than length " + lengths[1]); } else if(lengths[0].lessThan(lengths[1])) { System.out.println("Length "+ lengths[0] + " is less than length " + lengths[1]); } else { System.out.println("Length "+ lengths[0] + " is the same length as length " + lengths[1]); } // Compare successive lengths using compare() method String compareStr = null; for(int i = 0 ; i < lengths.length-1 ; ++i) { switch(lengths[i].compare(lengths[i+1])) { case -1: compareStr = " is less than length "; break; case 0: compareStr = " is equal to length "; break; case 1: compareStr = " is greater than length "; break; } System.out.println("Length " + lengths[i] + compareStr + lengths[i+1]); } } }
Output of Above Java Program
Length 0 is 2m 74cm 7mm
Length 1 is 0m 27cm 4mm
Length 2 is 274m 2cm 3mm
Length 3 is 0m 0cm 0mm
Addition: 2m 74cm 7mm plus 0m 27cm 4mm is 3m 2cm 1mm
Subtraction: 2m 74cm 7mm minus 0m 27cm 4mm is 2m 47cm 3mm
Multiplication: 2m 74cm 7mm times 10 is 27m 47cm 0mm
Division: 2m 74cm 7mm divided by 10 is 0m 27cm 4mm
Area: 2m 74cm 7mm by 0m 27cm 4mm is 752678 square mm
Length 2m 74cm 7mm is greater than length 0m 27cm 4mm
Length 2m 74cm 7mm is greater than length 0m 27cm 4mm
Length 0m 27cm 4mm is less than length 274m 2cm 3mm
Length 274m 2cm 3mm is greater than length 0m 0cm 0mm