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

Create A Menu Using Both Baseball Class And Team Info Class Asking User to Choos

ID: 659878 • 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

To all player stats..the class you given methods must embedded into player class...such that we can maintain into arrays...we can pass...but as of now no array was declared...I didnt want to change files..so i keep them unchanged.

import java.util.Scanner;
public class Menu{
   public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
       //showing menu here
       while(1){
           System.out.println("1.Add player 2.Update Player 3. show player stats 4.show all player stats");
           int choice = sc.nextInt();
       //this is for add new player  
           if(choice==1){
               System.out.println(" Enter name");
               String name = sc.nextLine();
               System.out.println(" Enter inHits");
               int inHits = sc.nextInt();
               System.out.println(" Enter inAtBats");
               int inAtBats = sc.nextInt();
               BaseballPlayer bs = new BaseballPlayer(name,inHits,inAtBats);
           }
       //this if for update player  
           else if(choice==2){
               System.out.println(" Enter inHits");
               int inHits = sc.nextInt();
               System.out.println(" Enter inAtBats");
               int inAtBats = sc.nextInt();
               bs.updateStats(inHits,inAtBats);
           }
       //this is to get player statastics  
           else if(choice==3){
               System.out.println(bs.toString());
           }
       //this is for exit  
           else{
               exit(1);
           }
       }
      
   }
}