Results 1 to 14 of 14
Thread: Loop with several methods
- 11-09-2010, 08:11 PM #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
- 11-09-2010, 08:23 PM #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?
- 11-09-2010, 08:25 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-09-2010, 10:58 PM #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.
- 11-10-2010, 02:59 PM #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; } } }
- 11-10-2010, 03:09 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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) { //your code here... }Java Code:do { //your code here... } while(continue_condition);
- 11-10-2010, 03:44 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
If that is the requirement then the loop is simply around the study part, while energy > 0.Java Code:Cram -> drink something. study until energy is zero. sleep.
- 11-10-2010, 04:51 PM #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 dewThe student will study for 3 hoursJava Code:drink("mountain dew")When the energy is depleted (0), the student will then sleep for 6 hours.Java Code:study(3)
Java Code:sleep(6)
I've done this so far ...
It looks like it compiles just fine, but I want to see if you guys see anything wrong with it.Java Code:public void cram() { while (energy > 0) { drink("mountain dew"); study(3); } sleep(6); }
Thanks again.
- 11-10-2010, 05:02 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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, 05:11 PM #10
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Just one comment... reading the description in your original post it states
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.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))
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, 05:17 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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, 05:58 PM #12
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Yes but drinking the mountain dew does give you energy
- 11-11-2010, 08:47 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
It's open to interpretation, that's for sure.
- 11-11-2010, 09:08 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
Set ang get methods
By jcm2302 in forum New To JavaReplies: 1Last Post: 03-06-2010, 09:09 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks