Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

I'm studying comp science in uni and this week our task is this;

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).
And I'm fucking stumped, the two java files are here;

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;
    }
}

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;
    }
}
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.

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++)
       
    }
}
I know it's probably completely wrong, but there you go, I'm shit at Java.


Sorry for the long op, but thanks for reading it.
Titch2349
iz me!
+358|6349|uk

Meh, ask me PHP, JS, HTML, CSS and I would be glad to help.

But urrr.... Java?

I'm sure "exit", or "die" is a function!?!?!
Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

Titch2349 wrote:

Meh, ask me PHP, JS, HTML, CSS and I would be glad to help.

But urrr.... Java?

I'm sure "exit", or "die" is a function!?!?!


Damn, there must be someone that knows Java on here though
Finray
Hup! Dos, Tres, Cuatro
+2,629|5785|Catherine Black

The Sheriff wrote:

Titch2349 wrote:

Meh, ask me PHP, JS, HTML, CSS and I would be glad to help.

But urrr.... Java?

I'm sure "exit", or "die" is a function!?!?!


Damn, there must be someone that knows Java on here though
I d-

Nah I'm not even going to try to fake it.
https://i.imgur.com/qwWEP9F.png
liquidat0r
wtf.
+2,223|6624|UK
I seem to recall that jsnipy knows Java.
Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

Ok, I have got as far as to generate the 6 random balls, create an array of pre-defined balls, all I need to do now is compare the random balls to the pre-defined ones and print the results to command line. It says to use nested loops, any ideas.

Here is the code I have so far.

public class Driver
{
    public static int[] ballsArray = {0,3,2,13,18,1};
    public static int[] lotteryBalls = new int[6];
    public static int ballsMatched = 0;
    public static void main(String[] args)
    {
        LotteryMachine camelot = new LotteryMachine();
        LotteryBall a = camelot.chooseBall();
        LotteryBall b = camelot.chooseBall();
        LotteryBall c = camelot.chooseBall();
        LotteryBall d = camelot.chooseBall();
        LotteryBall e = camelot.chooseBall();
        LotteryBall f = camelot.chooseBall();       
        lotteryBalls[0] = a.getValue();
        lotteryBalls[1] = b.getValue();
        lotteryBalls[2] = c.getValue();
        lotteryBalls[3] = d.getValue();
        lotteryBalls[4] = e.getValue();
        lotteryBalls[5] = f.getValue();       
        //for (int i=0; i<7; i++)
            //for (int j=0; j<7; j++)           
        System.out.println("Hi, and welcome to the CSc112 Lottery.");
        System.out.println("This weeks numbers are: " + lotteryBalls[0] + ", " + lotteryBalls[1] + ", " + lotteryBalls[2] + ", " + lotteryBalls[3] + ", " + lotteryBalls[4] + ", " + lotteryBalls[5] + ".");
        System.out.println("Your numbers were: " + ballsArray[0] + ", " + ballsArray[1] + ", " + ballsArray[2] + ", " + ballsArray[3] + ", " + ballsArray[4] + ", " + ballsArray[5] + ".");
    }
}
killer21
Because f*ck you that's why.
+400|6588|Reisterstown, MD

liquidat0r wrote:

I seem to recall that jsnipy knows Java.
I think it was him.  I can't remember.
SpIk3y
Minister of Silly Walks
+67|6136|New Jersey
It's been 2 years since I've programmed anything at all, so this is quite a stretch for me.  I can't give you code, because I simply don't remember the language.  You'll have to write a search method, which can be a loop depending on how you write it, to search through the array of randomly generated lottery balls and compare them to the predefined user choices.  Since the array is so small, you should just use a linear search (compare each item one-by-one).  You'll have to do this process 6 times, once for each user choice, so that's where the nested loops comes in. 

This could help you with the search:  http://math.hws.edu/eck/cs124/javanotes3/c8/s4.html
And google is always helpful if you're looking for specific info - it's all been written before

Last edited by SpIk3y (2009-01-29 12:48:39)

Titch2349
iz me!
+358|6349|uk

The Sheriff wrote:

Ok, I have got as far as to generate the 6 random balls, create an array of pre-defined balls, all I need to do now is compare the random balls to the pre-defined ones and print the results to command line. It says to use nested loops, any ideas.
A nested loop is a loop within a loop.

It will be something like this (sorry... this is written in Javascript! )

var userInput = Array(1,2,3,4,5,6);
var machineOutput = Array(2,3,4,5,6);

var matches = Array();

