Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Stuck with an Event Generator

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Stuck with an Event Generator

    Hey guys!

    Was recommended this forum by a friend of mine and the expertise here seems to be beyond anything I've seen before, so I'm thrilled to learn a lot from it. I'm currently studying a Object Oriented Java course in Sweden through BlueJ (the course book is David Barnes' Objects First With Java) and I'm currently stuck at a problem concerning randomizers, HashMaps and those kind of things.

    The object is to enter a persons name and then generate seven different events and 'mood points' on each day of the week (these mood points will be different on every event). There are four different classes for this:

    Person: [Java] class: Person - Pastebin.com
    EventGenerator: [Java] class: EventGenerator - Pastebin.com
    Event: [Java] class: Event - Pastebin.com
    Dice: [Java] class: Dice - Pastebin.com

    The problem I'm having is how to write the code in Person and generally the methods listWeek(), affectMood() and generateDayEvent(). I think I'm heading the right direction, but I can't figure out how to generate a new event for each day —*the main problem is really how to fetch the data from the generator.

    Notice: I'm asking you to give me the code needed, because this is a problem I have to solve in school. But I'm asking you for some guidance and suggestions.

    If you prefer me to paste the code directly into this post, I'll do that. Not sure about the manners in here. (:

    Best regards


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stuck with an Event Generator

    I can't figure out how to generate a new event for each day —*the main problem is really how to fetch the data from the generator.
    How about using the idea of a listener? The code that wants the events would register itself as a listener with the event generator. The event generator would call the listener when it generated a new event.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Stuck with an Event Generator

    Here is tutorial on how to write a custom listener - also what is known as the Observer pattern:
    http://www.javaprogrammingforums.com...r-pattern.html

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stuck with an Event Generator

    Thank you for the replies! Helped out a lot and the only thing I've got left is some NullPointerException for my ArrayList (we haven't learned List yet, so don't bother telling me to use that .

    [Java] class: Person (updated) - Pastebin.com

    So I'm getting the NullPointerException at line 54 and I don't get why and how to solve it. Haven't I filled the ArrayList in the constructor?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stuck with an Event Generator

    What variable on line 54 is null? If you can't tell by looking at the code, add a println just before line 54 and print out the values of all the variables used on line 54 to see which variable is null.

    If you want anyone to look at your code, please post it in the thread.

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stuck with an Event Generator

    Quote Originally Posted by Norm View Post
    What variable on line 54 is null? If you can't tell by looking at the code, add a println just before line 54 and print out the values of all the variables used on line 54 to see which variable is null.

    If you want anyone to look at your code, please post it in the thread.
    But I posted a link to it. It's right where it said "[Java] class: Person (updated) - Pastebin.com". If that's not a welcomed way of displaying code, I can include it instead.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stuck with an Event Generator

    Please post your code in the thread. Be sure to wrap it in code tags.
    See: http://www.javaprogrammingforums.com...do=bbcode#code
    Did you look at the code on line 54? And print out the values of the variables used there?

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stuck with an Event Generator

    Yes, I did, and it just printed null. But that shouldn't be correct, in my mind, because I used the add() method in the constructor. What am I missing out on here?

    import java.util.HashMap;
    import java.util.ArrayList;
    import java.util.Iterator;
    /**
     * Requires a name and then generates a week with random events for each day of the week.
     * Every event affects the mood in a specific way.
     * The mood starts at the value of 200.
     * 
     * @author Erik Blomqvist
     * @version 2011-11-07
     */
    public class Person
    {
        private String mName;
        private int mMood;
        private HashMap<String, Event> mWeekEvent;
        private EventGenerator mGenEvent;
        private ArrayList<String> mWeekday;
     
     
        /**
         * Constructor for objects of class. Automatically fills the list with random events.
         * Requires the name of a person.
         * 
         * @param name Name of the person
         */
        public Person(String name)
        {
            mName = name;
            mMood = 200;
            mWeekEvent = new HashMap<String, Event>();
            mGenEvent = new EventGenerator();
            ArrayList<String> mWeekday = new ArrayList<String>();
     
            mWeekday.add(new String("Måndag"));
            mWeekday.add(new String("Tisdag"));
            mWeekday.add(new String("Onsdag"));
            mWeekday.add(new String("Torsdag"));
            mWeekday.add(new String("Fredag"));
            mWeekday.add(new String("Lördag"));
            mWeekday.add(new String("Söndag"));
        }
     
        /**
         * The only public method which prints out the randomized week events and how the person's
         * mood is affected.
         */
        public void listWeek()
        {
            System.out.println("Hej, " + mName);
            System.out.println(" Du startar med gott humör på nivå: " + mMood);
            System.out.println(" Din vecka slumpar sig såhär...");
     
            for(String thisDay : mWeekday) {
                // Vänta en sekund...
                wait(1000);
                System.out.println(thisDay);
                generateDayEvent();
                Event thisEvent = (Event)mWeekEvent.get(thisDay);
                System.out.println(thisEvent);
                affectMood(thisDay);
                System.out.println(" Ditt humör är nu på: " + mMood);
            }
        }
     
        /**
         * The method to affect the total mood value based on the day of the week parameter.
         * 
         * @param day Day of the week
         */
        private void affectMood(String day)
        {
            Event thisEvent = (Event)mWeekEvent.get(day);
            int newMood = thisEvent.getAffectMood();
            mMood += newMood;
        }
     
        /**
         * Generates seven different events and puts them in the mWeekEvent HashMap.
         */
        private void generateDayEvent()
        {
            for(String thisDay : mWeekday) {
                mWeekEvent.put(thisDay, mGenEvent.generateEvent());
            }
        }
     
        /**
         * Wait for a specified number of milliseconds before finishing.
         * This provides an easy way to specify a small delay which can be
         * used when producing animations.
         * @param  milliseconds  the number 
         */
        private void wait(int milliseconds)
        {
            try
            {
                Thread.sleep(milliseconds);
            } 
            catch (Exception e)
            {
                // ignoring exception at the moment
            }
        }
    }

    edit: The reason I posted it on Pastebin is due to the fact that this forum doesn't support Java color highlighting and line numbers.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Stuck with an Event Generator

    it just printed null
    Then why is the variable null? You need to look back through your code and see why that variable is null.
    But that shouldn't be correct, in my mind, because I used the add() method in the constructor.
    Obviously you and the computer disagree on what you code is doing. I'll bet on the computer as being correct. So you need to figure out what is happening.

    Does your IDE tell you when you create a local variable that has the same name as a class variable? You should turn on that warning so the IDE will tell you if you are "hiding" a class variable with a local variable.
    Hint: That is your problem.
    Last edited by Norm; November 12th, 2011 at 11:45 AM.

  10. The Following User Says Thank You to Norm For This Useful Post:

    lither (November 12th, 2011)

  11. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stuck with an Event Generator

    Quote Originally Posted by Norm View Post
    Then why is the variable null? You need to look back through your code and see why that variable is null.
    Obviously you and the computer disagree on what you code is doing. I'll bet on the computer as being correct. So you need to figure out what is happening.

    Does your IDE tell you when you create a local variable that has the same name as a class variable? You should turn on that warning so the IDE will tell you if you are "hiding" a class variable with a local variable.
    Hint: That is your problem.
    Ah, got it. Should have been mWeekday = new ArrayList<String>(); and not ArrayList<String> mWeekday = new ArrayList<String>;

Similar Threads

  1. help with GUI password generator
    By semicolon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 30th, 2011, 12:22 PM
  2. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  3. Replies: 0
    Last Post: January 25th, 2011, 01:24 AM
  4. FLash generator app
    By axell in forum Java Theory & Questions
    Replies: 0
    Last Post: July 28th, 2010, 05:38 AM
  5. Java password generator program to generate 8 random integers
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 16th, 2009, 07:49 AM