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

Players Class: Instance variables: First name of player, Last name of player, Ba

ID: 3879951 • Letter: P

Question

Players Class:

Instance variables: First name of player, Last name of player, Batting average represented by a decimal number from 0 to 1.

Methods:

Default constructor

Constructor that accepts singles, doubles, triples, homeruns and atBats and then calls the calculateAverage

Getters, setters and toString- which displays the player’s name and batting average in a single line:

John Smith .462

Note: Batting average should be displayed with three decimal places.

calculateAverage- This method will accept the number of singles, doubles, triples and homeruns, as well as the number of atBats and then changes the result to set the player’s batting average.

setAverage- This method receives a player’s average already calculated and sets the value.

Team Class:

Instance variables: an array of Players, team name

Methods:

Team batting average- this method will collect the batting average of each player and return the team average.

Getters and setters

outputTeam- this method should output the team name, team average and then in a loop, call the toString of the Player Class

Red Sox Team Average: .486

Player                          Batting Average

John Smith                   .528

Adam Sandler               .479

….

Driver Class:

Create four instances of the Team Class.

Read in a file of player names and number of singles, doubles, triples, homeruns and atBats, as well as the team name. The file should contain at least 40 players, representing four different teams. The team order of the players should be scrambled. Therefore, each record read should create a new instance of the player class, determine batting average, store that average into the player instance variable, determine which team the player is on, and then add the player to the array of the correct team.

Generate a menu that offers the following options:

1- Display team roster

2- Display league roster

Explanation / Answer

here is your program :==========

#include<iostream>
#include<fstream>

using namespace std;

class Player{
string fname;
string lname;
double avarage;

public:
  Player(){
  }
  void operator=(const Player &other){
   fname = other.fname;
   lname = other.lname;
   avarage = other.avarage;
  }
  Player(double sing,double doub,double trip,double home,int atBats){
   calculateAvarage(sing,doub,trip,home,atBats);
  }
  void calculateAvarage(double sing,double doub,double trip,double home,int atBats){
   double avr = (sing+2*doub+3*trip+home)/atBats;
   setAvarage(avr);
  }
  void setAvarage(double avr){
  // if(avr < 1 && avr > 0){
    avarage = avr;
  // }
  }
  
  void setFirstName(string fn){
   fname = fn;
  }
  void setLastName(string ln){
   lname = ln;
  }
  
  double getAvarage(){
   return avarage;
  }
  string getName(){
   return fname+" "+lname;
  }
  string toString(){
   string s =" ";
   return getName()+s;
  }
};

class Team{
Player *player;
string name;
int size;
int cSize;

public:
  Team(){
   
  }
  void operator=(const Team &other){
   size = other.size;
   cSize = other.cSize;
   name = other.name;
   for(int i = 0;i<cSize;i++){
    player[i] = other.player[i];
   }
  }
  Team(int s){
   size = s;
   player = new Player[s];
   cSize = 0;
  }
  void setSize(int s){
   size = s;
   player = new Player[s];
   cSize = 0;
  }
  void addPlayer(Player &p){
   if(cSize < size){
    player[cSize] = p;
    cSize++;
   }
  }
  void setName(string name){
   this->name = name;
  }
  string getName(){
   return name;
  }
  Player getPlayer(int i){
   return player[i];
  }
  double calculateAvarage(){
   double res = 0;
   for(int i = 0;i<cSize;i++){
    res += player[i].getAvarage();
   }
   
   return res/cSize;
  }
  void outputTeam(){
   cout<<" "<<name<<" Team Avarage : ";
   printf("%.3f ",calculateAvarage());
   cout<<"Player Batting Avarage ";
   for(int i = 0;i<cSize;i++){
    cout<<player[i].toString();
    printf("%.3f ",player[i].getAvarage());
   }
  }
};

void read(ifstream &f,Team &team,int size){
string pfn,pln;
double s,d,t,h;
int at;
Player p;
for(int i = 0;i<size;i++){
  f>>pfn>>pln>>s>>d>>t>>h>>at;
  p.calculateAvarage(s,d,t,h,at);
  p.setFirstName(pfn);
  p.setLastName(pln);
  team.addPlayer(p);
}
}

int main(){
Team t[4];
ifstream f;
int i;
string name;
int si;
f.open("team.txt");
if(f.is_open()){
  while(!f.eof()){
   if(i >= 1){
    break;
   }
   f>>name;
   f>>si;
   t[i].setSize(si);
   t[i].setName(name);
   read(f,t[i],si);
   t[i].outputTeam();
   i++;
   cout<<endl;
  }
  
  f.close();
}else{
  
}
}