Results 1 to 14 of 14
  1. #1
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default 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. #2
    Neiladin is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

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

    Default

    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.

  4. #4
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    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. #5
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    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. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    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. #8
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    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. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    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. #10
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Presumably if you had no energy then you'd sleep.
    After sleeping you could cram?

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

  12. #12
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Yes but drinking the mountain dew does give you energy

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    It's open to interpretation, that's for sure.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by StormyWaters View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  3. Set ang get methods
    By jcm2302 in forum New To Java
    Replies: 1
    Last Post: 03-06-2010, 09:09 PM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. Replies: 4
    Last Post: 04-03-2009, 06:36 AM

Posting Permissions

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