# Thread: Loop with several methods

1. Member
Join Date
Oct 2010
Posts
8
Rep Power
0

## 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

2. Member
Join Date
Nov 2010
Posts
3
Rep Power
0
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?

3. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
11
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.

4. Member
Join Date
Oct 2010
Posts
8
Rep Power
0
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.

5. Member
Join Date
Oct 2010
Posts
8
Rep Power
0
Sorry for the bump ... but I'll give you guys the bare code of what I'm working with.

Java 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;
}
}
}```

6. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
12
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)

Java Code:
```     while(continue_condition) {
}```
Java Code:
```     do {
} while(continue_condition);```

7. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
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.
Java 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.

8. Member
Join Date
Oct 2010
Posts
8
Rep Power
0
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
Java Code:
` drink("mountain dew")`
The student will study for 3 hours
Java Code:
` study(3)`
When the energy is depleted (0), the student will then sleep for 6 hours.
Java Code:
` sleep(6)`

I've done this so far ...
Java 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.

9. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
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.
:)

10. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
12
Just one comment... reading the description in your original post it states
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. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
Presumably if you had no energy then you'd sleep.
After sleeping you could cram?

ETA: But that's probbaly overthinking the problem.
:)

12. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
12
Yes but drinking the mountain dew does give you energy

13. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
It's open to interpretation, that's for sure.

14. 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 ;-)

#### Posting Permissions

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