Create A Menu Using Both Baseball Class And Team Info Class Asking User to Choos
ID: 659963 • Letter: C
Question
Create A Menu Using Both Baseball Class And Team Info Class Asking User to Choose From
1.Add Player
2.Update Player
3.Show Player Stats
4.Show All Player Stats
5.Quit
When i type 1 it should make me create a player type in player first and last name, at bats , hits. When i type 2 it should let me edit player stats . When i type 3 it should display player. When i type 4 display all player stats. When i type 5 it should quit the program
PLAYER CLASS
/**********************************************************************
*Program Name :
*Author :
*Due Date :
*Course/Section :
*Program Description: Write a program to create an array based on a
*
*
*Constructor: initializes the instance data
*updateStats: adds hits and at bats to the totals in the instance data
*toString : formats the instance data for display
*********************************************************************/
import java.text.*;
public class BaseballPlayer
{
//class constants
//class variables
private String name; //players first and last name
private int hits; //players hit total
private int atBats; //number of attempts
private float avg; //players batting average
/**********************************************************************
*Method Name : BaseballPlayer(constructor)
*Author :
*Due Date :
*Course/Section :
*Program Description: This constructor will intialize the instance data
* to values passed in via the parameter list.
*
*BEGIN BaseballPlayer
* initialize all instace data
*END Baseball Player
***********************************************************************/
public BaseballPlayer(String inName, int inHits, int inAtBats)
{
//local constants
//local variables
/******************************************************/
//initialize the instance data
name = inName;
atBats = inBats;
hits = inHits;
avg = (float)hits / atBats;
}//end constructor
/**********************************************************************
*Method Name : Update Stats
*Author :
*Due Date :
*Course/Section :
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN update stats(hits, at bats)
* add hits to total hits in the class data
* add at bats to total at bats in the class data
* recalculate the batting average
*END Baseball Player
***********************************************************************/
public void updateStats(int inAtBats, int inHits)
{
//local constants
//local variables
/*******************************************************************/
//update player at bats
atBats += inAtBats;
//update player hits
hits += inHits;
//recalculate the batting average
avg = (float)hits / atBats;
}//end updateStats
/**********************************************************************
*Method Name : toString
*Author :
*Due Date :
*Course/Section :
*Program Description: This method will format the instance data in a
* manner fit for display
*
*BEGIN BaseballPlayer
* IF(player name is too long)
* make it 10 characters long
* ELSE
* padd the name to be 10 characters long
* END IF
* Create output string
* return output string
*
*END Baseball Player
***********************************************************************/
public String toString()
{
//local constants
final int NAME_LENGTH = 10;
//local variables
String fmtName; //name from instance data formatted to NAME_LENGTH
String output; //formatted instance data
DecimalFormat fmt = new DecimalFormat("0.000");
String avgFmt = fmt.format(avg);
/*******************************************************************/
//IF(player name is too long)
if (name.length() > NAME_LENGTH)
//make it 10 characters long
fmtName = name.substring(0,10);
//ELSE name is not too long
else
//padd the name to be 10 characters long
fmtName = Util.setRight(NAME_LENGTH, name);
//END IF
//Create output string
output = Util.setLeft(29, "Name : ") + fmtName + " " +
Util.setLeft(29, "At Bats:: ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
Util.setLeft(29, "Hits : ") + Util.setRight(NAME_LENGTH + 5, "" + hits) + " " +
Util.setLeft(29, "Avg : ") + Util.setRight(NAME_LENGTH + 5, "" + avgFmt);
//return output string
return output;
}//end toString
}//end BaseballPlayer class
Team Info Class
//Create a class TeamInfo
public class TeamInfo
{
// Declare variables
private int[] atBats;
private int[] hits;
int numberOfPlayers;
// constructor
public TeamInfo(int[] startHits, int[] startAtBats, int num) {
atBats = startAtBats;
hits = startHits;
numberOfPlayers = num;
}
//add player method
public int getNumberOfPlayers()
{
return numberOfPlayers;
}
// accessor for atBats
// return aray with number of at-bats per player
public int[] getAtBats()
{
int[] temp = new int[numberOfPlayers];
for (int i = 0; i < atBats.length; i++)
{
temp[i] = atBats[i];
}
return temp;
}
// accessor for hits
// return totals hits
public int[] getHits()
{
int[] temp = new int[numberOfPlayers];
for (int i = 0; i < hits.length; i++)
{
temp[i] = hits[i];
}
return temp;
}
// mutator for atBats
//to max player
public void setAtBats(int[] newAtBats)
{
atBats = new int[newAtBats.length];
for (int i = 0; i < atBats.length; i++)
atBats[i] = newAtBats[i];
}
// mutator for hits
// @ param newHits number of times each player batted
public void setHits(int[] newHits)
{
hits = new int[newHits.length];
}
// toString
// return number of players, at bats, and hits
public String toString() {
String returnString = " ";
for (int i = 0; i < hits.length; i++)
returnString = returnString + (i++) + " " + hits[i] + " "
+ atBats[i] + " ";
return returnString;
}
// return true if the numberOfPlayers, atBats, and hits in this object
// are equal to those in parameter object; false otherwise
public boolean equals(Object o) {
if (!(o instanceof BaseballClient))
return false;
else {
BaseballClient b = (BaseballClient) o;
if (numberOfPlayers != b.getNumberOfPlayers())
return false;
}
return true;
}
// batting averages
// @return array of batting averages for each player
public double[] battingAverages() {
double[] temp = new double[numberOfPlayers];
for (int i = 0; i < numberOfPlayers; i++) {
if (atBats[i] == 0)
temp[i] = 0;
else
temp[i] = (double) (hits[i] / atBats[i]);
}
return temp;
}
// totalHits
// @ return total number of hits for the team
public int totalHits()
{
int total = 0;
for (int i = 0; i < numberOfPlayers; i++) {
total = total + hits[i];
}
return total;
}
// good players
// @ return number of players with batting
public int playerArray() {
double average[] = battingAverages();
int count = 0;
for (int i = 0; i < numberOfPlayers; i++) {
if (average[i] > 0.300) {
count++;
}
}
return count;
}
private static int maxPlayer(int[] array, int size) {
int index = 0;
for (int i = 0; i < size; i++) {
if (array[i] > array[index])
index = i;
}
return index;
}
}
Explanation / Answer
import java.util.*;
import java.io.*;
class menu {
public static void main (String [] args) throws IOException
{
PlayerRip.PlayerInfo obj5 = new PlayerRip.PlayerInfo();
ArrayList<Player> obj6 = new Arraylist<player>();
MainMenu(obj5, obj6);
}
public static void MainMenu(PlayerRip P, ArrayList<Player> S) throws IOException
{
Scanner Keyboard = new Scanner(System.in);
int response = 0;
int response2 = 0;
ArrayList<Player> obj1 = new ArrayList<Player>();
obj1.add(new player("Agostini","Aldo","Pitcher", 170,20,72,12,6,1,1,0));
do
{
System.out.println("1.Create a player first and last name, bats,hits");
System.out.println("5. Quit the program");
response = keyboard.nextInt();
Switch (response)
{
case 1:
{
openFile(obj1);
do
{
System.out.println("2.Edit player stats");
System.out.println("3.Display player");
System.out.println("4.Display all player stats");
System.out.println("5. Quit program");
response2 = Keyboard.nextInt();
swithch (response2)
{
case 2:
{
edit player stats(obj1);
break;
}
case 3:
{
Display player(obj1);
break;
}
case 4:
{
Display all player stats(obj1);
break;
}
case 5:
{
system.exit(0);
break;
}
}
} while (response != 1 | | response ! = 5 ); // End of switch statement
}
class Player {
private String firstName;
private String lastName;
private float battingAverage;
public Player(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.battingAverage = battingAverage;
}
public float getBattingAverage() {
return battingAverage;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
public static void Add player(ArrayList<Player> S) {
System.out.printf("new Player("Gil",0.273f));
for (int index = 0; index <S.size(); index++)
S.get(index).Add player();
}
}
public static void Update player (ArrayList<Player> S)
{
int newUpdate = 0;
Scanner Keyboard = new Scanner(System.in);
System.out.println("Select a player to update");
do
{
for (int i=0; i < S.size(); i++)
{
System.out.println(i+ "." +S.get(i).getName());
System.out.println(" ");
}
public static void Player stats(ArrayList<Player> S) {
Player stats;
while(i < S.size()&&stats)
{
stats;
for (int j = 0; j < (S.size()-1)-i;j++)
System.out.println("Player stats");
}
}
i++;}
}
public static void All player stats(ArrayList<Player> S) {
All player stats;
while(i < S.size()&&stats)
{
stats;
for (int j = 0; j < (S.size()-1)-i;j++)
System.out.println("All player stats");
}
}
i++;}
}
public static void Quit program(ArrayList<Player> S) {
Scanner inputInfo = new Scanner (System.in);
System.out.println("Quit the program") ;
}
}
}