Results 1 to 3 of 3
  1. #1
    g33k3r is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Help with Bottle Class directions

    Hey guys, I appreciate the help in advance. I have an assignment that I am currently working on but don't seem to understand how I would get this to work. The following is the assignment:

    Project 1: Write a Bootle class. The class has these 13 methods: read(),set(int),set(Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), add(int), subtract(int), multiply(int), equals(Bottle), and toString() methods. All add, subtract,multiply and divide methods return a Bottle. Your Bottle class must guarantee bottles always have a positive value. Use System.exit(0) to end the program if a negative bottle value is generated.

    What I do not understand is this section: All add, subtract,multiply and divide methods return a Bottle.

    How can I add values if I am returning a value?

    I have to make it work with the provided BottleDemo.java which I made a few changes:

    Java Code:
    import java.util.Scanner;
    
    // test driver for the Bottle class
    public class BottleDemo {
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		int x;
    		Bottle bottle1 = new Bottle();
    		Bottle bottle2 = new Bottle();
    		Bottle bottle3 = new Bottle();
    		Bottle bottle4 = new Bottle();
    		Bottle bottleAve = new Bottle();
    		System.out.println("please enter a number for bottle1:");
    		bottle1.read();
    		System.out.println("bottle1 is this value " + bottle1.value);
    		System.out.println("please enter a number for bottle2:");
    		bottle2.read();
    		System.out.println("please enter a number for bottle3:");
    		bottle3.read();
    		System.out.println("please enter a number for bottle4:");
    		bottle4.read();
    		bottleAve = bottleAve.add(bottle1);
    		bottleAve = bottleAve.add(bottle2);
    		bottleAve = bottleAve.add(bottle3);
    		bottleAve = bottleAve.add(bottle4);
    		bottleAve = bottleAve.divide(4);
    		System.out.println("The 4 bottle average is: " + bottleAve);
    		if (bottle1.equals(bottle3)) {
    			System.out.println("Bottle1 and bottle3 are equal");
    		} else {
    			System.out.println("Bottle1 and bottle3 are not equal");
    		}
    		System.out.println("Enter an integer to add to bottle 1");
    		x = scan.nextInt();
    		bottle2 = bottle1.add(x);
    		System.out.println("Adding your number " + x + " to  bottle1 gives "
    				+ bottle2);
    		bottle2 = bottle1.add(bottle3);
    		System.out.print("Adding the number " + bottle3 + " to ");
    		System.out.println("Bottle1  gives " + bottle2);
    	}
    }
    The following is the code that I have written so far:

    Java Code:
    import java.util.Scanner;
    
    public class Bottle {
    
    	/**
    	 * 
    	 * 
    	 * @param args
    	 */
    
    	private Scanner scan = new Scanner(System.in);
    	private int value;
    
    	public Bottle() {
    
    		value = 0;
    
    	}
    	
    	// methods
    	/*
    	 * 
    	 * multiply(int) equals(Bottle) toString()
    	 */
    
    	public void read() {
    		value = scan.nextInt();
    	}
    
    	public void set(Bottle bottle1) {
    		value = bottle1.value;
    
    	}
    
    	public void set(int bottle1) {
    		value = bottle1;
    	}
    
    	public Bottle add(Bottle bottle1) {
    		value = value + bottle1.value;
    		return this;
    
    	}
    
    	public Bottle subtract(Bottle bottle1) {
    
    		return bottle1;
    
    	}
    
    	public Bottle multiply(Bottle bottle1) {
    
    		return this;
    
    	}
    
    	public Bottle divide(Bottle bottle1) {
    		
    		return this;
    
    	}
    
    	public Bottle add(int bottle1) {
    
    		return this;
    
    	}
    
    	public Bottle subtract(int bottle1) {
    
    		return this;
    
    	}
    
    	public Bottle multiply(int bottle1) {
    
    		return this;
    
    	}
    
    	public Bottle divide(int bottle1) {
    		value = value / bottle1;
    		return this;
    
    	}
    
    	public String toString() {
    		return name;
    	}
    
    	public boolean equals(Bottle bottle1) {
    		if (this == bottle1) {
    			return true;
    		}
    
    		else {
    			return false;
    		}
    
    	}
    
    }
    Thanks again for the help.

  2. #2
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: Help with Bottle Class directions

    Java Code:
    public Bottle add(Bottle bottle1) {         
    value = value + bottle1.value;
    return this;       }


    this.value += bottle1.value;
    return this;

    wouldn't that work?

    I'm a newbie too but I'm giving it a try. you're not passing in a value, you're passing in an object and the using that objects 'value' to add to the current object you working on 'this'

    I might get slammed by moderator but this is my attempt...sorry moderator

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Bottle Class directions

    Quote Originally Posted by Danieldcc View Post
    Java Code:
    public Bottle add(Bottle bottle1) {         
    value = value + bottle1.value;
    return this;       }


    this.value += bottle1.value;
    return this;

    wouldn't that work?

    I'm a newbie too but I'm giving it a try. you're not passing in a value, you're passing in an object and the using that objects 'value' to add to the current object you working on 'this'

    I might get slammed by moderator but this is my attempt...sorry moderator
    I'm not going to slam you, but I am going to ask what advantage your code would have over the original poster's code.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 03:06 AM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. Studying Directions
    By Deepwoods in forum New To Java
    Replies: 3
    Last Post: 08-19-2010, 08:53 PM
  4. Newbie asking for directions
    By geotri314 in forum New To Java
    Replies: 7
    Last Post: 04-28-2010, 12:06 PM
  5. Brand New to Java needs directions
    By NatasjaA in forum New To Java
    Replies: 13
    Last Post: 04-07-2008, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •