Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: OOP help!

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default OOP help!

    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.");
    		}
    	}
     
     
    }
    Last edited by imaznumkay; July 11th, 2011 at 01:44 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: OOP help!

    how to formulate toCompare method
    For the method to be able to compare the volumes they must be in the same units.
    Do all the classes store their volumes in a common unit as well as their own units?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: OOP help!

    As far as I can see your code is doing what it should. So I don't understand what your problem is. However I have a couple of points:
    public double inGallon() {
        volume = volume * 0.264;
        return volume;
    }
    Your code is actually changing the value. So in the Litre class it is no longer holding litres but gallons. What you should do instead is just return the conversion.
    public double inGallon() {
        return volume * 0.264;
    }
    Also when you are comparing volumes of the same type there is no need to make unnecessary method calls.
    if (tempVolume.volume == this.volume) {
        return 0;
    }
    Improving the world one idiot at a time!

  4. The Following User Says Thank You to Junky For This Useful Post:

    imaznumkay (July 11th, 2011)

  5. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: OOP help!

    Returning the conversion seemed to fix the problem. I did not realize that those methods were actually changing the values in the objects.

    Thank you!