Define a class, tkgWeight, to represent a weight in tons, kilograms, and grams and Demonstrate this class by creating and combining some class objects.
This is Main Class to Test above tkgWeight Class.
Output of Above Java Program
public class tkgWeight { public static final int KG_PER_TON = 1000; public static final int GRAMS_PER_KG = 1000; public static final int GRAMS_PER_TON = GRAMS_PER_KG*KG_PER_TON; // private member variables private int tons = 0; private int kilograms = 0; private int grams = 0; // Constructors: public tkgWeight(double kg) { this((int)Math.round(kg*GRAMS_PER_KG)); } public tkgWeight(int g) { tons = g/GRAMS_PER_TON; kilograms = (g - tons*GRAMS_PER_TON)/GRAMS_PER_KG; grams = g - tons*GRAMS_PER_TON - kilograms*GRAMS_PER_KG; } public tkgWeight(int t, int kg, int g) { this(t*GRAMS_PER_TON + kg*GRAMS_PER_KG + g); } public tkgWeight(){} // Methods public String toString() { return Integer.toString(tons) + "t " + kilograms + "kg " + grams + "g"; } public int toGrams() { return tons*GRAMS_PER_TON + kilograms*GRAMS_PER_KG + grams; } public double toKilograms() { return ((double)toGrams())/GRAMS_PER_KG; } public tkgWeight add(tkgWeight weight) { return new tkgWeight(toGrams() + weight.toGrams()); } public tkgWeight subtract(tkgWeight weight) { return new tkgWeight(toGrams() - weight.toGrams()); } public tkgWeight multiply(int n) { return new tkgWeight(n*toGrams()); } public tkgWeight divide(int n) { return new tkgWeight(toGrams()/n); } // Compare two weights // Return value is 1 if current greater than arg // 0 if current equal to arg // -1 if current less than arg public int compare(tkgWeight weight) { return greaterThan(weight) ? 1 : (equals(weight) ? 0 : -1); } public boolean equals(tkgWeight weight) { return toGrams() == weight.toGrams(); } public boolean lessThan(tkgWeight weight) { return toGrams() < weight.toGrams(); } public boolean greaterThan(tkgWeight weight) { return toGrams() > weight.toGrams(); } }
This is Main Class to Test above tkgWeight Class.
public class TestWeights { public static void main(String args[]) { // Test the constructors tkgWeight[] weights = { new tkgWeight(274.65) , new tkgWeight(274), new tkgWeight(274,2,3), new tkgWeight() }; //Display the weights for(int i = 0 ; i < weights.length ; ++i) System.out.println("Weight " + i + " is " + weights[i]); // Test the operations System.out.println("Addition: " + weights[0] + " plus " + weights[1] + " is " + weights[0].add(weights[1])); System.out.println("Subtraction: " + weights[0] + " minus " + weights[1] + " is " + weights[0].subtract(weights[1])); System.out.println("Multiplication: "+ weights[0] + " times 10 is " + weights[0].multiply(10)); System.out.println("Division: " + weights[0] + " divided by 10 is " + weights[0].divide(10)); // Test comparison methods if(weights[0].greaterThan(weights[1])) { System.out.println("Weight "+ weights[0] + " is greater than length " + weights[1]); } else if(weights[0].lessThan(weights[1])) { System.out.println("Weight "+ weights[0] + " is less than length " + weights[1]); } else { System.out.println("Weight "+ weights[0] + " is the same length as length " + weights[1]); } // Compare successive weights using compare() method String compareStr = null; for(int i = 0 ; i < weights.length - 1 ; ++i) { switch(weights[i].compare(weights[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("Weight " + weights[i] + compareStr + weights[i+1]); } } }
Output of Above Java Program
Weight 0 is 0t 274kg 650g
Weight 1 is 0t 0kg 274g
Weight 2 is 274t 2kg 3g
Weight 3 is 0t 0kg 0g
Addition: 0t 274kg 650g plus 0t 0kg 274g is 0t 274kg 924g
Subtraction: 0t 274kg 650g minus 0t 0kg 274g is 0t 274kg 376g
Multiplication: 0t 274kg 650g times 10 is 2t 746kg 500g
Division: 0t 274kg 650g divided by 10 is 0t 27kg 465g
Weight 0t 274kg 650g is greater than length 0t 0kg 274g
Weight 0t 274kg 650g is greater than length 0t 0kg 274g
Weight 0t 0kg 274g is less than length 274t 2kg 3g
Weight 274t 2kg 3g is greater than length 0t 0kg 0g