In: Computer Science
Please Use the ECLIPSE AND FOLLOW THESE RULES
Apply the naming conventions for variables, methods, classes, and packages:
- variable names start with a lowercase character
- classes start with an uppercase character
- packages use only lowercase characters
methods start with a lowercase character
question1:
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 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 and each time the sum of lotto numbers 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.
question2:
Write a Java class that implements a set of three overloaded static methods. The methods should have different set of parameters and perform similar functionalities. Call the methods within main method and display the results.
eclipse...
//Question1
//Lotto.java
package lotto_game;
import java.util.Random;
public class Lotto {
private int arr[];
Lotto() {
Random r = new Random();
arr = new int[3];
for (int i = 0; i < 3; i++)
{
arr[i] =
r.nextInt(9) + 1;
}
}
public int[] getArray() {
return arr;
}
}
========================================================
//Game_driver.java
package lotto_game;
import java.util.Scanner;
public class Game_driver {
public static void main(String[] args) {
int n;
Lotto l;
Scanner input = new
Scanner(System.in);
System.out.print("Enter a number
between (3 and 27): ");
n = input.nextInt();
while (n < 3 || n > 27)
{
System.out.println("Try Again!!");
System.out.print("Enter a number between (3 and 27): ");
n =
input.nextInt();
}
for (int i = 0; i < 5; i++)
{
l = new
Lotto();
int arr[] =
l.getArray();
int sum = arr[0]
+ arr[1] + arr[2];
if (sum == n)
{
System.out.println("You Win!!");
System.exit(0);
}
}
System.out.println("Computer
Wins!!");
}
}
//Output
===========================================================================
===========================================================================
//Question 2
public class Overloaded {
public static void add(int a, int b) {
System.out.println(a + " + " + b +
" = " + (a + b));
}
public static void add(float a, float b) {
System.out.println(a + " + " + b +
" = " + (a + b));
}
public static void add(double a, double b) {
System.out.println(a + " + " + b +
" = " + (a + b));
}
public static void main(String[] args) {
int a = 5, b = 10;
add(a, b);
double c = 5.5060, d =
10.5845;
add(c, d);
float e = (float) 3.3, f = (float)
5.6;
add(e, f);
}
}
//Output