Results 1 to 15 of 15
  1. #1
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Help with bottle Class

    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 bottle5 = new Bottle();
    System.out.println("please enter a number for bottle1:");
    bottle1.read();
    System.out.println("Bottle1 is this value " + bottle1 + ".");
    System.out.println("Please enter a number for bottle2:");
    bottle2.read();
    bottle3 = bottle3.add(bottle1);
    bottle3 = bottle3.add(bottle2);
    bottle3 = bottle3.divide(2);
    System.out.println("The 2 bottle average is: " + bottle3 + ".");
    System.out.print("Subtracting bottle1 from bottle2 is: " );
    bottle3 = bottle2.subtract(bottle1);
    System.out.println( bottle3);
    bottle3 = bottle2.divide(bottle1);
    System.out.println("Dividing bottle2 with bottle1 is: " + bottle3 + ".");
    if (bottle1.equals(bottle2))
    {
    System.out.println("Bottle1 and bottle2 are equal.");
    }
    else
    {
    System.out.println("Bottle1 and bottle2 are not equal.");
    }
    System.out.print("Bottle4 is now given the value of 10 with the set()");
    System.out.println(" method.");
    bottle4.set(10);
    System.out.println("The value of bottle4 is " + bottle4 + ".");
    System.out.println("Bottle4 is now multiplied with bottle1. The value ");
    System.out.println("is placed in bottle5.");
    bottle5 = bottle1.multiply(bottle4);
    System.out.println("The value of bottle5 is " + bottle5 + ".");
    System.out.println("Enter an integer to add to the value bottle1 has.");
    System.out.println("The sum will be put in bottle3.");
    x = scan.nextInt();
    bottle3 = bottle1.add(x);
    System.out.println("Adding your number " + x +
    " to bottle1 gives a new Bottle with " + bottle3 + " in it.");
    System.out.print("Adding the number " + bottle2 + " which is the number" +
    " in bottle2 to the\nnumber in ");
    bottle2 = bottle1.add(bottle2);
    System.out.println("bottle1 which is " + bottle1 +" gives " + bottle2 + ".");
    }
    }



    /* alright so the part I don't understand is the read() method, how am i suppose to make bottle1 into an integer in another bottle class*/ Please, any help would be appreciated

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Help with bottle Class

    When posting code, please use the code tags to preserve formatting.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with bottle Class

    how do i use code tags ? sorry I'm new here.

  4. #4
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with bottle Class

    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);
    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 average of the 4 bottles 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("Eenter an integer to add to bottle 1");
    x = scan.nextInt();
    bottle1.add(x);
    System.out.println("adding your number " + x +
    " to bottle1 gives " + bottle1);
    bottle2 = bottle1.add(bottle3);
    System.out.println("adding the number  + bottle3 +  from bottle3 to bottle1 gives " + bottle2);
    }
    }

    OK never mind i got it
    Anyways can anybody help me out on the read() method please. I'm really struggling there.
    I don't know to to get bottle1.read() to make bottle1 into an integer

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Help with bottle Class

    I'm not trying to be obnoxious, but the code you posted still doesn't have any indentation, probably because you copied it directly from your previous post.

    That being said, I'm really not sure what you're asking. What is the Bottle class? Does it even have a read() method? Why do you have a Scanner if you never use it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Help with bottle Class

    read will need to take in a scanner.

    Java Code:
    int numBottle; //in class bottle.  This is whatever the int in that class is for the number on bottle.
    public void read(Scanner s){
    if(s.hasNextInt()){
    numBottle= s.nextInt();
    } else{
    System.out.println("Entered invalid number.");
    }
    }
    Another way to do this is do all the reading in the main class and then just bottle1.setNumber(x);
    If the only thing in your bottle class is an integer this is kind of pointless. Why not just say int bottle1; ect in main class
    Last edited by jbarke12; 01-23-2014 at 07:44 PM.

  7. #7
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with bottle Class

    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 bottle5 = new Bottle(); 
            System.out.println("please enter a number for bottle1:"); 
            bottle1.read(); 
            System.out.println("Bottle1 is this value " + bottle1 + "."); 
            System.out.println("Please enter a number for bottle2:"); 
            bottle2.read(); 
            bottle3 = bottle3.add(bottle1); 
            bottle3 = bottle3.add(bottle2); 
            bottle3 = bottle3.divide(2); 
            System.out.println("The 2 bottle average is: " + bottle3 + "."); 
            System.out.print("Subtracting bottle1 from bottle2 is: " ); 
            bottle3 = bottle2.subtract(bottle1); 
            System.out.println( bottle3); 
            bottle3 = bottle2.divide(bottle1); 
            System.out.println("Dividing bottle2 with bottle1 is: " + bottle3 + "."); 
            
            if (bottle1.equals(bottle2)) 
            { 
                System.out.println("Bottle1 and bottle2 are equal."); 
            } 
            else 
            { 
                System.out.println("Bottle1 and bottle2 are not equal."); 
            } 
            System.out.print("Bottle4 is now given the value of 10 with the set()"); 
            System.out.println(" method."); 
            bottle4.set(10); 
            System.out.println("The value of bottle4 is " + bottle4 + "."); 
            System.out.println("Bottle4 is now multiplied with bottle1. The value "); 
            System.out.println("is placed in bottle5."); 
            bottle5 = bottle1.multiply(bottle4); 
            System.out.println("The value of bottle5 is " + bottle5 + "."); 
            System.out.println("Enter an integer to add to the value bottle1 has."); 
            System.out.println("The sum will be put in bottle3."); 
            
            x = scan.nextInt();
            
            bottle3 = bottle1.add(x); 
            System.out.println("Adding your number " + x + 
                    " to bottle1 gives a new Bottle with " + bottle3 + " in it."); 
            System.out.print("Adding the number " + bottle2 + " which is the number" + 
                    " in bottle2 to the\nnumber in "); 
            bottle2 = bottle1.add(bottle2); 
            System.out.println("bottle1 which is " + bottle1 +" gives " + bottle2 + "."); 
        } 
    }

    what I'm asking is how so I create a read() method in order for bottle.read()
    Java Code:
    bottle1.read(); 
            System.out.println("Bottle1 is this value " + bottle1 + ".");
    to store the integer I type into "bottle1" ?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Help with bottle Class

    Are you sure you even need a read() method in your Bottle class? Why not just a setValue(int x) method that takes in a value you read using your Scanner?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Help with bottle Class

    Look at my previous postthat is how you would do it.
    System.out.println("Bottle1 is this value " + bottle1.toString() + ".");

    And if you want to print the number you need to define a toString() method in your bottle class.
    Last edited by jbarke12; 01-23-2014 at 07:57 PM.

  10. #10
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Help with bottle Class

    Also you use the .equals method in your main class. You have to override the equals method to check values. Otherwise it won't ever be equal unless memory location match up

  11. #11
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with bottle Class

    I'm not allowed to change or modify the BottleDemo class
    How would the ToString() method work with the read() method

  12. #12
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Help with bottle Class

    Java Code:
    System.out.println("Bottle1 is this value " + bottle1 + ".");
    
    System.out.println("Bottle1 is this value " + bottle1.toString() + ".");

    It doesn't do what you think it will. You need to define a toString() method in the Bottle class then you can use the 2nd system.out.println

  13. #13
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with bottle Class

    Alright thanks for the help guys , i got it
    Just one last thing, how would i guarantee that bottles never exceed a maximum number that i can input ??
    for example the integer stored in bottle is called marbles...I set the limit of marble as a MAX. how would I set that limit in the bottle class

  14. #14
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Help with bottle Class

    Lets say MAX= 25....

    Then where you set the numberOfMarbles use an if statement.

    Java Code:
    int x= scan.nextInt();
    if(x>25){
    System.out.println("The value is too high.");
    
    
    } else{
    numberOfMarbles=x;
    }

  15. #15
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: Help with bottle Class

    Got it !
    I guess I was overthinking it.
    Thanks so much for your help !

Similar Threads

  1. Replies: 3
    Last Post: 12-08-2013, 09:28 PM
  2. Help with Bottle Class directions
    By g33k3r in forum New To Java
    Replies: 2
    Last Post: 01-26-2013, 12:38 AM
  3. Replies: 1
    Last Post: 12-13-2012, 03:06 AM
  4. Replies: 0
    Last Post: 03-27-2011, 05:49 AM

Tags for this Thread

Posting Permissions

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