C++ Pitcher Calculator I was told i needed more info some im going to copy and p
ID: 3760724 • Letter: C
Question
C++ Pitcher Calculator
I was told i needed more info some im going to copy and paste literally everything i was given for this program.
For this assignment, implement the methods for a class called Pitcher.
Pitcher class
Use the following class definition:
Data Members
The data members for the class are:
name this holds the name of a pitcher
hitsAllowed this holds the number of hits the pitcher has allowed
walksAllowed this holds the number of base on balls (walks) the pitcher has allowed
earnedRunsAllowed this holds the number of earned runs the pitcher has allowed
inningsPitched this holds the number of innings the pitcher has pitched
Constructors
This class has two constructors. The default constructor (the one that takes no arguments) creates an "empty" Pitcher object. It should initialize the name data member to an empty string - this can be done by assigning a null terminator ('') to the 0th spot of the character array or by using the strcpy function from the cstring library to copy an empty string ("") into the character array. The numeric data members should all be initialized to 0. Note:see Note 2 at the end of the write-up for an alternative way to initialize the data members.
The other constructor takes 5 arguments: an array of character constants that holds the initial name for the pitcher (it was made constant to allow for the use of string literals (ie. "Jane Doe") when using the constructor), an integer that holds the initial number of hits allowed by the pitcher, an integer that holds the initial number of walks allowed by the pitcher, an integer that holds the initial number of earned runs given up by the pitcher, an a double that holds the initial number of innings pitched by the pitcher. The passed in arguments should be used to initialize the corresponding data members of the object. No error checking is required. If the "change" methods that are described below are going to be used to initialize the numeric data members, don't forget to set the value of the data member to 0 before calling the method.
Methods
void changeName( const char newName[] )
This access method changes the name of the pitcher. It takes one argument: an array of constant characters that represents the new name for a pitcher. It returns nothing.
The passed-in argument should be used to change the name data member of the object.
void changeHits( int additionalHits )
This access method changes the number of hits allowed by the pitcher. It takes one argument: an integer that represents the change in the number of hits allowed. It returns nothing.
The passed-in argument should simply be added to the hitsAllowed data member of the object. No error checking is required.
void changeWalks( int additionalWalks )
This access method changes the number of walks allowed by the pitcher. It takes one argument: an integer that represents the change in the number of walks allowed. It returns nothing.
The passed-in argument should simply be added to the walksAllowed data member of the object. No error checking is required.
void changeEarnedRuns( int additionalRuns )
This access method changes the number of earned runs given up by the pitcher. It takes one argument: an integer that represents the change in the number of earned runs that were given up. It returns nothing.
The passed-in argument should simply be added to the earnedRunsAllowed data member of the object. No error checking is required.
void changeInnings( double additionalInnings )
This access method changes the number of innings pitched by the pitcher. It takes one argument: a double that represents the change in the number of innings that were pitched by the pitcher. It returns nothing.
The passed-in argument should simply be added to the inningsPitched data member of the object. No error checking is required.
char * getName()
This accessor method returns the current name of the pitcher. It takes no arguments and returns a pointer to a character.
Note: The char * represents a data type that can hold the address of a character. In the case of this method, the address of the name data member is being returned - or another way to view it is that the address of the first character of the pitcher's name is being returned.
The code for this method has been provided as part of the driver program that is described below.
int getHits()
This accessor method returns the number of hits allowed by the pitcher. It takes no arguments.
int getWalks()
This accessor method returns the number of walks allowed by the pitcher. It takes no arguments.
int getEarnedRuns()
This accessor method returns the number of earned runs given up by the pitcher. It takes no arguments.
double getInnings()
This accessor method returns the number of innings pitched by the pitcher. It takes no arguments.
double calcWHIP()
This method calculates and returns a pitcher's WHIP value. It takes no arguments and returns a double: the calculated value.
The WHIP value for a pitcher is the Walks and Hits per Inning Pitched. It is a measure of the rate at which a pitcher allows opposing batters to get on base. It is calculated using the following formula:
double calcERA()
This method calculates and returns a pitcher's ERA value. It takes no arguments and returns a double: the calculated value.
The ERA value for a pitcher is the average number of earned runs given up by a pitcher per 9 innings pitched. It is calculated using the following formula:
void display()
This method displays information about the pitcher. It takes no arguments and returns nothing.
The pitcher's name, number of hits allowed, number of walks allowed, number of earned runs given up, number of innings pitched, ERA, and WHIP should all be displayed in a nicely formatted manner. The number of innings pitched should have exactly 1 digit after the decimal point. The ERA and WHIP values should have exactly 2 digits after the decimal point.
For example:
Testing the Pitcher class
For this assignment, you are only required to write the various methods that are described above (with the exception of getName). A driver program that uses the various methods can be downloaded from
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign8.cpp
Make sure to complete the documentation box at the top of the file and add your methods at the end of the file.
Programming Requirements
Each method must have a documentation box like a function.
Hand in a copy of the source code from part 2 of the assignment using Blackboard.
Output
Notes
This method of initialization requires that the C++11 flag be set as a compiler option for some compilers. To do this for the Dev compiler, go to Tools/Compiler Options. Select the General tab. Click the check box next to "Add the following commands when calling the compiler:" and put -std=c++11 in the box. Select OK. Compile and run the program like normal.
Unfortunately, the Quincy compiler is too old and will not support adding the C++11 flag.
Typically, if a pitcher didn't complete a full inning, when viewing statistics the number of innings pitched will be represented with .1 or .2. The .1 indicates that the pitcher recorded 1 out of 3 possible outs before being removed from the game. The .2 indicates that the pitcher recorded 2 out of 3 possible outs before being removed from the game. However, when averages such as ERA and WHIP are calculated the .1 is changed to 1.0/3 or .3333 and the .2 is changed to 2.0/3 or .6666. For ease, we're skipping the .1 and .2 and using the .3333 and .6666 when saving the number of innings pitched. This is why you'll see .3 or .7 in our output even though it wouldn't show up that way on a baseball stats website.
C++11 adds the ability to initialize the non-static data members of a class at the time you declare them using a "brace-or-equal" syntax. This is very convenient, and can eliminate most or all of the code from your default constructor. Here are a few examples of the kind of initialization you can do in a class declaration:
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
As you already have the driver, here is the class Pitcher.h:
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class Pitcher
{
public:
Pitcher();
Pitcher( const char [], int, int, int, double );
void changeName( const char [] );
void changeHits( int );
void changeWalks( int );
void changeEarnedRuns( int );
void changeInnings( double );
char * getName();
int getHits();
int getWalks();
int getEarnedRuns();
double getInnings();
double calcWHIP();
double calcERA();
void display();
private:
char name[50];
int hitsAllowed, walksAllowed, earnedRunsAllowed;
double inningsPitched;
};
Pitcher::Pitcher()
{
strcpy(name, "");
hitsAllowed = walksAllowed = earnedRunsAllowed = inningsPitched = 0;
}
Pitcher::Pitcher(const char nm[], int hits, int walks, int earned, double innings)
{
strcpy(name, nm);
hitsAllowed = hits;
walksAllowed = walks;
earnedRunsAllowed = earned;
inningsPitched = innings;
}
void Pitcher::changeName(const char newName[])
{
strcpy(name, newName);
}
void Pitcher::changeHits(int additionalHits)
{
hitsAllowed += additionalHits;
}
void Pitcher::changeWalks(int additionalWalks)
{
walksAllowed += additionalWalks;
}
void Pitcher::changeEarnedRuns(int additionalRuns)
{
earnedRunsAllowed += additionalRuns;
}
void Pitcher::changeInnings(double additionalInnings)
{
inningsPitched += additionalInnings;
}
/*char *Pitcher::getName()
{
return name;
}*/
int Pitcher::getHits()
{
return hitsAllowed;
}
int Pitcher::getWalks()
{
return walksAllowed;
}
int Pitcher::getEarnedRuns()
{
return earnedRunsAllowed;
}
double Pitcher::getInnings()
{
return inningsPitched;
}
double Pitcher::calcWHIP()
{
return (double)(hitsAllowed + walksAllowed) / inningsPitched;
}
double Pitcher::calcERA()
{
return (earnedRunsAllowed * 9.0) / inningsPitched;
}
void Pitcher::display()
{
double era = (double)(earnedRunsAllowed * 9.0) / inningsPitched;
double whip = (double)(hitsAllowed + walksAllowed) / inningsPitched;;
cout<<name<<endl<<"Hits: "<<hitsAllowed<<" Walks: "<<walksAllowed;
cout<<" Runs: "<<earnedRunsAllowed<<" Innings: "<<inningsPitched<<endl;
cout<<"ERA: "<<fixed<<setprecision(2)<<era;
cout<<" WHIP: "<<fixed<<setprecision(2)<<whip<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.