Designing and creating classes is a fundamental ability every C++ programmer nee
ID: 673897 • Letter: D
Question
Designing and creating classes is a fundamental ability every C++ programmer needs to develop. With this assignment, you will be getting some further practice working with many of the recent things we have been trying to learn, including the use of const, default-value arguments, namespaces and error handling. Eventually, we will be doing even more sophisticated kind of error handling involving exceptions and exception handling. All in good time...
Project 1: BaseballTeam
In recognition of the start of Major League Baseball this year (and my love of the Dodgers), please create the class BaseballTeam. A BaseballTeam represents a team in the Major Leagues with both a city and a name. For each new season, the team should track its wins and losses, whether at home or away from home.
The point behind this assignment is to:
- use the const keyword. Please note how all the getters have been marked const. This will be required to get the .printTeam operation to work.
- use default-valued arguments. Please remember the value of the defaulted arguments belongs only in a .h file, not in a .cpp file.
- track different possible errors including when the count of games won or lost is negative or exceeds 162 in total. A season is only 162 games in total.
Following the class diagrams shown below, implement the BaseballTeam class. Embed your class in a suitable test program that provides the sample dialogue shown below. You may choose to create any number of additional methods, but you are required to implement all of the public methods shown below. You are free to ignore the private parts of the class I suggest below.
BaseballTeam
BaseballTeam( string name, string city,
int season=2015,
bool nationalLeague=true );
void win( int count=1, bool home=true );
void lose( int count=1, bool home=true );
string getName( ) const;
string getCity( ) const;
int getSeason( ) const;
void setSeason( int season );
bool isNationalLeague( ) const;
bool isAmericanLeague( ) const;
void newSeason( int season );
void printTeam( ) const;
string my_Name, my_City;
bool isInNationalLeague;
int my_Season;
int my_HomeWins, my_HomeLosses;
int my_AwayWins, my_AwayLosses;
Sample Driver:
BaseballTeam la( "Dodgers",
"Los Angeles" );
la.printTeam( ); // The NL team: Los Angeles Dodgers has a 0-0 record
la.win( );
la.lose( );
la.printTeam( ); // The NL team: Los Angeles Dodgers has a 1-1 record
la.newSeason( 2014 ); The NL team: Los Angeles Dodgers - 2014 season starting
la.win( 45, true ); // 45 home wins
la.lose( 36, true ); // 36 home loss
la.win( 49, false ); // 49 away wins
la.lose( 32, false );// 32 away loss
la.printTeam( ); // The NL team: Los Angeles Dodgers has a 94-68 record
// now some errors...
la.newSeason( -100 ); // The NL team: Los Angeles Dodgers can't have a -100 season!
la.newSeason( 2015 ); // The NL team: Los Angeles Dodgers - 2015 season starting
la.win( 1000, true ); // he NL team: Los Angeles Dodgers - Season is only 162 games!
la.newSeason( 2015 ); // The NL team: Los Angeles Dodgers - 2015 season starting
la.lose( 1000, false ); // The NL team: Los Angeles Dodgers - Season is only 162 games!
la.newSeason( 2015 ); // The NL team: Los Angeles Dodgers - 2015 season starting
la.lose( -10 ); // The NL team: Los Angeles Dodgers - count can't be negative!
la.newSeason( 2015 ); // The NL team: Los Angeles Dodgers - 2015 season starting
la.win( -10 ); // The NL team: Los Angeles Dodgers - count can't be n
BaseballTeam
BaseballTeam( string name, string city,
int season=2015,
bool nationalLeague=true );
void win( int count=1, bool home=true );
void lose( int count=1, bool home=true );
string getName( ) const;
string getCity( ) const;
int getSeason( ) const;
void setSeason( int season );
bool isNationalLeague( ) const;
bool isAmericanLeague( ) const;
void newSeason( int season );
void printTeam( ) const;
string my_Name, my_City;
bool isInNationalLeague;
int my_Season;
int my_HomeWins, my_HomeLosses;
int my_AwayWins, my_AwayLosses;
Explanation / Answer
#include<iostream>
using namespace std;
class BaseballTeam{
string name,city;
int season;
bool nationalLeague;
int homeWins,homeLosses;
int awayWins,awayLosses;
public:
BaseballTeam(string name,string city){
this.name = name;
this.city = city;
season = 2015;
nationalLeague = true;
homeWins = 0;
homeLosses = 0;
awayLosses = 0;
awayWins = 0;
}
void win(int count = 1,bool home = true){
if(home){
homeWins += count;
}
else
awayWins += count;
}
void lose(int count = 1,bool home = true){
if(home){
homeLosses += count;
}
else
awayLosses += count;
}
string getName(){
return name;
}
string getCity(){
return city;
}
int getSeason(){
return season;
}
bool isNationalLeague(){
return nationalLeague;
}
bool isAmericanLeague(){
return !nationalLeague;
}
void newSeason(int season){
this.season = season;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.