Peter Chirco

Greenhorn
+ Follow
since Nov 20, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Peter Chirco

void crazyLoop() {int c = 0;JACK: while (c < 8) {JILL: System.out.println(c);if (c > 3) break JACK; else c++;}}
Sonir
Here is what I think is happening:
Your first loop is Jack, which runs from c=0 to c=7. Your second loop is Jill, which stops Jack when c > 3 (the first time that happens is when c = 4). Since the print statement happens before the break statement, all values of c will be printed out, including the first value that will make the test in Jill false. Then we break out of Jack, and move on.
Pete
OK, I did some coding, and I am getting the results you mentioned, but I am still not sure why. I created a frame, and added 2 components to it: a Button, and a myButton, which I defined. I first tried to call button1.enableEvents(), and I got an error telling me that method was not visible. That is what you told me to expect. I then used the usual process, added an ActionListener, and got it to work fine. For myButton, I called enableEvents() in the constructor, and then overrode the processActionEvent() method. I added that to my frame, and it works also. So I have a frame with two buttons, both of which respond to action events, one through an event listener and one through explicit event enabling.
I still have the question, though, why I could not call button1.enableEvents(). since the method is defined in Component, as a protected final method, I should be able to call that method from any subclass of Component, or any class in the same package as Component.
import java.awt.*;
import java.awt.event.*;
public class ActionTest extends Frame implements ActionListener
{
class windowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
Button button1;
MyButton button2;
public ActionTest()
{
addWindowListener(new windowHandler());
button1 = new Button("Button");
button2 = new MyButton("MyButton");
setLayout(new GridLayout(1,2));
add(button1);
add(button2);
// button1.enableEvents(AWTEvent.ACTION_EVENT_MASK);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Button tempbutton = (Button)ae.getSource();
if(tempbutton == button1)
System.out.println("Button 1");
}
public static void main(String[] args)
{
ActionTest at = new ActionTest();
at.setBounds(10, 10, 50, 200);
at.show();
}
}
22 years ago
I have a problem with one of the applied reasoning questions. The question reads like this:
Select each of the code fragments that would allow a newly created java.awt.Button to generate ActionEvents:
a. button.enableActionEvents();
b. button.addActionListener(anActionListener);
c. button.enableEvents(true);
d. button.enableEvents(AWTEvent.ACTION_EVENT_MASK);
I answered both b and d. He suggetst b is the only correct answer, and the reasoning for d not being correct is that , since enableEvents is protected, it can only be called by a subclass. Since enableEvents is defined in Component, and since Button derives from Component, shouldn't it be able to call it? Also, looking at the Exam Cram Sun Certified Java Programmer book, they have an example that is almost identical to this.
22 years ago
I have a question about shifting
Question 70: To place a 1 in the high-bit of an int named ref that�s set to 0x00000001, you can write:
Select the one right answer.
A. ref >> 31;
B. ref >>= 31;
C. ref << 31;
D. ref <<= 31;
E. Shifts the bits in an integer to the left by the number of F. bits specified and fills the right-most bit with 1.
F. Shifts the bits in an integer to the left by the number of bits specified and fills the right-most bit with 0.
The answer as given in the test is d, but I thought the answer was c. I looked through the Robert Heller and Ernest book, and I cannot find a <<= shift operator anywhere.
Thanks
22 years ago