For the assignment, I made a program that takes in two volumes and compares them returning whether the first volume is equal to, larger than, or smaller than the second volume. The volumes can be in Liters (L), Gallons (G), or Fluid Ounces (oz).
We had to create 1 abstract superclass (called Volume), 1 interface (called Vol), 3 sub-abstract classes (liter, gallon, ounce)
The 3 subclasses each include methods that convert the local volume variable to the other volume scales.
What I don't get right now to do is how to formulate toCompare class.
toCompare class is an abstract method that must be implemented in the subclasses. It takes in a Volume object and compares its own (local) volume with the volume of the object passed into it. If the volumes are equal it returns a ’0′, or a ‘-1′ if the passed in object is larger, or a ’1′ if the passed in object is smaller in volume.
I know I need to use the methods to convert the volume of the object being passed onto according to its class, but how? by doing this.inGallong, etc?
The code is below.
public abstract class Volume { public double volume; public String unit; public double getVolume(){ return volume; } public String getUnit(){ return unit; } abstract int compareTo(Volume tempVolume); } public class Liter extends Volume implements Vol { public Liter(double tempLiter) { unit = "L"; volume = tempLiter; } public double inLiter() { return volume; } public double inGallon() { volume = volume * 0.264; return volume; } public double inOunce() { volume = volume * 33.814; return volume; } @Override int compareTo(Volume tempVolume) { // TODO Auto-generated method stub if (tempVolume.getUnit() == "L") { if (tempVolume.getVolume() == this.inLiter()){ return 0; } else if (tempVolume.getVolume() < this.inLiter()){ return 1; } else { return -1; } } else if (tempVolume.getUnit() == "G") { if (tempVolume.getVolume() == this.inGallon()){ return 0; } else if (tempVolume.getVolume() < this.inGallon()){ return 1; } else { return -1; } } else { if (tempVolume.getVolume() == this.inOunce()){ return 0; } else if (tempVolume.getVolume() < this.inOunce()){ return 1; } else { return -1; } } } public String toString() { return (volume + " " + unit); } } public class Gallon extends Volume implements Vol { public Gallon(double tempGallon){ unit = "G"; volume = tempGallon; } public double inLiter() { volume = volume * 3.785; return volume; } public double inGallon() { return volume; } public double inOunce() { volume = volume * 128.0; return volume; } @Override int compareTo(Volume tempVolume) { // TODO Auto-generated method stub if (tempVolume.getUnit() == "L") { if (tempVolume.getVolume() == this.inLiter()){ return 0; } else if (tempVolume.getVolume() < this.inLiter()){ return 1; } else { return -1; } } else if (tempVolume.getUnit() == "G") { if (tempVolume.getVolume() == this.inGallon()){ return 0; } else if (tempVolume.getVolume() < this.inGallon()){ return 1; } else { return -1; } } else { if (tempVolume.getVolume() == this.inOunce()){ return 0; } else if (tempVolume.getVolume() < this.inOunce()){ return 1; } else { return -1; } } } public String toString() { return (volume + " " + unit); } } public class Ounce extends Volume implements Vol { public Ounce(double tempOunce){ unit = "Oz"; volume = tempOunce; } public double inLiter() { volume = volume * 2.957 / 100; return volume; } public double inGallon() { volume = volume * 7.813 / 1000; return volume; } public double inOunce() { return volume; } @Override int compareTo(Volume tempVolume) { // TODO Auto-generated method stub if (tempVolume.getUnit() == "L") { if (tempVolume.getVolume() == (this).inLiter()){ return 0; } else if (tempVolume.getVolume() < (this).inLiter()){ return 1; } else { return -1; } } else if (tempVolume.getUnit() == "G") { if (tempVolume.getVolume() == (this).inGallon()){ return 0; } else if (tempVolume.getVolume() < (this).inGallon()){ return 1; } else { return -1; } } else { if (tempVolume.getVolume() == (this).inOunce()){ return 0; } else if (tempVolume.getVolume() < (this).inOunce()){ return 1; } else { return -1; } } } public String toString() { return (volume + " " + unit); } } public interface Vol { abstract double inLiter(); abstract double inGallon(); abstract double inOunce(); abstract String toString(); } import java.io.*; public class VolTestDrive { private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { System.out.println("\nWELCOME TO THE VOLUME COMPARER!\n"); System.out.print("Please enter a volume you want to compare: "); double Vol = Double.parseDouble(stdin.readLine()); System.out.print("\nPlease select the scale of the volume:\n1 - Liter\n2 - Gallon\n3 - Fluid Ounce\n"); int selection = Integer.parseInt(stdin.readLine()); if (selection == 1) { Liter V = new Liter(Vol); Compare(V); } else if (selection == 2) { Gallon V = new Gallon(Vol); Compare(V); } else { Ounce V = new Ounce(Vol); Compare(V); } } public static void Compare(Volume V) throws IOException { System.out.print("Please enter a second volume to compare to the first: "); double Vol2 = Double.parseDouble(stdin.readLine()); System.out.print("\nPlease select the scale of the second volume to compare:\n1 - Liter\n2 - Gallon\n3 - Fluid Ounce\n"); int selection2 = Integer.parseInt(stdin.readLine()); if (selection2 == 1) { Liter L = new Liter(Vol2); if(L.compareTo(V) == 0){ System.out.println(L.toString() + " is equal to " + V.toString()); } else if(L.compareTo(V) == 1){ System.out.println(L.toString() + " is larger than " + V.toString()); } else if(L.compareTo(V) == -1){ System.out.println(L.toString() + " is smaller than " + V.toString()); } } else if (selection2 == 2) { Gallon G = new Gallon(Vol2); if(G.compareTo(V) == 0){ System.out.println(G.toString() + " is equal to " + V.toString()); } else if(G.compareTo(V) == 1){ System.out.println(G.toString() + " is larger than " + V.toString()); } else if(G.compareTo(V) == -1){ System.out.println(G.toString() + " is smaller than " + V.toString()); } } else if (selection2 == 3) { Ounce F = new Ounce(Vol2); if(F.compareTo(V) == 0){ System.out.println(F.toString() + " is equal to " + V.toString()); } else if(F.compareTo(V) == 1){ System.out.println(F.toString() + " is larger than " + V.toString()); } else if(F.compareTo(V) == -1){ System.out.println(F.toString() + " is smaller " + V.toString()); } } else{ System.out.println("Invalid option. Good Bye."); } } }