# Loop with several methods

• 11-09-2010, 09:11 PM
joshft91
Loop with several methods
So in my current assignment, we're modeling a student in a school ... I'll spare you guys the boring details, but here's what's up.

The method I'm working on is called cram() - this method contains 1 method to drink mountain dew ( drink("mountain dew")). After drinking one mountain dew, the study studies for 3 hours (study(3)). These repeat until the energy is at zero. At this point, when the energy is 0, the student will sleep for 6 hours (sleep(6))

So in summary, I have the drink(), study(), and sleep() methods - I know I'm going to have to use some sort of a loop to do this, however I'm somewhat unsure of how to go about doing this (the teachers in our class haven't talked about loops much at all).

Please note that I'm not looking for a way to cheat/take the lazy way out - I want to learn, but after doing some research myself, I'm unable to find anything.

Thanks,
joshft91
• 11-09-2010, 09:23 PM
I'm new, as well, but that sounds like a do while loop to me. do whatever while energy does not equal 0. What does energy equate to? What is it's starting value? What does it decrease by each time something happens?
• 11-09-2010, 09:25 PM
m00nchile
Your Student class doesn't need a loop, just write up the methods you described. Then, in your driver class (a class with the main method) you could make a Student object and call the cram() method in a loop. Unless I misunderstood the assignment and a call of the cram() method has to keep executing until the student goes to sleep.
• 11-09-2010, 11:58 PM
joshft91
Sorry for the confusion ... The energy, when the object is initially created is set to 0.

I have the drink() study() and sleep() methods all done ... they're all functioning fine. However, the cram method is going to be some sort of a loop (I'm thinking a while loop) that will drink one mountain dew, study for 3 hours, and then, when the energy is 0, the student will sleep for 6 hours.

I'm just not sure how to set up the while loop.

Thanks.
• 11-10-2010, 03:59 PM
joshft91
Sorry for the bump ... but I'll give you guys the bare code of what I'm working with.

Code:

``` package student;   public class CS234Student implements CollegeStudent {   private int energy;     public CS234Student() {     energy = 0;   }       //public void cram() {   //  while   //}       public void drink (String beverage) {     if (beverage.equals("coffee")) {       energy = energy + 3;     }     if (beverage.equals("mountain dew")) {       energy = energy + 2;     }     if (beverage.equals("juice")) {       energy = energy + 1;     }     if (beverage.equals("milk")) {       energy = energy - 1;     }     if (beverage.equals("beer")) {       energy = energy - 2;     }     if (energy > 10) {       energy = 10;     }     if (energy < 0) {       energy = 0;     }   }         public void eat() {     energy = energy + 2;     if (energy > 10) {       energy = 10;     }   }       public int energy() {     return energy;   }       public String mood() {     return ((energy >= 0) && energy < 4) ? "Happy" : ((energy < 8) ? "Energized" : ((energy < 10) ? "Ecstatic" : null));   }       public void sleep(int hours) {     energy = energy + hours;     if (energy > 10) {       energy = 10;     }   }       public void study(int hours) {     energy = energy - hours;     if (energy < 0) {       energy = 0;     }   }       public void writeProgram(int hours) {     energy = energy - (2*hours);     if (energy < 0) {       energy = 0;     }   } }```
• 11-10-2010, 04:09 PM
StormyWaters
First, off topic, to consolidate all those if (energy < 0) and if (energy > 10) statements, just create a method called addEnergy(int energyToAdd) and handle all that in there. Call that method when adding/subtracting the energy.

As for your cram method. A while or do while statement should be fine.
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

Code:

```    while(continue_condition) {           //your code here...     }```
Code:

```    do {           //your code here...     } while(continue_condition);```
• 11-10-2010, 04:44 PM
Tolls
And I'd consider making beverage into a Beverage class...but that's just me. It would only have an energy attribute, but I feel that's neater.

Actually...an enum.
:)

Not that that solves your problem.
I'm not too clear what you want to happen.
Can you write out the steps?
eg.
Code:

```Cram ->     drink something.     study until energy is zero.     sleep.```
If that is the requirement then the loop is simply around the study part, while energy > 0.
• 11-10-2010, 05:51 PM
joshft91
Thanks for the replies - bear with me, I'm just learning Java.
I'm pretty sure the rest of my code is set up correct ... I know there may be some different thing you guys would do different, however this is the way the teachers want it set up.

As you can see there's all those separate methods ... but the one cram() method basically contains the other methods inside it.

Here's how the cram() method should work:

The student will drink one mountain dew
Code:

` drink("mountain dew")`
The student will study for 3 hours
Code:

` study(3)`
When the energy is depleted (0), the student will then sleep for 6 hours.
Code:

` sleep(6)`

I've done this so far ...
Code:

```public void cram() {     while (energy > 0) {       drink("mountain dew");       study(3);     }       sleep(6);   }```
It looks like it compiles just fine, but I want to see if you guys see anything wrong with it.

Thanks again.
• 11-10-2010, 06:02 PM
Tolls
Ah, so they are drinking then studying, and keep doing that until energy == 0.
That does what you just described.
I'd stick some s.o.p (System.out.println()) in there, so you can "see" them drink and study and sleep. In fact stick the s.o.p in the drink/study and sleep methods rather than the cram method. It's always good to learn how to debug.
:)
• 11-10-2010, 06:11 PM
StormyWaters
Just one comment... reading the description in your original post it states
Quote:

The method I'm working on is called cram() - this method contains 1 method to drink mountain dew ( drink("mountain dew")). After drinking one mountain dew, the study studies for 3 hours (study(3)). These repeat until the energy is at zero. At this point, when the energy is 0, the student will sleep for 6 hours (sleep(6))
Does this mean they should always drink the mountain dew and study for at least 3 hours whenever the method is called? Or should it check if the student has energy to even drink.

Mainly, the question deals with whether this should be a do-while or a while loop. The difference would be seen if a Student with 0 energy attempts to cram.
• 11-10-2010, 06:17 PM
Tolls
Presumably if you had no energy then you'd sleep.
After sleeping you could cram?

ETA: But that's probbaly overthinking the problem.
:)
• 11-10-2010, 06:58 PM
StormyWaters
Yes but drinking the mountain dew does give you energy
• 11-11-2010, 09:47 AM
Tolls
It's open to interpretation, that's for sure.
• 11-11-2010, 10:08 AM
JosAH
Quote:

Originally Posted by StormyWaters
Yes but drinking the mountain dew does give you energy

Is "mountain dew" a synonym for "Grolsch"? If not the entire problem is moot ...

kind regards,

Jos ;-)