C++ This should be a simple edit of the origional code from 3.8. Complete the ad
ID: 675453 • Letter: C
Question
C++ This should be a simple edit of the origional code from 3.8. Complete the additions of 3.15 before completing the main problem I am stuck on ( 20.8 )
3.15 : Revise Listing 3.8, Lottery.java (below), to generate a lottery of a threedigit
number. The program prompts the user to enter a three-digit number and
determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is
$10,000.
2. If all digits in the user input match all digits in the lottery number, the award is
$3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is
$1,000
20.8 (Game: lottery) Revise Programming Exercise 3.15 to add an additional $2,000
award if two digits from the user input are in the lottery number. (Hint: Sort
the three digits in the lottery number and three digits in the user input into two
lists, and use the Collection’s containsAll method to check whether the
two digits in the user input are in the lottery number.)
Program 3.8:
import java.util.Scanner;
public class Lottery {
public static void main(String[] args) {
// Generate a lottery number
int lottery = (int)(Math.random() * 100);
// Prompt the user to enter a guess
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (two digits): ");
int guess = input.nextInt();
// Get digits from lottery
int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;
// Get digits from guess
int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;
System.out.println("The lottery number is " + lottery);
// Check the guess
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if (guessDigit2 == lotteryDigit1
&& guessDigit1 == lotteryDigit2)
System.out.println("Match all digits: you win $3,000");
else if (guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry, no match");
}
}
Explanation / Answer
Answer :
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
public class Lottery {
public static void main(String[] strings) {
// Generate a lottery first
int lottery = (int)(Math.random() * 1000);
// Prompt the user to enter a guess three digit number
java.util.Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery number i.e three digits: ");
int guess = input.nextInt();
// Keep the lottery and guess digits in two separate list then sort
List<Integer> lotteryList = getDigits(lottery, 3);
Collections.sort(lotteryList);
List<Integer> guessList = getDigits(guess, 3);
Collections.sort(guessList);
System.out.println("Lottery number is: " + lottery);
// Examine the guess based on given lottery numbers
if (guess == lottery) // exact match found
System.out.println("Exact match found !!!!: you win $10,000");
else if (lotteryList.equals(guessList)) // match all 3 digits which you entered in any order
System.out.println("Match all digits which you entered: you win $3,000");
else if (lotteryList.containsAll(guessList.subList(0, 2)) || // If 2 digits are matched
lotteryList.containsAll(guessList.subList(1, 3)) ||
(lotteryList.contains(guessList.get(0)) && lotteryList.contains(guessList.get(3))))
System.out.println("Match of two digits is found for the given input number: you win $2,000");
else if (lotteryList.contains(guessList.get(0)) || // If 1 digit is matched
lotteryList.contains(guessList.get(1)) ||
lotteryList.contains(guessList.get(2)))
System.out.println("Match of one digit is found for the given input number: you win $1,000");
else
System.out.println("Sorry, no match");
}
public static ArrayList<Integer> getDigits(int number, int digitCount) {
int count = 0;
ArrayList<Integer> list = new ArrayList<>(digitCount);
while (number > 0) {
list.add(number % 10);
number /= 10;
count++;
}
while (count < digitCount) {
list.add(0, 0);
count++;
}
return list;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.