Results 1 to 3 of 3
- 01-25-2013, 07:59 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
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:
The following is the code that I have written so far: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); } }
Thanks again for the help.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; } } }
- 01-25-2013, 09:28 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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
-
Re: Help with Bottle Class directions
Similar Threads
-
Class example not compiling Runner class and Swimmer class
By Joshsmith in forum New To JavaReplies: 1Last Post: 12-13-2012, 03:06 AM -
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
Studying Directions
By Deepwoods in forum New To JavaReplies: 3Last Post: 08-19-2010, 08:53 PM -
Newbie asking for directions
By geotri314 in forum New To JavaReplies: 7Last Post: 04-28-2010, 12:06 PM -
Brand New to Java needs directions
By NatasjaA in forum New To JavaReplies: 13Last Post: 04-07-2008, 04:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks