Question

C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class...

C++

Please Provide .cpp

Overview

For this assignment, implement and use the methods for a class called Player that represents a player in the National Hockey League.

Player class

Use the following class definition:

class Player
{
public:
  Player();
  Player( const char [], int, int, int );
    
  void printPlayer();

  void setName( const char [] );
  void setNumber( int );
  
  void changeGoals( int );
  void changeAssists( int );

  int getNumber();
  int getGoals();
  int getAssists();

private:
  char name[50];
  int number;
  int goals;
  int assists;
};

Data Members

The data members for the class are:

  • name this holds the player's name

  • number this holds the player's number

  • goals this holds the number of goals that the player has scored

  • assists this holds the number of assists that the player has earned

Constructors

This class has two constructors. The default constructor (the one that takes no arguments) should initialize the name to the null string (this can be done by assigning '\0' to the 0th spot of the character array or by using strcpy to copy an empty string ("") into the character array) and the integer data members to 0.

The other constructor for the class should initialize the data members using the passed in arguments. It takes 4 arguments: a character array with a player name, and integer that holds the player's number, an integer that holds the number of goals that the player has scored, and an integer that holds the number of assists the player has earned. The numeric values can be initialized with simple assignment statements or by calling the set/change methods that are described below. However, if the change methods are going to be used, don't forget to set the individual data members to 0 before calling the appropriate change method. Use the setName method or a simple call to strcpy function to initialize the name.

Methods

void printPlayer()

This method displays the player information and the number of points. It takes no arguments and returns nothing. The number of points is calculated as follows:

Points = number of goals + number of assists

The information should be displayed as follows:

Steven Stamkos     #91
Goals: 1       Assists: 1        Points: 2

void setName( const char playerName[] )

This method sets/changes a player's name. It takes one argument: an array of constant characters that represents the player's name. It returns nothing.

Use the strcpy function to assign the passed in argument to the name data member. No error checking is required.

void setNumber( int newNumber )

This method sets/changes a player's number. It takes one argument: an integer that represents the player's number. It returns nothing.

Use a simple assignment statement to assign the passed in argument to the number data member. No error checking is required.

void changeGoals( int goalsScored )

This method changes a player's number of goals. It takes one argument: an integer that represents the number of goals that have recently been scored by the player. It returns nothing.

If the passed in number of goals that have recently been scored is less than 0, display an error message and do not make any changes to the goals data member. If the passed in number of goals that have recently been scored is greater than or equal to 0, increment the goals data member by the passed in number of goals that have been scored.

void changeAssists( int assistsEarned )

This method changes a player's number of assists. It takes one argument: an integer that represents the number of assists that have recently been earned by the player. It returns nothing.

If the passed in number of assists that have recently been earned is less than 0, display an error message and do not make any changes to the assists data member. If the passed in number of assists that have recently been earned is greater than or equal to 0, increment the assists data member by the passed in number of assists that have been earned.

int getNumber()

This method returns a player's number. It takes no arguments.

int getGoals()

This method returns a player's number of goals scored. It takes no arguments.

int getAssists()

This method returns a player's number of assists earned. It takes no arguments.

main()

