Question

In: Computer Science

Respond to the following in a minimum of 175 words: Describe how you would create a...

Respond to the following in a minimum of 175 words:

  • Describe how you would create a Java program for creating a lottery program using arrays and methods. If the user wants to purchase 5 lottery tickets, which looping structure would you use and why? Include the necessary code and code descriptions.

Solutions

Expert Solution

As the question requires, we need to use the arrays for the lottery system. then for obtaining the lottery number, we are to use the Random class to generate the random/lottery number. This program requires the use of various methods, for example, to check whether a ticket is available or not, or to check if the user wor or not. The working code for this question is:

import java.io.PrintStream;
import java.util.*;

public class Test {
    private static int[] ticketNumber;
    private static int[] usersTickets;
    public static void main(String[] args) {
        ticketNumber = new int[100];
        for(int i=0; i<100; i++){
            ticketNumber[i] = i+1;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome to the Lottery game.\n---------------------------------------------------------");
        System.out.println("How many tickets would you like to draw: ");
        int n = sc.nextInt();
        usersTickets = new int[n];
        int i=0;
        while (i < n) {
            System.out.println("Enter number for ticket " + (i+1)+ ": ");
            int num = sc.nextInt();
            if (num <= ticketNumber.length) {
                if (isAvailable(ticketNumber, num)) {
                    ticketNumber = removeFromArray(ticketNumber, num);
                    System.out.println("good you have taken ticket numbered "+num);
                    usersTickets[i] = num;
                    i++;
                }
                else
                    System.out.println("Ticket number not available!");
            }
            else {
                System.out.println("This much tickets not available. Enter a smaller number: ");
            }
        }

        System.out.println("Getting the lottery draw........................");
        int luckyNo = luckyNumber();
        System.out.println("The lucky number is "+luckyNo);
        int result = didYouWin(usersTickets, luckyNo);
        if(result != 0)
            System.out.println("Congratulations!!! Yoy won the lottery on ticket "+luckyNo);
        else
            System.out.println("Better luck next time!!!!");
    }

    public static int didYouWin(int[] yourTickets, int luckyNo){
        for(int i = 0; i<yourTickets.length; i++){
            if(yourTickets[i]==luckyNo){
                return i;
            }
        }
        return 0;
    }

        public static int luckyNumber () {
            Random rand = new Random();
            return rand.nextInt(100) + 1;
        }

        public static boolean isAvailable ( int[] arr, int num){
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] == num)
                    return true;
            }
            return false;
        }

        public static int[] removeFromArray ( int[] arr, int num){
            int[] newArray = new int[arr.length - 1];
            for( int i=0, k=0; i<newArray.length; i++){
                if(arr[i] == num){
                    continue;
                }
                newArray[k++] = arr[i];
            }
            return newArray;
        }
    }



Related Solutions

Respond to the following in a minimum of 175 words: Share how you would describe the...
Respond to the following in a minimum of 175 words: Share how you would describe the overall purpose and mechanics of both primary and secondary markets. How would you explain the way the performance of your company is influenced by the activity of the markets you described? After your initial post, choose a classmate’s approach that is different from the approach you’d take on the guest lecture. What additional information might you include in your lecture based on your classmate’s...
Respond to the following in a minimum of 175 words: How would you choose to invest...
Respond to the following in a minimum of 175 words: How would you choose to invest your retirement savings? Why did you choose that option(s)? What should you take into consideration when beginning to plan for investments? How could you address investment risk in your plan?
Respond to the following in a minimum of 175 words: Provide an example of how an...
Respond to the following in a minimum of 175 words: Provide an example of how an international company (such as Unilever or Mondelez International) changes its distribution channels and/or marketing messages based on country.
Respond to the following in a minimum of 175 words: How will you graphically represent data...
Respond to the following in a minimum of 175 words: How will you graphically represent data in your future career? What types of graphs are typically used in that industry? What types of conclusions are drawn from the graphs?
Respond to the following in a minimum of 175 words: Describe the 3 main categories of...
Respond to the following in a minimum of 175 words: Describe the 3 main categories of ratios and provide a specific example of a ratio that is used in each category. For each of the 3 ratios you selected, describe how it is used in managerial decision-making.
Respond to the following in a minimum of 175 words: Describe which phases of the systems...
Respond to the following in a minimum of 175 words: Describe which phases of the systems development lifecycle should include a phase review. Provide a real-life example of a phase review from your own experience or that you have found from your own research.
Respond to the following in a minimum of 175 words: How has the supply and demand...
Respond to the following in a minimum of 175 words: How has the supply and demand of a specific product affected your/your family’s purchasing decisions? What variables most affect your decision to either buy or not buy a good or service?
Please respond to the following in a minimum of 175 words: Provide an example of how...
Please respond to the following in a minimum of 175 words: Provide an example of how an international company (such as Unilever or Mondelez International) changes its distribution channels and/or marketing messages based on country.
Respond to the following in a minimum of 175 words: This week, we consider how to...
Respond to the following in a minimum of 175 words: This week, we consider how to conduct hypotheses test on one sample data. Discuss the concepts associated with these tests. Consider the following: The difference between a one tail and a two tailed test. The importance of stating the null and alternative hypotheses before conducting the test. The importance of a type one error (p) in conducting the test   The relationship between the p value and our decision to accept...
Respond to the following in a minimum of 175 words: Capital Budgeting Decisions Describe the six...
Respond to the following in a minimum of 175 words: Capital Budgeting Decisions Describe the six models of a capital budgeting decision, which are typically defined as a 'go or no-go' decision. These are - 1. Payback period (standard) 2. Discounted payback period (modified from payback period) 3. Net present value (NPV) (standard) 4. Internal rate of return (IRR) (standard) 5. Modified internal rate of return (MIRR) (modified from IRR) 6. Profitability index (PI) (modified from NPV) In reviewing these,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT