There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen blue screen of death boot computer connection crash css dell display driver drivers email error firefox firefox 3 game hard drive internet internet explorer itunes laptop lcd linux malware monitor network networking outlook outlook 2003 outlook express partition password printer problem problems ram router security slow software sound trojan usb virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Big Favor Java Program


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
ICONIC's Avatar
Computer Specs
Senior Member with 208 posts.
 
Join Date: Sep 2007
Experience: Intermediate
02-Nov-2007, 04:03 PM #1
Big Favor Java Program
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
__________________
People say they know a lot about themselves. I just know more about other things.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,681 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
02-Nov-2007, 08:47 PM #2
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:

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;
    }
}
Here's a small program to show how to use the Ticket class :
Code:

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("");
    }
    
}
I'll come back later with the console stuff.
ICONIC's Avatar
Computer Specs
Senior Member with 208 posts.
 
Join Date: Sep 2007
Experience: Intermediate
02-Nov-2007, 09:07 PM #3
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?

Last edited by ICONIC : 02-Nov-2007 10:25 PM.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,681 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
03-Nov-2007, 06:53 AM #4
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:

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

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;
    }
}
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
Attached Files
File Type: zip Super7.zip (4.3 KB, 0 views)
__________________
Never teach an old monkey how to make faces. - (French maxim)

Last edited by Chicon : 03-Nov-2007 11:21 AM.
ICONIC's Avatar
Computer Specs
Senior Member with 208 posts.
 
Join Date: Sep 2007
Experience: Intermediate
03-Nov-2007, 12:59 PM #5
Thanks, i really appreciate this
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:16 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.