// We are looping through all the machine values.
for(var i=0;i<machineOutput.length;i++) {
    // This machine value is machineOutput[i];
    var machineValue = machineOutput[i];
   
    // Now loop through all the userInput values, and see if any match
    for(var j=0;j<userInput.length;j++){
        // The user input is userInput[j];
        var userValue = userInput[j];
       
        // Compare- does the machineValue and userInput value match?
        if(userValue == machineValue) {
            // Yes they do! add the matching value to the matches array.
            matches.push(userValue);
        } else {
            // No they don't... but nvm, carry on....
        }
    }
   
    // When we get here we have gone through all the userInputs for that machineValue- i.e. we have compared each userInput to the machineOutput, so know if the machine ball has been picked by the user.
    // Now we do the same for the next ball
}

// When we get here, we have compared every ball. The values in "matches" array, are the values that both the machine picked, and the user chose.

alert("Huzzah, you chose " + matches.length + " balls correctly!");
You need to be driving towards something like....
public class Driver
{
    public static int[] ballsArray = {0,3,2,13,18,1};
    public static int[] lotteryBalls = new int[6];
    public static int ballsMatched = 0;
    public static void main(String[] args)
    {
        LotteryMachine camelot = new LotteryMachine();
        LotteryBall a = camelot.chooseBall();
        LotteryBall b = camelot.chooseBall();
        LotteryBall c = camelot.chooseBall();
        LotteryBall d = camelot.chooseBall();
        LotteryBall e = camelot.chooseBall();
        LotteryBall f = camelot.chooseBall();       
        lotteryBalls[0] = a.getValue();
        lotteryBalls[1] = b.getValue();
        lotteryBalls[2] = c.getValue();
        lotteryBalls[3] = d.getValue();
        lotteryBalls[4] = e.getValue();
        lotteryBalls[5] = f.getValue();       
        System.out.println("Hi, and welcome to the CSc112 Lottery.");
        System.out.println("This weeks numbers are: " + lotteryBalls[0] + ", " + lotteryBalls[1] + ", " + lotteryBalls[2] + ", " + lotteryBalls[3] + ", " + lotteryBalls[4] + ", " + lotteryBalls[5] + ".");
        System.out.println("Your numbers were: " + ballsArray[0] + ", " + ballsArray[1] + ", " + ballsArray[2] + ", " + ballsArray[3] + ", " + ballsArray[4] + ", " + ballsArray[5] + ".");
       
        // Translate the for loops I did into java, and use them here.
        // Instead of doing "  matches.push(userValue);" as I did (for matches), increment ballsMatched by one (ballsMatched++ ?)
       
        // After the for loop has been done, instead of "alerting" the result as I did, you should be doing something like...
        System.out.println("Huzzah, you matched " + ballsMatched + " balls!");
    }
}

Last edited by Titch2349 (2009-01-29 13:39:25)

Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

I knew what nested loops were

I figured it out though, that last post helped a bit, I was getting ArrayOutOfBounds exceptions before but now it works.

Thanks for the help. Karma++
Titch2349
iz me!
+358|6349|uk

The Sheriff wrote:

I knew what nested loops were

I figured it out though, that last post helped a bit, I was getting ArrayOutOfBounds exceptions before but now it works.

Thanks for the help. Karma++
Heh...

is

public class Driver
{
    public static int[] ballsArray = {0,3,2,13,18,1};
    public static int[] lotteryBalls = new int[6];
    public static int ballsMatched = 0;
    public static void main(String[] args)
    {
        LotteryMachine camelot = new LotteryMachine();
       
        // Initiate balls;
        for(int s=0; s<7; s++) {
                lotteryBalls[s] = camelot.chooseBall();
        }
       
        System.out.println("Hi, and welcome to the CSc112 Lottery.");
        System.out.println("This weeks numbers are: " + lotteryBalls[0] + ", " + lotteryBalls[1] + ", " + lotteryBalls[2] + ", " + lotteryBalls[3] + ", " + lotteryBalls[4] + ", " + lotteryBalls[5] + ".");
        System.out.println("Your numbers were: " + ballsArray[0] + ", " + ballsArray[1] + ", " + ballsArray[2] + ", " + ballsArray[3] + ", " + ballsArray[4] + ", " + ballsArray[5] + ".");
       
        for(int i=0;i<lotteryBalls.length;i++){
            for(int j=0;j<ballsArray.length;j++){
                if(lotteryBalls[i] == ballsArray[j]) {
                    ballsMatched++;
                }
            }
        }
       
        System.out.println("Huzzah, you matched " + ballsMatched + " balls!");
    }
}
anywhere close to the solution ?

(First attempt at Java )
Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

This was my final solution, I think you were spot on.

for (int i=0; i<6; i++)
            {
            for (int j=0; j<6; j++)
                {
                    if (ballsArray[i] == lotteryBalls[j])
                    {
                        ballsMatched++;
                    }
                    else
                    {
                        ballsMatched = ballsMatched;
                    }
                }
            }
ghettoperson
Member
+1,943|6646

