Question

It’s almost baseball season in Spokane (go Indians!), and the major league season is well underway. You are writing an a...

It’s almost baseball season in Spokane (go Indians!), and the major league season is well underway. You are writing an application to manage players on a baseball team (such as the Mariners). For a team, you’ll need to track the team’s name and its players.

Define a class called BaseballPlayer and put this in a class definition file called baseballplayer.h.

· Define an enum for positions in Baseball:

enum Position { Pitcher=1, Catcher, First, Second, Third, Shortstop, LeftField, CenterField, RightField, DesignatedHitter };

· The class has the following public methods (behaviors) implemented in a file called baseballplayer.cpp:

BaseballPlayer(); //Come up with a default player

BaseballPlayer(string name, Position pos); //Initialize a player

string GetName(); //Get the player’s name

Position GetPos(); //Get the player’s position

· Think about what additional private properties the BaseballPlayer class needs to have in order to correctly implement the above behaviors.

· The default player could have any name you want, and play whatever position you choose. In my code, the player was named “Who”, and he played first base

Also define a class called BaseballTeam, and put this class definition in baseballteam.h.

· The class has the following public methods (behaviors) implemented in a file called baseballteam.cpp:

BaseballTeam(string name);

int GetPlayerCount(); //How many players are on the team?

void AddPlayer(BaseballPlayer& player); //Add a player to the team

BaseballPlayer& GetPlayerWithName(string name); //Find a player with a name. If that player doesn’t exist, return the default player

int CountPlayersAtPos(Position pos); //How many players play the given position?

BaseballPlayer& GetPlayerAtPos(Position pos); //Find a player at a position. If that playerdoesn’t exist, return the default player

· By default player, we mean return the player that is created by the default BaseballPlayer constructor

· Think about what additional private properties the BaseballPlayer and BaseballTeam classes needs to have in order to correctly implement the above behaviors.

Once your classes are designed and implemented, test it with the following program:

#include <iostream>

#include <string>

#include "baseballplayer.h"

#include "baseballteam.h"

