In: Computer Science
Java language
Exercise #2:
Design a Lotto class with one array instance variable to hold three
random integer values (from 1 to 9). Include a constructor that
randomly populates the array for a lotto object. Also, include a
method in the class to return the array.
Use this class in the driver class (LottoTest.java) to simulate a
simple lotto game in which the user chooses a number between 3 and
27. The user runs the lotto up to 5 times ( by creating an object
of Lotto class each time and with that three random integer values
will be stored in objects’s array instance variable) and each time
the sum of lotto numbers (sum of three random integers values) is
calculated. If the number chosen by the user matches the sum, the
user wins and the game ends. If the number does not match the sum
within five rolls, the computer wins.
Exercise #3:
Write a Java class that implements a static method –
SortNumbers(int… numbers) with variable number of arguments. The
method should be called with different numbers of parameters and
does arrange the numbers in descending order. Call the method
within main method of the driver classand display the results.
Exercise 2#:
Screenshot of the code:
Sample Output:
Code to copy:
//Lotto.java:
//Import required packages.
import java.util.Random;
import java.util.Scanner;
//Define the class Lotto.
class Lotto
{
//Declare an instance variable for an array of
//integers.
private int[] randArr;
//Define the default constructor of the class Lotto.
public Lotto()
{
//Create an array of 3 integers.
randArr = new int[3];
//Create an object of the class Random.
Random ran = new Random();
//Start a for loop till the size of the array.
for(int i = 0; i < 3; i++)
{
//Generate a random number between 1 and 9 for
//each array value.
randArr[i] = ran.nextInt(9) + 1;
}
}
//Define the function getRandArr().
public int[] getRandArr()
{
//Return the array of 3 random integers between 1
//and 9.
return randArr;
}
}
//LottoTest.java:
//Define the class LottoTest.
class LottoTest
{
//Start the execution of the main() method.
public static void main(String[] args)
{
//Create an object of the Scanner class.
Scanner sc = new Scanner(System.in);
//Declare and initialize required variables.
int num, sumOfArrValues, isUserWin, index;
isUserWin = 0;
//Prompt the user to enter a number between 3 and
//27.
System.out.print("Choose a number between 3 and "
+ "27: ");
num = sc.nextInt();
//Start a while loop till the value entered by the
//user is less than 3 or greater than 27.
while(num < 3 || num > 27)
{
//Prompt the user to enter the number between 3
//and 27 again.
System.out.print("Please choose a valid number "
+ "between 3 and27: ");
num = sc.nextInt();
}
//Start a for loop till the value of 5.
for(index = 0; index < 5; index++)
{
//Create an object of the class Lotto.
Lotto lottoObj = new Lotto();
//Get the array from the function getRandArr() of
//the Lotto class.
int[] randArr = lottoObj.getRandArr();
//Initialize the value of the variable
//sumOfArrValues to 0.
sumOfArrValues = 0;
//Start another for loop till the size of the
//array randArr.
for(int i = 0; i < randArr.length; i++)
{
//Add array values to the variable
//sumOfArrValues.
sumOfArrValues = sumOfArrValues +
randArr[i];
}
//If the value of the variable sumOfArrValues is
//equal to the variable num, then display
//message that user wins, set the value of the
//variable isUserWin to 1 and break the loop.
if(sumOfArrValues == num)
{
System.out.println("You win!");
isUserWin = 1;
break;
}
}
//If the value of the variable isUserWin is still 0
//and the value of the for loop index is 5, then
//display the message computer wins.
if(isUserWin == 0 && index == 5)
{
System.out.println("Computer wins!");
}
}
}
Exercise 3#:
Note: The code for the required function SortNumbers() is highlighted in grey and bold in the code given below.
Screenshot of the code:
Sample Output:
Code to copy:
//Import the required package.
import java.util.Arrays;
//Define the class Main.
class Main
{
//Define the static method SortNumbers() having a
//variable number of arguments.
public static void SortNumbers(int... numbers)
{
//Declare and initialize the required variables.
int index, temp;
index = temp = 0;
//Create an integer array of size equal to the
//length of the given parameter of the function
//numbers.
int[] integerArr = new int[numbers.length];
//Start a for each loop over numbers.
for(int num : numbers)
{
//Assign num to the current array value.
integerArr[index++] = num;
}
//Traverse the array using nested for loops.
for(int i = 0; i < integerArr.length; i++)
{
for(int j = i + 1; j < integerArr.length; j++)
{
//If the value of the array at index i is less
//than the value of the array integerArr at
//index j.
if(integerArr[i] < integerArr[j])
{
//Swap both values using a third variable
//temp.
temp = integerArr[i];
integerArr[i] = integerArr[j];
integerArr[j] = temp;
}
}
}
//Display array values after sorting using a for
//loop.
System.out.println("Array after sorting:");
System.out.print("[");
for(int arr_index = 0; arr_index <
integerArr.length; arr_index++)
{
if(arr_index != (integerArr.length - 1))
{
System.out.print(integerArr[arr_index]
+ ", ");
}
else
{
System.out.println(integerArr[arr_index]
+ "]");
}
}
}
//Start the execution of the main() method.
public static void main(String[] args)
{
//Call the function SortNumbers() using variable
//number of arguments.
SortNumbers(1, 2, 5, 10, 4, 6);
System.out.println();
//Call the function SortNumbers() using variable
//number of arguments.
SortNumbers(7, 12, 8, 11);
}
}