Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write two Java classes, one called MyLottery and the other called MyLotteryDemo.

ID: 3628014 • Letter: W

Question

Write two Java classes, one called MyLottery and the other called MyLotteryDemo.

The MyLottery Java class will have three private fields;
-one for a winner’s first name
-one for a winner’s last name
-one for a winning dollar amount

Create a constructor for the MyLottery Java class that initializes;
- the winner’s first name
- the winner’s last name
-the winner’s My Lottery dollar amount

In addition, create both public getter (or accessor) methods and public setter (or mutator) methods for;
- the winner’s first name
- the winner’s last name
- and the winner’s My Lottery dollar amount.
Do not put a main method in the MyLottery Java class.

The MyLotteryDemo Java class will have a main method. In the main method enable the user to enter values for;
- a winner’s first name
- a winner’s last name
- and a winner’s My Lottery dollar amount.

Input Validation: Only allow a positive dollar amount to be entered for a winner’s MyLottery amount. Validate negative or zero MyLottery dollar amounts with a loop.
Then pass the three values in the constructor for the MyLottery Java class and create at least 11 different MyLottery objects. Once the 11 different MyLottery objects have been created, store them in an ArrayList named arrayList. Finally use a loop to display the contents of each object in the ArrayList.

There should first be a heading to the display that looks like this:
Last Name, First Name MyLottery Amount

When displaying each winner’s name and MyLottery dollar amount, display each object’s full name and MyLottery dollar amount on a separate line as:
last name, first name My Lottery dollar amount

For extra credit get and display the following:
• Lowest My Lottery dollar amount
• Highest My Lottery dollar amount
• Total My Lottery dollar amount
• Average (mean) My Lottery dollar amount

Explanation / Answer

//NOTE : As per your requirements of taking input from keyboard,

// you will have to enter the 11 players yourself by keyboard.

//The complete code is ready ! It works
//Made with java 6.
//some lines of code dont fit in one line in cramster.
//please copy the code and make it neat.

//Please rate ! This took a lot of time to make !


public class MyLottery implements Comparable<MyLottery>
{
    private String firstName;
    private String lastName;
    private Float amount;
    static int totalPlayers = 0;
   
    public int compareTo(MyLottery other)
    {
    return amount.compareTo(other.getAmount());
    }
   
    //constructors
    MyLottery(){totalPlayers++;}
   
    MyLottery(String f, String l, float a)
    {
    firstName = f;
    lastName = l;
   
    if(a<=0)
    {
    System.out.println("!!! You are trying to make a player with amount <=0");
    amount = 1.0f;//set it as 1.
    }else{amount = a;}
   
    totalPlayers++;
    }
   
    //setters
    public void setFirstName(String f){firstName = f;}
    public void setLastName(String l){lastName = l;}
    public boolean setAmount(float a)
    {
    if(a > 0)
    {
    amount = a;
    return true;
    }else
    {
       System.out.println(" The amount you entered must be > 0 !");
       return false;
    }
    }
   
    //getters
    public String getFirstName(){return firstName;}
    public String getLastName(){return lastName;}
    public float getAmount(){return amount;}
   
}


____________________________________________________________________________

import java.util.*;

public class MyLotteryDemo
{
    public static void main(String[]args)
    {
   
    Scanner in = new Scanner(System.in);   
    int option = 0;
    System.out.println("");
    System.out.println("Welcome to the lottery ! ");
    ArrayList<MyLottery> players = new ArrayList<MyLottery>();
   
    try
    {
    do
    {
    System.out.println(" To add a lottery player, press 1");
    System.out.println("To stop adding players, press 2 or any other number ! ");
    option = in.nextInt();
    if(option == 1)
    {
       MyLottery my = new MyLottery();
       System.out.println(" please enter first name");
       System.out.print(">");
       String f = in.next();
       my.setFirstName(f);
      
      
       System.out.println(" please enter last name");
       System.out.print(">");
       String l = in.next();
       my.setLastName(l);
      
       float cash = 0.0f;
       
       do
       {    
        System.out.println(" Please enter dollar amount of lottery !");
        System.out.print(">");
        cash = in.nextFloat();
        my.setAmount(cash);
       }while(cash <= 0);   
      
       players.add(my); // add current player to the list !
      
    }//if
      
            
    }while(option == 1);
   
    Collections.sort(players); // sort the array list to get highest and lowest.
    int len = players.size();
    float sum = 0.0f;
   
    System.out.printf("%1$-10s %2$-11s %3$-7s", "#last name","#first name","#amount");   
    System.out.println(" ");
   
    for(int i = 0; i < len ; i++)
    {
    MyLottery p = players.get(i);
    sum = sum + p.getAmount();   
    System.out.printf("%1$-10s %2$-11s %3$-7.2f ", p.getLastName(), p.getFirstName(), p.getAmount());
    System.out.println();
    }
   
   
    System.out.println(" ");
   
    System.out.println(" Lowest My Lottery dollar amount : " + players.get(1).getAmount());
    System.out.println("Highest My Lottery dollar amount : " + players.get(len-1).getAmount());
    System.out.println("Highest My Lottery dollar amount : " + sum);
    System.out.println("Average (mean) My Lottery dollar amount : " + sum/MyLottery.totalPlayers);
   
    System.out.println(" Goodbye !");
   
    }catch(Exception e){System.out.println("It seems that you made an error, try running the program again ! " + e);}
   
    }//main


}

//Enjoy !