all events occur in real time
Originally posted by Ernest Friedman-Hill:
The GUI event-handling thread will keep a program from exiting when main() returns. Using any GUI components will start it, and thereafter to make the program exit, you must call System.exit(0).
One thing I wanted to was create a method initPlayers(int numberOfPlayers) in my GuessGame class, so I would just call this method from my startgame() method. However, I'd have to make my array of players available to the entire class if I understand correctly, so not do a "Player p[] = new Player[numberOfPlayers];" in a method, right?
he original poster's program is a sort of hybrid thing which just throws up a few dialogs; when main() returns, the program won't exit by itself because the GUI thread (which is a non-daemon thread) is still running. He could just put System.exit(0) at the end of main().
all events occur in real time
Also, I'm *very* interested in any tips concerning my code: style, breaking down in enough methodes,
Originally posted by Burkhard Hassel:
Ernest Friedman-Hill posted October 30, 2006 09:54 AM
But if I have only this:
it seems, that the program exits normally. It seems to exit only a second earlier if I use the system exit at the end of the main method.
Is this JVM dependent?
Yours,
Bu.
Your code doesn't exit for me using jdk 1.4.2_06.
all events occur in real time
Except by wrapping the method it leaves open the possibility that a different implementation may be used. Perhaps in the future you might decide to use an implementation of a Mersenne Twister instead of java.util.Random to generate random numbers, or for testing you might want to create a mock implementation which overrides the createRandomNumber() method to return a non-random predetermined number. You then only have to change a single method implementation and leave the rest of your code untouched.Originally posted by sven studde:
Well, you can go too far with that as well. For instance, I think this is a case of going too far:
To me that almost seems equivalent to using a method like this:
instead of just using:
System.out.println("some string");
directly in your code.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Originally posted by David O'Meara:
"Ludo",
Welcome to the JavaRanch.
We're a friendly group, but we do require members to have valid display names.
Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.
Please edit your profile and correct your display name since accounts with display names get deleted, often without warning
thanks,
Dave
Originally posted by Ernest Friedman-Hill:
The GUI event-handling thread will keep a program from exiting when main() returns. Using any GUI components will start it, and thereafter to make the program exit, you must call System.exit(0).
Originally posted by Ernest Friedman-Hill:
This line:
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Arranges for a WindowEventListener to be attached to the JFrame which calls System.exit(0) when you close the window.
The original poster's program is a sort of hybrid thing which just throws up a few dialogs; when main() returns, the program won't exit by itself because the GUI thread (which is a non-daemon thread) is still running. He could just put System.exit(0) at the end of main().
Originally posted by sven studde:
In java, you can declare and create a new array in one line like this:
int[] nums = new int[10];
But, that is a two part operation, which can be broken up into its constituent parts:
int[] nums;
nums = new int[10];
The first line declares a variable that can refer to an int array. The second line creates an array and sets nums to refer to the array. However, you don't have to write the second line immediately after the first. In other words, you can create the variable, but not assign anything to it until later:
int[] nums;
...
...
nums = new int[5];
In your case, to make a Players array available to the whole class, you can declare a data member for your class:
private Players[] p;
and then sometime later, like in an initPlayers() method, you can create a Players array and set p to refer to it:
p = new Players[5];
(Note: you might want to set p=null in the constructor for the class, so that you at least initialze p when p is created.)
Originally posted by sven studde:
Additionally, which is something I didn't know you could do in java, you can make the size of the array a variable:
p = new Players[numPlayers];
Therefore, you can set the value of the numPlayers variable equal to the user input from the JOptionsPane.
Originally posted by sven studde:
Well, you can go too far with that as well. For instance, I think this is a case of going too far:
[...]
You can always wrap every java function in a function of your own, but that just creates needless overhead.
Originally posted by Garrett Rowe:
Except by wrapping the method it leaves open the possibility that a different implementation may be used. Perhaps in the future you might decide to use an implementation of a Mersenne Twister instead of java.util.Random to generate random numbers, or for testing you might want to create a mock implementation which overrides the createRandomNumber() method to return a non-random predetermined number. You then only have to change a single method implementation and leave the rest of your code untouched.
Originally posted by Ludo Aelbrecht:
So when the value of numPlayers changes, the array is automatically adapted? Seems weird (though useful), will try it out. Isn't it possible in Java to create some kind of dynamic array of which the length isn't known in the beginning, and you can add elements as you want? I think I've seen that before in another language (VB I think).
Joanne
Originally posted by Ludo Aelbrecht:
Ah, I guess that explains it. But since it seems to be VM-dependent, I'm wondering if the VM's which require it are the ones with bugs, or if it's those which don't require it?
What I noticed: if I use Sun's Java 1.5 SDK, it exits even without System.exit(). If I use GNU's GCJ, it doesn't (i.e. it needs System.exit()).
[ October 31, 2006: Message edited by: Ludo Aelbrecht ]
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|