0

I don't understand why it doesn't give a random amount. Please explain.

Thanks :)

my code(learned from tutorial):

public class day5 {
    int totalwater = 0;

    public day5(){
        //default constructor
    }
    public day5(int wateramount){
        totalwater = wateramount;
    }
    // don't need static in Object. It will be used in other classes.
    public void addwater(int amount){
        totalwater = totalwater + amount;
    }
    public void drinkwater(int amount){
        totalwater = totalwater - amount;
    }
    public int getwater(){
        return totalwater;
        //because we are going to return an integer, public "int" and "return"
    }
}



public class day5obtest {
    public static void main(String[] args){
        day5 waterbottle = new day5(0);
        int rand = (int)(Math.random()*100);
        waterbottle.addwater(rand); 
        waterbottle.drinkwater(rand);
        System.out.println("The amount of water in your bottle now is: " + waterbottle.getwater());
    }
}

Output:

The amount of water in your bottle now is: 0

1
  • 1
    Are you expecting the amount of water to have changed after calling drinkwater() ? Commented May 9, 2015 at 22:57

3 Answers 3

5

You add and drink the same amount. You have to generate a new random amount after adding, like this:

    int rand = (int)(Math.random()*100);
    waterbottle.addwater(rand); 
    rand = (int)(Math.random()*100);
    waterbottle.drinkwater(rand);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh! I understand now! Thanks very much. I feel dumb xD
1

You only generate one random number.

You add a randomly generated number to the amount of water, but then you remove that same number from the amount of water, so there's always a net change of 0.

To fix this you could create a second random number rand2 and remove that from the amount of water in drinkwater()

Comments

1
  1. It is a good practice to begin you class name with uppercase like Day5.
  2. Add getters and setters in your Day5 class therefore you can access their fields outside.
  3. If you want to make your program more realistic, add a test in your drinkwater method

as follow

public void drinkwater(int amount){
    if(amount<=totalwater)
        totalwater = totalwater - amount;
    else 
        System.out.println("amount bigger than total water");
}
  1. Make new random amount after adding water.

Finally

 int rand = (int)(Math.random()*100);
 waterbottle.addwater(rand); 
 rand = (int)(Math.random()*100);
 waterbottle.drinkwater(rand);

2 Comments

You have a point there, this way negative result can be avoided.
it is logic that we cannot drink more than we have ;) @TamásG.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.