int main() {

BaseballPlayer players[] = {

BaseballPlayer("Kyle Seager", Third), BaseballPlayer("Daniel Vogelbach", DesignatedHitter),

BaseballPlayer("Mitch Haniger", RightField), BaseballPlayer("Mallex Smith", CenterField),

BaseballPlayer("Shed Long", Second), BaseballPlayer("Dee Gordon", Second),

BaseballPlayer("Omar Narvaez", Catcher), BaseballPlayer("Domingo Santana", LeftField),

BaseballPlayer("Felix Hernandez", Pitcher), BaseballPlayer("Marco Gonzalez", Pitcher),

BaseballPlayer("Justice Sheffield", Pitcher), BaseballPlayer("Mike Leake", Pitcher)

};

BaseballTeam team("Seattle Mariners");

int count = sizeof(players)/sizeof(players[0]);

for (int i=0; i<count; i++)

team.AddPlayer(players[i]);

int errors = 0;

if (team.GetPlayerCount() != count) {

cout << "Playercount Failed" << endl;

errors++;

}

//Make sure we can find an existing player

BaseballPlayer bp = team.GetPlayerWithName("Shed Long");

if (bp.GetName() != "Shed Long") {

cout << "GetPlayerWithName failed: " << bp.GetName() << endl;

errors++;

}

BaseballPlayer defaultPlayer;

//This player does not exist, so we should get the default BaseballPlayer

bp = team.GetPlayerWithName("Does Not Exist");

if (bp.GetName() != defaultPlayer.GetName()) {

cout << "GetPlayerWithName with wrong name failed: " << bp.GetName() << endl;

errors++;

}

if (team.CountPlayersAtPos(Pitcher) != 4) {

cout << "CountPlayersAtPos failed: " << team.CountPlayersAtPos(Pitcher) << endl;

errors++;

}

//Make sure we can find an existing player

bp = team.GetPlayerAtPos(Catcher);

if (bp.GetName() != "Omar Narvaez" && bp.GetPos() != Catcher) {

cout << "GetPlayerAtPos failed: " << bp.GetName() << endl;

errors++;

}

//This player does not exist, so we should get the default BaseballPlayer

bp = team.GetPlayerAtPos(Shortstop);

if (bp.GetName() != defaultPlayer.GetName()) {

cout << "GetPlayerAtPosition with invalid position failed: " << bp.GetName() << endl;

errors++;

}

if (errors == 0) {

cout << "Tests passed!\n";

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here is code :

baseball.cpp (main function as given just have to remove #include "baseballplayer.h" because i already added it in baseballteam.h file)

#include <iostream>
#include <string>
#include "baseballteam.h"

int main() {
BaseballPlayer players[] = {
   BaseballPlayer("Kyle Seager", Third), BaseballPlayer("Daniel Vogelbach", DesignatedHitter),
   BaseballPlayer("Mitch Haniger", RightField), BaseballPlayer("Mallex Smith", CenterField),
   BaseballPlayer("Shed Long", Second), BaseballPlayer("Dee Gordon", Second),
   BaseballPlayer("Omar Narvaez", Catcher), BaseballPlayer("Domingo Santana", LeftField),
   BaseballPlayer("Felix Hernandez", Pitcher), BaseballPlayer("Marco Gonzalez", Pitcher),
   BaseballPlayer("Justice Sheffield", Pitcher), BaseballPlayer("Mike Leake", Pitcher)
};

BaseballTeam team("Seattle Mariners");

int count = sizeof(players)/sizeof(players[0]);

for (int i=0; i<count; i++){
   team.AddPlayer(players[i]);
}

int errors = 0;
if (team.GetPlayerCount() != count) {
   cout << "Playercount Failed" << endl;
   errors++;
}

//Make sure we can find an existing player

BaseballPlayer bp = team.GetPlayerWithName("Shed Long");

if (bp.GetName() != "Shed Long") {
   cout << "GetPlayerWithName failed: " << bp.GetName() << endl;
   errors++;
}

BaseballPlayer defaultPlayer;

//This player does not exist, so we should get the default BaseballPlayer

bp = team.GetPlayerWithName("Does Not Exist");

if (bp.GetName() != defaultPlayer.GetName()) {
   cout << "GetPlayerWithName with wrong name failed: " << bp.GetName() << endl;
   errors++;
}

if (team.CountPlayersAtPos(Pitcher) != 4) {
   cout << "CountPlayersAtPos failed: " << team.CountPlayersAtPos(Pitcher) << endl;
   errors++;
}

//Make sure we can find an existing player

bp = team.GetPlayerAtPos(Catcher);


if (bp.GetName() != "Omar Narvaez" && bp.GetPos() != Catcher) {
   cout << "GetPlayerAtPos failed: " << bp.GetName() << endl;
   errors++;
}

//This player does not exist, so we should get the default BaseballPlayer

bp = team.GetPlayerAtPos(Shortstop);

if (bp.GetName() != defaultPlayer.GetName()) {
   cout << "GetPlayerAtPosition with invalid position failed: " << bp.GetName() << endl;
   errors++;
}

if (errors == 0) {
   cout << "Tests passed!\n";
}
return 0;
}

code for baseballplayer.h :

#include <string>

using namespace std;

enum Position { Pitcher=1, Catcher, First, Second, Third, Shortstop, LeftField, CenterField, RightField, DesignatedHitter };

class BaseballPlayer{
   private:
       string name;
       Position pos;
   public:
       BaseballPlayer(); //Come up with a default player
       BaseballPlayer(string name, Position pos); //Initialize a player
       string GetName(); //Get the player’s name
       Position GetPos(); //Get the player’s position  
};
       

code for baseballplayer.cpp :

#include "baseballplayer.h"

BaseballPlayer::BaseballPlayer(){
   name = "Noname";
   pos = First;
}

BaseballPlayer::BaseballPlayer(string n,Position p){
   name = n;
   pos = p;
}

string BaseballPlayer::GetName(){
   return name;
}

Position BaseballPlayer::GetPos(){
   return pos;
}

code for baseballteam.h :

#include <vector>
#include <string>
#include "baseballplayer.h"

using namespace std;

class BaseballTeam{
   private:
       string name;
       int count;
       vector<BaseballPlayer> players;
  
   public:
       BaseballTeam(string name);
       int GetPlayerCount(); //How many players are on the team?
       void AddPlayer(BaseballPlayer& player); //Add a player to the team
       BaseballPlayer& GetPlayerWithName(string name); //Find a player with a name. If that player doesn’t exist, return the default player
       int CountPlayersAtPos(Position pos); //How many players play the given position?
       BaseballPlayer& GetPlayerAtPos(Position pos); //Find a player at a position. If that playerdoesn’t exist, return the default player
};

code for baseballteam.cpp :

#include "baseballteam.h"

BaseballTeam::BaseballTeam(string str){
   name = str;
   count = 0;
}

int BaseballTeam::GetPlayerCount(){
   return count;
}

void BaseballTeam::AddPlayer(BaseballPlayer & p){
   players.push_back(p);
   count++;
}

BaseballPlayer& BaseballTeam::GetPlayerWithName(string str){
   for(int i=0;i<players.size();i++){
       if(0 == (players.at(i).GetName().compare(str))){
           return players.at(i);
       }
   }
   BaseballPlayer *p = new BaseballPlayer();
   return *p;
}

int BaseballTeam::CountPlayersAtPos(Position pos){
   int c=0;
   for(int i=0;i<players.size();i++){
       if(players.at(i).GetPos() == pos){
           c++;
       }
   }
   return c;
}

BaseballPlayer& BaseballTeam::GetPlayerAtPos(Position pos){
   for(int i=0;i<players.size();i++){
       if(players.at(i).GetPos() == pos){
           return players.at(i);
       }
   }
   BaseballPlayer *p = new BaseballPlayer();
   return *p;
}

Add a comment
Know the answer?
Add Answer to:
It’s almost baseball season in Spokane (go Indians!), and the major league season is well underway. You are writing an a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

  • Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be...

    Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...

  • Don't attempt if you can't attempt fully, i will dislike and negative comments would be given...

    Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...

  • C++ Project Overview The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an...

    C++ Project Overview The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an efficient system that generates a menu. Since in Hell's Kitchen, menu changes every day, your system should easily add items to the menu. Therefore, dynamically allocated arrays would be an excellent solution. The Dish Class (Header File) You need to create a struct called...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT