Hello. I decided to instead of doing all of the program in main i would try and write a program that uses different classes and i'm not sure i've done it right. I will briefly explain what the program does and how i have split it into different classes and i would really appreciate it if you could tell me how you would have split up the same program.
It is a lottery program. It starts off in main and creates a Player object which it uses to invoke the setPlayerGuesses method in the Player class. The setPlayerGuesses method asks the user to pick 6 numbers. One at a time it checks to see if the number is between 1 and 49 and also checks to see if the player has already guessed that number. If the number hasn't been guessed before and it's between 1 and 49 it assigns that number to an index in the guess array. If it's been either guessed before or the number is too large or small then the player is asked to guess again. Once it has 6 guesses then the method ends. I made the guess array 7 indexes so i didn't have to worry about the 0 index, as that i found made things unnecessarily confusing.
Then back to the main method which is in the GameLauncher Class. There it creates a lotteryGame object and invokes the startDraw method. This method picks 6 random numbers and uses a boolean array to make sure the number hasn't been drawn before, and if it hasn't then that value is assigned to the balls array.
Then it's back to the main method where it invokes the printWinningNumbers method in the LotterGameClass, which prints the winning numbers. Then back to the main method which invokes the printPlayerGuesses method that is in the Player class.
Before I ask specific questions I will show you the code.
The next thing i want to add to the code is to compare the winning numbers to the player's guesses and print out how many numbers they have got right. Now i'm not a hundred percent sure how i'm going to that yet but i'm sure i will figure it out. The problem i am having is i'm not sure which class i would do that in.
I started writing a checkGuessesToWinningNumbers() method in the lotteryGame class but then i realized i need to get the new vallues of the guess array. So i made another player object and called it p1 (like the last one that was in the main method). But just to check i decided i would print out the value of of guess to see what it said. It said 0. I then realised that this is because the p1 object in the LotteryGame class is a different object to the p1 in the gameLauncher class. Then i was stuck, because i'm not sure how i would get the user values of the guess array into the LotteryGame Class without having to create a new object and then starting again. Can someone please tell me what i'm doing wrong? How would be the best way to organise this program into different classes?
I'm still very new to computer programming and java so my java vocabulary is very basic still and i realise a lot of this code will be very amature and probably more complicated then it needs to be so sorry about that.
Thanks for any help and just let me know if i haven't explained anything well. I'll post this now and check for typos and such after because i'm terrified i'm going to lose all this and have to start again!
This is great so far and I glad you're thinking about all these issues.
The way I would do this: main() creates a Player and a LotteryGame. It tells Player to pick some numbers. It tells LotteryGame to draw some numbers. So far, so good. Now, how to decide if the player wins?
I would give LotteryGame a method like this (not real code):
Then add a method to Player getGuesses() that returns the guesses array, and then main() can do something like
If you want to do something a little more complicated -- print something different if the user got 3, or 4, or 5 numbers right -- then you could change areWinningNumbers() to something like didIWin(guesses) which returns a String, and the String could be the message to display to the user.
Agree w/Ernest. You are on the right track. In general, when considering whether or not to make start splitting you code into multiple classes, I think most usually consider:
1. Readability and organization. A couple clearly written classes is usually much clearer (and easier to troubleshoot) than 1 "mega class."
2. Portability/extensibility! Maybe in the future you might develop another game that can use your Player class, or you might refine your Lottery game by extending it with some embellishments.
So, then the challenge becomes how to exchange necessary information (like the Player's guess) between classes. If you are hung up by Ernest's suggestion, you need to spend a bit of time looking at INSTANCE VARIABLES and GETTER and SETTER methods. Your main (executable) class will need to pull information from one class and send it to another class using these concepts. All introductory Java texts (and many online tutorials) have great coverage of this basic concept.
Joined: Feb 13, 2010
Thanks Bert that information helped me. It was helpful to see some basic ground rules. I started a new program earlier today and i ended up dwelling on the how many classes problem so much i didn't get anywhere with it!
I finished the program that i posted in this topic now. I would post the finished code but it's on the other computer. But i'm really happy with it. I used a getter method, and also a switch statement so i could return a different string depending on how many numbers the player got right.
cheers for the help guys.
subject: Need opinion on how i've divided my program up into different classes.