In main(), create 6 Player objects. They should contain the values:

  • The first player should have your name, the number 66, 5 goals scored, and 3 assists. Note: if you're pair programming, set the name to both you and your partner: "Jane Doe/John Doe".

  • The second player should be created using the default constructor (the one that doesn't take any arguments)

  • The third player should have the name "Jonathan Toews", the number 19, 35 goals scored, and 46 assists earned

  • The fourth player should have the name "Patrick Kane", the number 88, 44 goals scored, and 66 assists earned

  • The fifth player should have the name "Alex DeBrincat", the number 12, 41 goals scored, and 35 assists earned

  • The sixth player should have the name "Dylan Strome", the number 17, 20 goals scored, and 37 assists earned

The rest of main() will include using the various methods on each of the 6 Player objects. Display a label similar to "The first Player object" before anything is outputted for each of the objects.

For the first player, display the player information.

For the second player, display the player information, set the player name to "Duncan Keith", set the number to 2, change the number of goals scored to 6, and change the number of assists to 34, and then display the player information once again.

For the third player, display the player's information, using the changeGoals method, try to change the player's number of goals by -8, using the changeAssists method, try to change the player's number of assists by -2, and then display the player information once again.

For the fourth player, display the player's information, using changeAssists, increase the number of assists by 4, and then display the player information once again.

For the fifth player, display only the player's number and the number of goals scored, and then display all the player information.

For the sixth player, display only the player's number and the number of assists earned, and then display all the player information.

Programming Notes

  1. Each method must have a documentation box like a function.

  2. Hand in a copy of your source code using Blackboard.

Output

Note 1: The information for the first Player object will have your name.

The first Player object
Ted E. Bear    #66
Goals:     5  Assists:    3  Points:     8


The second Player object
    #0
Goals:     0  Assists:    0  Points:     0

Duncan Keith    #2
Goals:     6  Assists:   34  Points:    40


The third Player object
Jonathan Toews    #19
Goals:    35  Assists:   46  Points:    81

Error in changeGoals. The # of goals scored cannot be negative
Error in changeAssists. The # of assists earned cannot be negative

Jonathan Toews    #19
Goals:    35  Assists:   46  Points:    81


The fourth Player object
Patrick Kane    #88
Goals:    44  Assists:   66  Points:   110

Patrick Kane    #88
Goals:    44  Assists:   70  Points:   114


The fifth Player object

Player number 12 has scored 41 goals

Alex DeBrincat    #12
Goals:    41  Assists:   35  Points:    76


The sixth Player object

Player number 17 has earned 37 assists

Dylan Strome    #17
Goals:    20  Assists:   37  Points:    57
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// change your name in below line . Replace "My name with your name. I have marked this line in bold in main method in code

//  Player firstPlayer ("My Name",66,5,3);

#include <cstring>
#include <iostream>
using namespace std;

class Player
{
private:
char name[50];
int number;
int goals;
int assists;   
  
public:

/**
* Default Constructor
*
* @param no parameters
* @return does not return anything
*/
Player(){
strcpy(name,"");
number = 0;
goals = 0;
assists = 0;
}
  
/**
* Parameterized Constructor
*
* @param playername , playernumber , playergoals, playerassists
* @return does not return anything
*/
Player( const char playername [], int playernumber, int playergoals, int playerassists ){
strcpy(name,playername);
number = playernumber;
goals = playergoals;
assists = playerassists;
}
  
  
/**
* print player's information
*
* @param no parameters
* @return does not return anything
*/
void printPlayer(){
cout << name <<" #"<< number << endl;
cout<< "Goals : "<< goals << " Assits : "<< assists <<" Points : "<< goals+assists << endl;
}
  

/**
* set player's new name
*
* @param playername - player's new name
* @return does not return anything
*/   
void setName( const char playername[] ){
strcpy(name,playername);
  
}
  
/**
* set player's new number
*
* @param newNumber - player's new number
* @return does not return anything
*/
void setNumber( int newNumber){
number = newNumber;
}
  
/**
* change player's goals. Increase the goals by goalsScored if greater than zero else throw error
*
* @param goalsScored - number of goals scored
* @return does not return anything
*/
void changeGoals( int goalsScored){
if(goalsScored >= 0)
goals += goalsScored;
else
cout<< endl <<"Error in changeGoals. The # of goals scored cannot be negative ";
}
  
/**
* change player's assists. Increase the assists by assistsEarned if greater than zero else throw error
*
* @param assistsEarned - number of assists earned
* @return does not return anything
*/
void changeAssists( int assistsEarned){
if(assistsEarned >= 0)
assists += assistsEarned;
else
cout<< endl <<"Error in changeAssists. The # of assists earned cannot be negative ";
}

/**
* get current player's number
*
* @param no parameters
* @return number - current player's number
*/
int getNumber(){
return number;
}
  
/**
* get current player's goals
*
* @param no parameters
* @return goals - current player's goals
*/
int getGoals(){
return goals;
}
  
  
/**
* get current player's assists
*
* @param no parameters
* @return assists - current player's assists
*/
int getAssists(){
return assists;
}

};

int main(){
  
  
// creating first player object.
Player firstPlayer ("My Name",66,5,3);
  
cout << "The first Player object" << endl;
firstPlayer.printPlayer();
  
// creating second player object.
Player secondPlayer;
  
cout << endl << "The second Player object" << endl;
secondPlayer.printPlayer();
  
secondPlayer.setName("Duncan Keith");
secondPlayer.setNumber(2);
secondPlayer.changeGoals(6);
secondPlayer.changeAssists(34);
  
cout << endl;
secondPlayer.printPlayer();
  
// creating third player object.
Player thirdPlayer ("Jonathan Toews",19,35,46);
  
cout << endl << "The third Player object" << endl;
thirdPlayer.printPlayer();
  
thirdPlayer.changeGoals(-8);
thirdPlayer.changeAssists(-2);
  
cout << endl << endl;
thirdPlayer.printPlayer();
  
  
// creating fourth player object.
Player fourthPlayer ("Patrick Kane",88,44,66 );
  
cout << endl << "The fourth Player object" << endl;
fourthPlayer.printPlayer();
  
fourthPlayer.changeAssists(4);
  
cout << endl << "The fourth Player object" << endl;
fourthPlayer.printPlayer();
  

// creating fifth player object.
Player fifthPlayer ("Alex DeBrincat",12,41,35 );
cout << endl << "The fifth Player object" << endl;
cout<< endl <<"Player number " << fifthPlayer.getNumber() << " has scored " << fifthPlayer.getGoals() << " goals";
cout<< endl << endl;
fifthPlayer.printPlayer();

  
// creating sixth player object.
Player sixthPlayer ( "Dylan Strome",17,20,37);
cout << endl << "The sixth Player object" << endl;
cout<<endl <<"Player number " << sixthPlayer.getNumber() << " has earned " << sixthPlayer.getAssists() << " assists";
cout<< endl << endl;
sixthPlayer.printPlayer();
  
  
  
return 0;
}

// Output

Add a comment
Know the answer?
Add Answer to:
C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class...
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
  • C++ implement and use the methods for a class called Seller that represents information about a...

    C++ implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; }; Data Members The...

  • implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...

    implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...

  • Need done in C++ Can not get my code to properly display output. Instructions below. The...

    Need done in C++ Can not get my code to properly display output. Instructions below. The code compiles but output is very off. Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a team. When the program runs, it should ask the user...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • 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...

  • programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate...

    programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate a game of Roulette. Roulette is a casino game of chance where a player may choose to place bets on either a single number, the colors red or black, or whether a number is even or odd. (Note: bets may also be placed on a range of numbers, but we will not cover that situation in this program.) A winning number and color is...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • Part (A) Note: This class must be created in a separate cpp file Create a class...

    Part (A) Note: This class must be created in a separate cpp file Create a class Employee with the following attributes and operations: Attributes (Data members): i. An array to store the employee name. The length of this array is 256 bytes. ii. An array to store the company name. The length of this array is 256 bytes. iii. An array to store the department name. The length of this array is 256 bytes. iv. An unsigned integer to store...

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