Results 1 to 3 of 3
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Posts
    111
    Rep Power
    0

    Default I need didactic explanation

    Need a detailed explanation on how this works, I know it does, but not sure how to explain it to some one else.

    Java Code:
    public void actionPerformed(ActionEvent event) {
    
    int value = 0;
    
    if (event.getSource() == tryAgain)
    guess=0;
    else if (event.getSource() == one)
    value = 1;
    else if (event.getSource() == two)
    value = 2;
    else if (event.getSource() == three)
    value = 3;
    if(value > 0) {
    guess *= 10;
    guess += value;
    }
    Thanks.

    Eric

  2. #2
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    Somewhere up in the code there are a few objects (hard to tell what they are from this code) that are named tryAgain, one, two, and three. Something is triggering an event on these that is calling actionPerformed.

    I would "guess" that guess is defined as an int somewhere else. It looks like if the event came from tryAgain then you reset the guess. Otherwise you do the math as shown.

    Does that make sense?
    Greetings.

    Albert:rolleyes:

  3. #3
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    Default

    Looking at that code, I would assume that the four objects, tryAgain, one, two, and three, all share the same action listener, and this method is the actionPerformed(ActionEvent) method within that class. When an event is triggered, this method is called.

    Then, you check four things. First, you check if the object that triggered the event (event.getSource()) is the same object as tryAgain. You perform the same check three more times with different objects.

    Note that == compares memory. The code works because when objects are passed into methods the value is not passed in, but a reference of the memory location.

    Greetings.

    Marcus:cool:

Similar Threads

  1. Class explanation
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 06:50 PM
  2. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM
  3. Explanation bout threading and concurrency?
    By cruxblack in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 10:33 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
  •