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

Using Java modify the following code using the Scanner class import javax.swing.

ID: 3766855 • Letter: U

Question

Using Java modify the following code using the Scanner class

import javax.swing.JOptionPane;  

/**
   This program calculates the number of soccer teams
   that a youth league may create from the number of
   available players. Input validation is demonstrated
   with while loops.
*/

public class SoccerTeams
{
   public static void main(String[] args)
   {
       final int MIN_PLAYERS = 9;   //Minimum players per team
       final int MAX_PLAYERS = 15;   //Maximum players per team
       int players;           //Number of available players
       int teamSize;           //Number of players per team
       int teams;           //Number of teams
       int leftOver;           //Number of leftover players
       String input;           //To hold the user input

       //Get the number of players per team.
       input = JOptionPane.showInputDialog("Enter the number of " + "players per team.");
      
       teamSize = Integer.parseInt(input);

       //Validate the number entered.
       while (teamSize < MIN_PLAYERS || teamSize > MAX_PLAYERS)
       {  
       input = JOptionPane.showInputDialog("The number must " + "be at least " + MIN_PLAYERS + " and no more than " + MAX_PLAYERS + ". Enter " + "the number of players.");
       teamSize = Integer.parseInt(input);
       }

       //Get the number of availabel players.
       input = JOptionPane.showInputDialog("Enter the available " + "number of players.");
  
       players = Integer.parseInt(input);

       //Validate the number entered.
       while (players < 0)
       {
           input = JOptionPane.showInputDialog("Enter 0 or " + "greater.");
           players = Integer.parseInt(input);
       }
      
       //Calculate the number of teams.
       teams = players / teamSize;

       //Calculate the number of leftover players.
       leftOver = players % teamSize;

       //Display the results.
       JOptionPane.showMessageDialog(null, "There will be " + teams + " teams with " + leftOver + " players left over. ");

       System.exit(0);
   }
}

Explanation / Answer

import javax.swing.JOptionPane;
import java.util.*;
/**
This program calculates the number of soccer teams
that a youth league may create from the number of
available players. Input validation is demonstrated
with while loops.
*/
public class SoccerTeams
{
public static void main(String[] args)
{
final int MIN_PLAYERS = 9; //Minimum players per team
final int MAX_PLAYERS = 15; //Maximum players per team
int players; //Number of available players
int teamSize; //Number of players per team
int teams; //Number of teams
int leftOver; //Number of leftover players
String input = ""; //To hold the user input
  
       Scanner in = new Scanner(System.in);
       //Get the number of players per team.
System.out.println("Enter the number of " + "players per team.");
  
teamSize = in.nextInt();
//Validate the number entered.
while (teamSize < MIN_PLAYERS || teamSize > MAX_PLAYERS)
{
System.out.println("The number must " + "be at least " + MIN_PLAYERS + " and no more than " + MAX_PLAYERS + ". Enter " + "the number of players.");
teamSize = in.nextInt();
}
//Get the number of availabel players.
System.out.println("Enter the available " + "number of players.");
  
players = in.nextInt();
//Validate the number entered.
while (players < 0)
{
System.out.println("Enter 0 or " + "greater.");
players = in.nextInt();
}
  
//Calculate the number of teams.
teams = players / teamSize;
//Calculate the number of leftover players.
leftOver = players % teamSize;
//Display the results.
JOptionPane.showMessageDialog(null, "There will be " + teams + " teams with " + leftOver + " players left over. ");
System.exit(0);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote