Ludo Aelbrecht

Greenhorn
+ Follow
since Oct 30, 2006
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 Ludo Aelbrecht

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.


Yes I see what you mean, I kind of overdid it there

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.


That was my line of thinking when I wrote it, or that maybe I'd want to use that random-number method somewhere else in my app.
I guess it's not always very clear when things should be broken down or not. Though in this specific case, it was kind of overkill

Thanks for the suggestions, greatly appreciated.
[ April 02, 2007: Message edited by: LudoA ]
17 years ago

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.)


Great, that's exactly what I need! Thanks a lot. I've gotta say I find the new() stuff a bit confusing, though your explanation helps in understanding it.
I'm curious, why wouldn't I want to initialize p, if I'm sure I'll do it later? Just in case I change the program later, and I try to use p when it's not initialized yet, to avoid a crash? If so, won't it crash anyway since p is null?


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.


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).
17 years ago

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).


System.exit(..) does work, but from searching around, this doesn't seem like a good way of doing things. Most sites say it should be used "with care".

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().


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()).
[ April 02, 2007: Message edited by: LudoA ]
17 years ago

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


Actually, Ludo is my real name - first name. Thanks for the reply, I wasn't aware of this. I've gotta say I'm kind of reluctant to use my last name - not because I want to abuse the forums or anything, I just don't want to use it too much on the 'net. I could use a fake one, but why bother. Anyway since this forum (well, its users) seems really friendly & helpful, I guess I'll abide. I'm surprised of this rule though, never saw that on any other site.
[ April 02, 2007: Message edited by: LudoA ]
17 years ago
Hi all,
I'm reading head first java and tried writing the guess-the-number game myself. It's just a simple game, and it works until I use JOptionPane for a parameter, instead of hard-coding it myself. To decide how many players are guessing numbers, I can use one of these:
g.startGame(Integer.parseInt(JOptionPane.showInputDialog("How many players?")));
OR:
g.startGame(4);

The second solution works fine, the first makes the game work, but it doesn't exit -- I have to kill it myself.

Here are my classes:
headclass - GuessGame



Player class



Testing class - GameTest


What am I doing wrong?
Also, I'm *very* interested in any tips concerning my code: style, breaking down in enough methodes,... and any other thing where my logic isn't good.
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? But how can I do this, since I don't know the numberOfPlayers at the start of the class...
I hope my explanation is a bit clear.

Thanks!

[ I split up some extra-long code lines that were screwing up the page formatting - Jim ]
[ October 30, 2006: Message edited by: Jim Yingst ]
17 years ago