I'm studying comp science in uni and this week our task is this;
Sorry for the long op, but thanks for reading it.
And I'm fucking stumped, the two java files are here;Imagine that you are running a lottery similar to the national lottery. Your challenge is to write a java application which will:
* Generate six lottery balls at random
* Store those lottery balls in an array
* Compare the result of the lottery with a predefined list of a player's chosen numbers, to see how many balls match
* Display the result of lottery to the console. This should include the balls chosen at random, the player's chosen numbers, and how many of the player's chosen numbers were drawn in the lottery.
In order to help you on your way, we've written two java classes for you. You should use these classes in your program. Firstly, there's a LotteryBall class which holds information (not surprisingly) about a lottery ball, and a LotteryMachine class which will create LotteryBalls for you at random. You don't need to change any Java code in these classes for this exercise - just use them, but to do that you'll need to download the two class files and store them in the same directory as your program. For more information on what these classes can provide you with, see the online documentation (written in javadoc, of course!).
Note - Gathering user input isn't the focus of this exercise, so setting your lottery numbers via command line parameters like you did in Week2 is fine.
Try to make your program output as close as possible to the following format:
Hi, and welcome to Joe's CSc112 Lottery.
This week's Lottery numbers are 5,2,13,7,9,8
Your numbers were 7,8,9,10,11,12
You matched 3 numbers!
HINTS
* Start by creating your own class, with a main method in it (a driver program).
* Use an integer array with 6 elements to hold your player's choice of lottery numbers. These are the balls you have bet on.
* Create an instance of LotteryMachine, and use it to generate your six LotteryBalls. You can also store these in an array...
* Once your two arrays have been set up, search through the array of LotteryBalls to see how many of the values generated match the values you chose. (BIG HINT - this will require a nested loop).
LotteryBall wrote:
/**
* This class holds the value of a single lottery ball. Typically, LotteryBall
* instances are obtained via a LotteryMachine.
*
* @see LotteryMachine
*/
public class LotteryBall{
/**
* The face value of this lottery ball.
*/
private int value;
/**
* A reference to another LotteryBall object. This is used for
* maintaining a linked list of lottery balls.
*/
public LotteryBall next;
/**
* Constructs a default LotteryBall. The ball is generated with a
* face value of zero and a null next reference.
*/
public LotteryBall(){
value=0;
next=null;
}
/**
* Constructs a LotteryBall of the specified face value. The ball is
* generated with a null next reference.
*
* @param newValue The face value of the LotteryBall.
*/
public LotteryBall(int newValue){
value=newValue;
next=null;
}
/**
* Update the face value of this LotteryBall to the specified value.
*
* @param newValue the new value for the lottery ball.
*/
public void setValue(int newValue){
value=newValue;
}
/**
* read the face value of this LotteryBall.
*
* @return the current value of this lottery ball.
*/
public int getValue(){
return value;
}
}
So far I've got this and I'm stuck, is there anyone that can provide hints or clues where to go from here, no I'm not asking you to write it for me, although it would be nice, just a helping hand.LotteryMachine wrote:
/**
* This class models a lottery machine, capable of supplying a non-repeating
* pattern of LotteryBall objects. The LotteryMachine class can provide
* LotteryBall objects with a face value between 1 and 20. Once a LotteryBall
* number has been supplied, it will not be provided again unless the reset
* method is invoked.
*
* The range of numbers provided (1-20) is currently internally fixed, and
* cannot be changed.
*/
public class LotteryMachine
{
/**
* The number of balls inside this LotteryMachine.
*/
private int MAX_BALL_VALUE;
/**
* An array holding a list of balls which have been already chosen.
*/
private boolean[] ballsUsed;
/**
* The number of balls left in the machine
*/
private int ballsLeft;
/**
* This constructor will create a default LotteryMachine, which will
* generate LotteryBall objects with a face value ranging from 1 to 20.
*/
public LotteryMachine()
{
// Create default of 20 lottery balls, ready to be chosen;
MAX_BALL_VALUE = 20;
ballsUsed = new boolean[MAX_BALL_VALUE];
for (int i = 0; i< MAX_BALL_VALUE; i++)
ballsUsed[i] = false;
// No balls chosen yet!
ballsLeft = MAX_BALL_VALUE;
}
/**
* Select a lottery number which has not yet been chosen.
*
* @return a LotteryBall with a unique face value between 1 and 20,
* unless this LotteryMachine is empty (has supplied all its
* LotteryBalls), in which case a value of null is returned.
*/
public LotteryBall
chooseBall()
{
double rnd;
int randomValue;
int ball = 0;
// Sanity check - make sure we have some balls left in
// the machine!
if (ballsLeft == 0)
return null;
// Get a random number between 0.0 and 1.0
rnd = Math.random();
// Use this to form a random integer between 1 and ballsLeft.
randomValue = ((int) (rnd * (double) (ballsLeft - 1))) + 1;
// OK, now find the n'th ball still in the machine, where n
// is the random number just chosen...
for (int i=0; i<MAX_BALL_VALUE; i++){
if (!ballsUsed[i])
randomValue--;
if (randomValue == 0){
ball = i;
break;
}
}
// Mark that ball as used, so we don't select it again...
ballsUsed[ball] = true;
ballsLeft--;
// Finally, create a new LotteryBall object with the chosen
// face value and return it...
return new LotteryBall(ball+1);
}
/**
* Resets the LotteryMachine to its default state. This metaphorically
* reinserts all used lottery balls back into the machine.
*/
public void reset()
{
for (int i=0; i<MAX_BALL_VALUE; i++)
ballsUsed[i] = false;
ballsLeft = MAX_BALL_VALUE;
}
}
I know it's probably completely wrong, but there you go, I'm shit at Java.What I've got so far wrote:
public class Driver
{
public int[] ballsArray = new int[6];
public int[] lotteryBalls = new int[6];
public static void main(String[] args)
{
LotteryMachine camelot = new LotteryMachine();
for(int i=0; i<7; i++)
}
}
Sorry for the long op, but thanks for reading it.