[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
Need an Explanation  XML
Forum Index » Java Programming
Author Message
Stirex

Newbie

Joined: 03/29/2006 16:50:31
Messages: 2
Offline

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

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;
}
stdunbar

Newbie
[Avatar]

Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline

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?

Thanks for using the forums at hotjoe.com
[WWW] [Yahoo!] [ICQ]
destin

Newbie
[Avatar]
Joined: 01/07/2006 19:13:04
Messages: 224
Offline

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.
[Email]
Stirex

Newbie

Joined: 03/29/2006 16:50:31
Messages: 2
Offline

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Safetest extends Applet
implements ActionListener {

private int guess = 0;
private int combination = 321;
private Button one, two, three, tryAgain;

public void init() {
one = new Button("1");
add (one);
one.addActionListener(this);
two = new Button("2");
add (two);
two.addActionListener(this);
three = new Button ("3");
add (three);
three.addActionListener(this);
tryAgain = new Button("Try Again");
add(tryAgain);
tryAgain.addActionListener(this);
}

public void paint(Graphics g) {
if (guess==combination)
g.drawString ("Unlocked", 50,50);
else
g.drawString ("Locked", 50,50);
}

public void actionPerformed(ActionEvent event) {

if (event.getSource() == one)
guess = guess*10 + 1;
if (event.getSource() == two)
guess = guess*10 + 2;
if (event.getSource() == three)
guess = guess*10 + 3;
if (event.getSource() == tryAgain)
guess = 0;

repaint();
}
}


ok so thats the whole code (tad bit different gets rid of X) basically it gives you 3 buttons, and you try to guess the combination of the safe in 3 guesses.

Now the part that I dont totally understand, how does it know you put in the right combination 321, and how does it keep track of the guesses. This is an example given in a lesson plan, but the explanation is very vague. I understand the action listeners, the button initialzations, just not the math.
stdunbar

Newbie
[Avatar]

Joined: 06/22/2005 14:51:37
Messages: 849
Location: Superior, CO, USA
Offline

The math is kinda funky but if you walk through it it makes sense.

guess starts out at zero or is reset to zero when the tryAgain Button is pushed. If you press the three button guess is set to the value 3. Then, if you press the two button guess is set to 32. Lastly, if you press the one button guess is set to 321. Every time any of the buttons are pressed the repaint() method is called which calls paint().

Make sense?

Thanks for using the forums at hotjoe.com
[WWW] [Yahoo!] [ICQ]
 
Forum Index » Java Programming
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle