Big Favor Java Program

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

ICONIC

Thread Starter
Joined
Sep 7, 2007
Messages
244
I need a huge favor here, i am a teacher working on a program for my Programming class, the other classes made a program and i was wondering if anyone could help me out with it.


I need to Write a Java console program that will randomly generate the numbers for a Super 7 lottery ticket.

With each ticket the customer receives three sets of seven numbers. Each set of seven numbers is called a “board” in the lottery business. So, if the user buys just one ticket, they get 3 boards. If they buy two tickets, they get 6 boards. If they buy three tickets, they get 9 boards, and so on. Each number on a board is a unique integer (no duplicates permitted) in the range of 1 to 47 (inclusive).


The program should do the following:
• Ask the user how many tickets they want to purchase. Remember, for one ticket you have to generate three rows of seven numbers.
• Generate all the tickets, ensuring no duplicate numbers within each board, and sort them in ascending order (smallest to largest).
• Output the numbers as displayed in the screen shots below:


Figure 1 - Sample input and output
 
Joined
Jul 29, 2004
Messages
6,650
Hi ICONIC,

I've built a class I called Ticket which constructor generates a bidimensional array - type : int[3][7].
The class provides two methods :
getBoards and getMatches

Code:
[SIZE=2]
import java.util.Arrays;

public class Ticket {
    public static final int BOARDS = 3;
    public static final int DRAWS = 7;
    public static final int LIMIT = 47;
    
    private int[][] board;
    
    public Ticket() {
        init();
    }
    
    public int[][] getBoards() {
        return board;
    }
    
    public int[] getMatches(int[] draft) {
        int[] res = new int[BOARDS];
        int[] sorted = draft;
        Arrays.sort(sorted);
        for (int i = 0; i < sorted.length; i++) {
            for (int j = 0; j < BOARDS; j++) {
                for (int k = 0; k < DRAWS; k++) {
                    if (sorted[i]==board[j][k]) {
                        res[j]++;
                        break;
                    }    
                }
            }
        }
        return res;
    }
    
    private void init() {
        board = new int[BOARDS][DRAWS];
        int[] draft;
        for (int i = 0; i < BOARDS; i++) {
            draft = generateDraft();
            for (int j = 0; j < DRAWS; j++) {
                board[i][j] = draft[j];
            }
        }
    }
    
    private int[] generateDraft() {
        int[] draft = new int[DRAWS];
        boolean[] isUsed = new boolean[LIMIT];
        int index;
        for (int i = 0; i < DRAWS; i++) {
            index = (int) (Math.random() * LIMIT);
            while (isUsed[index]) {
                index++;
                if (index==LIMIT) index=0;
            }
            isUsed[index] = true;
            draft[i] = index + 1;
        }
        Arrays.sort(draft);
        return draft;
    }
}
[/SIZE]
Here's a small program to show how to use the Ticket class :
Code:
[SIZE=2]
public class Test {
    
    public static void main(String[] args) {
        System.out.println("");
        Ticket ticket = new Ticket();
        int[][] boards = ticket.getBoards();
        for (int i = 0; i < Ticket.BOARDS; i++) {
            for (int j = 0; j < Ticket.DRAWS; j++) {
                System.out.print(boards[i][j] + " ");
            }
            System.out.println("");
        }
        
        int[] test = { 10, 15, 5, 6, 40, 25, 33 };
        int[] res = ticket.getMatches(test);
        System.out.println("");
        for (int j = 0; j < res.length; j++) {
            System.out.print(res[j] + " ");   
        }
        System.out.println("");
    }
    
}
[/SIZE]
I'll come back later with the console stuff.
 

ICONIC

Thread Starter
Joined
Sep 7, 2007
Messages
244
thanks so much.

i took the top code and got this error:
Tickets2.java:10: class Ticket is public, should be declared in a file named Tic
ket.java
public class Ticket {

also i dont see any prompt or am i missing something?
 
Joined
Jul 29, 2004
Messages
6,650
In my prior post, I told I was not ready with the console output.
BTW, I add a markBoards method in the Ticket to mark the winning numbers.

Here's the complete program (2 classes Ticket and Main stored in the package super7)

Code:
[SIZE=2]
package super7;

import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
    private static Scanner scan;
    private static int input;
    private static int[] win;
    
    public static void main(String[] args) {
        System.out.println("            Get your Super 7 lottery numbers !");
        
        System.out.println("");
        System.out.println("Enter the number of tickets you'd like to purchase (<=3) :");
        while(!getNumberTickets()) {}
        
        System.out.println("");
        System.out.println("Thanks for your purchase ! Here is the detail :");
        
        int[][] board = new int[Ticket.BOARDS][Ticket.DRAWS];
        Ticket[] ticket = new Ticket[input];
        for (int i=0; i < input; i++) {
            System.out.println("");
            System.out.println("Ticket " + (i+1));
            ticket[i] = new Ticket();
            board = ticket[i].getBoards();
            for (int j = 0; j < Ticket.BOARDS; j++) {
                System.out.print("  ");
                for (int k = 0; k < Ticket.DRAWS; k++) {
                    if (board[j][k] > 9) {
                        System.out.print("      " + board[j][k]);
                    } else {
                        System.out.print("       " + board[j][k]);
                    }
                }
                System.out.print("\n");
            }
        }
        
        System.out.println("");
        System.out.println("Enter 0 to quit, or 1 to check your ticket(s) :");
        while (!getChoice()) {}
        if (input==0) System.exit(input);
        
        System.out.println("");
        System.out.println("Enter the seven winning numbers in any order separated by spaces :");
        while (!getWinningNumbers()) {}
        
        System.out.println("");
        boolean[][] isWinning = new boolean[Ticket.BOARDS][Ticket.DRAWS];
        int[] matches = new int[Ticket.BOARDS];
        for (int i = 0; i < ticket.length; i ++) {
            board = ticket[i].getBoards();
            isWinning = ticket[i].markBoards(win);
            matches = ticket[i].getMatches(win);
            System.out.println("Checking ticket " + (i+1));
            for (int j = 0; j < Ticket.BOARDS; j++) {
                System.out.print("       Board " + (j+1) + " : ");
                for (int k = 0; k < Ticket.DRAWS; k++) {
                    if (isWinning[j][k]) {
                        if (board[j][k] > 9) {
                            System.out.print("  " + board[j][k]);
                        } else {
                            System.out.print("  " + board[j][k]);
                        }
                    }
                }
                if (matches[j] > 2) {
                    System.out.print("    (" + matches[j] + " MATCHES WIN !)");
                }
                System.out.print("\n");
            }
        }
    }
    
    private static boolean getNumberTickets() {
        scan = new Scanner(System.in);
        try {
            input = scan.nextInt();
            if ((input < 1) || (input > 3)) {
                System.out.println("The entered number is out of range !");
                System.out.println("Enter the number of tickets you'd like to purchase (<=3) :");
                return false;
            } else {
                return true;
            }
        } catch(InputMismatchException ime) {
            System.out.println("The entered value is not a number !");
            System.out.println("Enter the number of tickets you'd like to purchase (<=3) :");
            return false;
        }
    }
    
    private static boolean getChoice() {
        scan = new Scanner(System.in);
        try {
            input = scan.nextInt();
            if ((input < 0) || (input > 1)) {
                System.out.println("The entered number is out of range !");
                System.out.println("Enter 0 to quit, or 1 to check your ticket(s) :");
                return false;
            } else {
                return true;
            }
        } catch(InputMismatchException ime) {
            System.out.println("The entered value is not a number !");
            System.out.println("Enter 0 to quit, or 1 to check your ticket(s) :");
            return false;
        }
    }
    
    private static boolean getWinningNumbers() {
        win = new int[7];
        scan = new Scanner(System.in);
        String text = scan.nextLine();
        
        if (text.length()==0) {
            System.out.println("Nothing has been entered  !");
            System.out.println("Enter the seven winning numbers in any order separated by spaces :");
            return false;
        } else {
            scan = new Scanner(text);
            for (int i = 0; i < 7; i++) {
                try {
                    win[i] = scan.nextInt();
                    if ((win[i] < 1) || (win[i] > Ticket.LIMIT)) {
                        System.out.println("One of the entred numbers is out of range !");
                        System.out.println("Enter the seven winning numbers in any order separated by spaces :");
                        return false;
                    }
                } catch(InputMismatchException ime) {
                    System.out.println("There are illegal characters !");
                    System.out.println("Enter the seven winning numbers in any order separated by spaces :");
                    return false;
                } catch(NoSuchElementException nsee) {
                    System.out.println("The list of winning numbers is uncomplete !");
                    System.out.println("Enter the seven winning numbers in any order separated by spaces :");
                    return false;
                }
            }
        }
        return true;
    }
}
[/SIZE]
Code:
[SIZE=2]
package super7;

import java.util.Arrays;

public class Ticket {
    public static final int BOARDS = 3;
    public static final int DRAWS = 7;
    public static final int LIMIT = 47;
    
    private int[][] board;
    
    public Ticket() {
        init();
    }
    
    public int[][] getBoards() {
        return board;
    }
    
    public int[] getMatches(int[] draft) {
        int[] res = new int[BOARDS];
        int[] sorted = draft;
        Arrays.sort(sorted);
        for (int i = 0; i < sorted.length; i++) {
            for (int j = 0; j < BOARDS; j++) {
                for (int k = 0; k < DRAWS; k++) {
                    if (sorted[i]==board[j][k]) {
                        res[j]++;
                        break;
                    }    
                }
            }
        }
        return res;
    }

    public boolean[][] markBoards(int[] draft) {
        boolean[][] res = new boolean[BOARDS][DRAWS];
        int[] sorted = draft;
        Arrays.sort(sorted);
        for (int i = 0; i < sorted.length; i++) {
            for (int j = 0; j < BOARDS; j++) {
                for (int k = 0; k < DRAWS; k++) {
                    if (sorted[i]==board[j][k]) {
                        res[j][k] = true;
                        break;
                    }    
                }
            }
        }
        return res;
    }    
    
    private void init() {
        board = new int[BOARDS][DRAWS];
        int[] draft;
        for (int i = 0; i < BOARDS; i++) {
            draft = generateDraft();
            for (int j = 0; j < DRAWS; j++) {
                board[i][j] = draft[j];
            }
        }
    }
    
    private int[] generateDraft() {
        int[] draft = new int[DRAWS];
        boolean[] isUsed = new boolean[LIMIT];
        int index;
        for (int i = 0; i < DRAWS; i++) {
            index = (int) (Math.random() * LIMIT);
            while (isUsed[index]) {
                index++;
                if (index==LIMIT) index=0;
            }
            isUsed[index] = true;
            draft[i] = index + 1;
        }
        Arrays.sort(draft);
        return draft;
    }
}
[/SIZE]
Also, I attached a zip file Super7.zip that contains the executable Super7.jar and a README file. Make sure you have the latest version of your Java runtime.
Good luck if you mean to use the program to play lottery ! ;)


Edit : Just corrected the program - a line was missing
 

Attachments

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top