Fuckin' hate Java.
Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

And now we go on to the last part.

I basically had to store the balls in a linked list, I have this done correctly, but when scanning the list and checking against the int array it doesn't work properly.

public class Lottery
{
    public static int[] ballsArray = {0,3,2,13,18,1};
    public static int ballsMatched = 0;
    public static void main(String[] args)
    {
        LotteryMachine camelot = new LotteryMachine();
        LotteryBall firstBall;
        LotteryBall currentBall;
        LotteryBall a = camelot.chooseBall();
        firstBall = a;
        currentBall = firstBall;
        System.out.println("Your numbers are " + ballsArray[0] + ", " + ballsArray[1] + ", " + ballsArray[2] + ", " + ballsArray[3] + ", " + ballsArray[4] + ", " + ballsArray[5] + ".");
        System.out.print("This weeks lottery numbers are " + a.getValue() + ", ");
        for (int i = 0; i < 5; i++)
        {           
            LotteryBall b = camelot.chooseBall();
            currentBall.next = b;
            currentBall = currentBall.next;   
            System.out.print(b.getValue() + ", ");           
        }
        while (currentBall.next != null);
        {
        for (int i=0; i<6; i++)
            {               
                if (ballsArray[i] == currentBall.getValue())
                {
                    ballsMatched++;
                }
                else
                {                   
                }
                currentBall = currentBall.next;                   
            }
        }           
        System.out.println("");
        System.out.println("You matched " + ballsMatched + " balls!");
    }   
}
It compiles ok, but most of the time it prints out that ballsMatched = "0", and occasionally it prints out "1".

Any clues?
Flaming_Maniac
prince of insufficient light
+2,490|6704|67.222.138.85
Are you sure your linked list is correct? It prints as it should?

edit: We have just done linked lists in my java class, and I can't count the times when someone thought their insert method or something similar was working correctly but definitely wasn't.
Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

Nevermind, I got it working.

I changed;

while (currentBall.next != null);
        {
        for (int i=0; i<6; i++)
            {               
                if (ballsArray[i] == currentBall.getValue())
                {
                    ballsMatched++;
                }
                else
                {                   
                }
                currentBall = currentBall.next;                   
            }
        }
To

while (currentBall.next != null);
        {
        currentBall = firstBall;
        for (int i=0; i<6; i++)
            {               
                if (ballsArray[0] == currentBall.getValue() || ballsArray[1] == currentBall.getValue() || ballsArray[2] == currentBall.getValue() || ballsArray[3] == currentBall.getValue() || ballsArray[4] == currentBall.getValue() || ballsArray[5] == currentBall.getValue())
                {
                    ballsMatched++;
                }
                else
                {                   
                }
                currentBall = currentBall.next;                   
            }
        }
Flaming_Maniac
prince of insufficient light
+2,490|6704|67.222.138.85
You're supposed to use nested loops for that haha.
phishman420
Banned
+821|5678
I don't know how people could do that stuff for a living. I guess they get used to it after a while, but fuckin a that shit looks boring.
Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

Flaming_Maniac wrote:

You're supposed to use nested loops for that haha.
I couldn't be arsed in the end so just took the easy option, it's getting late and I'm not working on this much longer
Rod Foxx
Warblgarbl
+78|5981|Perth, Australia

phishman420 wrote:

I don't know how people could do that stuff for a living. I guess they get used to it after a while, but fuckin a that shit looks boring.
Personally i fucking love that shit. It's basically just problem solving on a large scale and it keeps me up to all hours of the morning if i can't come up with the answer.

Sorry i didn't see the thread until you had already solved the problem.

Good luck with the tasks though.
Surgeons
U shud proabbly f off u fat prik
+3,097|6487|Gogledd Cymru

Rod Foxx wrote:

Sorry i didn't see the thread until you had already solved the problem.

Good luck with the tasks though.
I get a new assignment today, don't worry haha
AussieReaper
( ͡° ͜ʖ ͡°)
+5,761|6150|what

Hey sheriff I know what your going through.

Don't worry though, you'll find a lot of functions get used again and again. And rewriting code becomes 2nd nature.

And don't forget:

Hello World!
https://i.imgur.com/maVpUMN.png
Defiance
Member
+438|6668

Rod Foxx wrote:

phishman420 wrote:

I don't know how people could do that stuff for a living. I guess they get used to it after a while, but fuckin a that shit looks boring.
Personally i fucking love that shit. It's basically just problem solving on a large scale and it keeps me up to all hours of the morning if i can't come up with the answer.
Same, which is why I'm in to networking. As with anything else, when I have a goal in mind, nothing get's in the way. I might like programming but I was never motivated to start, though I did dabble in web design for a little while. Not that they explicitly compare or anything.

Board footer

Privacy Policy - © 2024 Jeff Minard