Question

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 an array of objects as a class data member, and introduces the concept of object serialization or "binary I/O".

Set Up

  1. As with Assignment 1, you should create a subdirectory to hold your files for Assignment 2.

  2. In that directory, make a symbolic link to the data file for this part of the assignment:

       ln -s /home/turing/t90kjm1/CS241/Data/Fall2019/Assign2/teamData
    
  3. In this assignment, you will be creating several source code and header files, as described below. You can create each of these files separately using the nano editor, just as you did on Assignment 1.

Program

For this assignment, you will need to write three source code files as well as two header files. Each of these files is relatively short, but many inexperienced programmers are overwhelmed by the idea of writing a program as multiple files. "Where do I start?!!" is a common refrain. This assignment sheet attempts to walk you through the steps of writing a multi-file program.

The steps outlined below should not be thought of as a purely linear process, but rather an iterative one - For example, work a little on Step 1, then a little on Step 2, then test what you've written (Step 3).

Step 1: Write the Player class declaration

The Player class represents information about a baseball player. The code for the Player class will be placed in two separate files, which is the norm for non-template C++ classes.

The header file for a class contains the class declaration, including declarations of any data members and prototypes for the methods of the class. The name of the header file should be of the form ClassName.h (for example, Player.h for the header file of the Player class).

A skeleton for the Player.h file is given below. As shown, a header file should begin and end with header guards to prevent it from accidentally being #included more than once in the same source code file (which would produce duplicate symbol definition errors). The symbol name used in the header guards can be any valid C++ name that is not already in use in your program or the C/C++ libraries. Using a name of the format CLASSNAME_H (like SELLER_H in the code below) is recommended to avoid naming conflicts.

#ifndef PLAYER_H
#define PLAYER_H

//***************************************************************
//
// Player.h
// CSCI 241 Assignment 2
//
// Created by name(s) and z-ID(s)
//
//***************************************************************

class Player
{
private:
 
    // Data members for the Player class go here.

public:

    // Method prototypes for the Player class.

    Player();
    Player(const char*, const char*, const char*, char, char, int, int);
    const char* getName() const;
    double computeAverage() const;
    void incrementHits(int);
    void incrementAtBats(int);
    void print() const;
};

#endif

Data Members

The Player class should have the following seven private data members:

  • A jersey number (a character array with room for 2 characters plus the null character
  • A name (a character array with room for 30 characters plus the null character)
  • A position (a character array with room for 2 characters plus the null character
  • Batting handedness (a single character)
  • Throwing handedness (a single character)
  • Number of hits (an integer)
  • Number of at bats (an integer)

Note: Make sure that you code your data members in THE EXACT ORDER LISTED ABOVE and with THE EXACT SAME DATA TYPES. For example, if you make the name array 30 characters long instead of 31, your final program will not work correctly.

C++11 Initialization Option for Data Members

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 initializations you can do in a class declaration:

class Foo
{
private:

    // Data members
    int x = 0;                                  // Initialize x to 0
    double y = 9.9;                             // Initialize y to 9.9
    char text[21]{};                            // Initialize text to an empty string
    char name[11]{'J', 'o', 'h', 'n', '\0'};    // Initialize name to "John"
    string s{"Hello"};                          // Initialize s to "Hello"

    etc.
};

Feel free to use this option if you like.

Method Prototypes

The Player class declaration contains public prototypes for all of the methods in the Player.cpp source code file described in Step 2 below.

Step 2: Write the Player class implementation

The source code file for a class contains the method definitions for the class. The name of the source code file should be of the form ClassName.cpp (for example, Player.cpp for the source code file of the Player class).

The Player class implementation should (eventually) contain definitions for all of the methods described below. Make sure to #include "Player.h" at the top of this file.

  • Player::Player()

    Parameters: None.

    Returns: Constructors do not have a return data type.

    A "default" constructor in C++ is a constructor that has no parameters. If no parameters are supplied when a Player object is created, this constructor will be called to set the object's data members to valid default values.

    This method should set the number, name, and position data members to "null" or "empty" strings. This can be done by copying a null string literal ("") into the character array using strcpy() or by setting element 0 of the array to a null character ('\0'). The two handedness data members should be set to 'R'). The hits and at bats data members should be set to 0.

    (If you're working in C++11 and you initialized the data members at declaration as described above under C++11 Initialization Option for Data Members, this method's body can be empty. You still need to code the method definition, even though it won't actually do anything.)

  • Player::Player(const char* newNumber, const char* newName, const char* newPosition, char newBats, char newThrows, int newHits, int newAtBats)

    Parameters: 1) a C string that will not be changed by this method that contains a new jersey number, 2) a C string that will not be changed by this method that contains a new name, 3) a C string that will not be changed by this method that contains a new position, 4) a character that contains a new batting handedness, 5) a character that contains a new throwing handedness, 6) an integer that contains a new number of hits, and 7) an integer that contains a new number of at bats. DO NOT GIVE THESE PARAMETERS THE SAME NAMES AS YOUR DATA MEMBERS..

    Returns: Constructors do not have a return data type.

    Use strcpy() to copy the new number and new name parameters into the object's number and name data members. Assign the new hits and new at bats parameters to their corresponding data members.

  • const char* Player::getName() const

    Parameters: None.

    Returns: The name data member.

    This method does not modify the object for which it is called and is therefore declared to be const.

    This accessor method should return the name data member. In C++, the usual return data type specified when you are returning a C string is const char* or "pointer to a character constant" (since returning an array's name will convert the name into a pointer to the first element of the array, which in this case is data type char; the "const" part means that the calling function will not be able to use the pointer to modify the data that it points to).

  • double Player::computeAverage() const

    Parameters: None.

    Returns: The player's batting average.

    This method does not modify the object for which it is called and is therefore declared to be const.

    This method should compute and return the player's batting average. This is simply the player's number of hits divided by their number of at bats. If the player has never batted, that formula will cause a "division by zero" runtime error; in that case, the method should return 0.0 for the player's batting average.

  • void Player::incrementHits(int newHits)

    Parameters: An integer that contains a new number of hits to be added to the player's hits.

    Returns: Nothing.

    This mutator method should add the new hits parameter to the number of hits data member.

  • void Player::incrementAtBats(int newAtBats)

    Parameters: An integer that contains a new number of at bats to be added to the player's at bats.

    Returns: Nothing.

    This mutator method should add the new at bats parameter to the number of at bats data member.

  • void Player::print() const

    Parameters: None.

    Returns: Nothing.

    This method does not modify the object for which it is called and is therefore declared to be const.

    This method should print the seven data members of the object on the console using cout. Use setw() to line the printed values up in columns. Call the computeAverage() method to obtain the player's batting average. The batting average should be printed using fixed-point notation with three places after the decimal point. Consult the sample output for an example of how to space and justify the output from this method.

Step 3: Test and debug the Player class

As you write your declaration and implementation of the Player class, you should begin testing the code you've written. Create a basic main program called testPlayer.cpp that tests your class. This is not the final version of main() that you will eventually submit. In fact, you'll end up discarding it entirely by the time you're done with the assignment. An example test program is given below.

You do not have to have written all of the methods for the Player class before you begin testing it. Simply comment out the parts of your test program that call methods you haven't written yet. Write one method definition, add its prototype to the class declaration, uncomment the corresponding test code in your test program, and then compile and link your program. If you get syntax errors, fix them before you attempt to write additional code. A larger amount of code that does not compile is not useful - it just makes debugging harder! The goal here is to constantly maintain a working program.

//***************************************************************
//
// testPlayer.cpp
// CSCI 241 Assignment 2
//
// Created by name(s) and z-ID(s)
//
//***************************************************************

#include <iostream>
#include "Player.h"

using std::cout;
using std::endl;

int main()
   {
   // Test default constructor.
   Player p1;

   // Test alternate constructor.
   Player p2("12", "Van Damme, Cletus", "P", 'R', 'L', 0, 0);

   // Test print() method and whether constructors
   // properly initialized objects.
   cout << "Printing first player\n\n";
   p1.print();
   cout << endl << endl;

   cout << "Printing second player\n\n";
   p2.print();
   cout << endl << endl;

   // Test accessor method.
   cout << p2.getName() << endl;

   p2.incrementHits(2);
   p2.incrementAtBats(9);
   
   // Test print() again with updated batting average.
   p2.print();

   return 0;
   }

Once you've written testPlayer.cpp, you can compile and link your program to test your Player class using the following command:

   g++ -Wall -Werror -std=c++11 -o testPlayer testPlayer.cpp Player.cpp

If everything compiles and links successfully, you can run the resulting executable program by typing:

   ./testPlayer

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

//Player.h

#ifndef PLAYER_H

#define PLAYER_H

//***************************************************************

//

// Player.h

// CSCI 241 Assignment 2

//

// Created by name(s) and z-ID(s)

//

//***************************************************************

class Player

{

private:

    // Data members for the Player class go here.

       char number[3];

       char name[31];

       char position[3];

       char bats ;

       char throws ;

       int hits;

       int atBats;

public:

    // Method prototypes for the Player class.

    Player();

    Player(const char*, const char*, const char*, char, char, int, int);

    const char* getName() const;

    double computeAverage() const;

    void incrementHits(int);

    void incrementAtBats(int);

    void print() const;

};

#endif

//end of Player.h

// Player.cpp

#include "Player.h"

#include <string.h>

#include <iostream>

#include <iomanip>

using namespace std;

Player::Player()

{

       strcpy(number,"");

       strcpy(name,"");

       strcpy(position,"");

       hits=0;

       atBats=0;

       bats = 'R';

       throws = 'R';

}

Player::Player(const char* newNumber, const char* newName, const char* newPosition, char newBats, char newThrows, int newHits, int newAtBats)

{

       strcpy(number,newNumber);

       strcpy(name,newName);

       strcpy(position,newPosition);

       bats = newBats;

       throws = newThrows;

       hits = newHits;

       atBats = newAtBats;

}

const char* Player:: getName() const

{

       return name;

}

double Player:: computeAverage() const

{

       if(atBats == 0)

             return 0.0;

       else

             return(((double)hits)/atBats);

}

void Player:: incrementHits(int newHits)

{

       hits += newHits;

}

void Player:: incrementAtBats(int newAtBats)

{

       atBats += newAtBats;

}

void Player:: print() const

{

       cout<<left<<setw(30)<<"Jersey Number : "<<number<<endl;

       cout<<left<<setw(30)<<"Name : "<<name<<endl;

       cout<<left<<setw(30)<<"Poistion : "<<position<<endl;

       cout<<left<<setw(30)<<"Hits : "<<hits<<endl;

       cout<<left<<setw(30)<<"At Bats : "<<atBats<<endl;

       cout<<left<<setw(30)<<"Batting handedness : "<<bats<<endl;

       cout<<left<<setw(30)<<"Throwing handedness : "<<throws<<endl;

       cout<<left<<setw(30)<<"Batting Average : "<<fixed<<setprecision(3)<<computeAverage()<<endl;

}

//end of Player.cpp

//***************************************************************

//

// testPlayer.cpp

// CSCI 241 Assignment 2

//

// Created by name(s) and z-ID(s)

//

//***************************************************************

#include <iostream>

#include "Player.h"

using std::cout;

using std::endl;

int main()

   {

   // Test default constructor.

   Player p1;

   // Test alternate constructor.

   Player p2("12", "Van Damme, Cletus", "P", 'R', 'L', 0, 0);

   // Test print() method and whether constructors

   // properly initialized objects.

   cout << "Printing first player\n\n";

   p1.print();

   cout << endl << endl;

   cout << "Printing second player\n\n";

   p2.print();

   cout << endl << endl;

   // Test accessor method.

   cout << p2.getName() << endl;

   p2.incrementHits(2);

   p2.incrementAtBats(9);

   // Test print() again with updated batting average.

   p2.print();

   return 0;

}

//end of testPlayer.cpp

Output:

Add a comment
Know the answer?
Add Answer to:
Assignment Overview In Part 1 of this assignment, you will write a main program and several...
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++ 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;...

  • Assignment 1 In this assignment you will be writing a tool to help you play the...

    Assignment 1 In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain...

  • This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class...

    This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class Write a class named Player that stores a player’s name and the player’s high score. A player is described by:  player’s name  player’s high score In your class, include:  instance data variables  two constructors  getters and setters  include appropriate value checks when applicable  a toString method Part 2: PlayersList Class Write a class that manages a list...

  • == Programming Assignment == For this assignment you will write a program that reads in the...

    == Programming Assignment == For this assignment you will write a program that reads in the family data for the Martian colonies and stores that data in an accessible database. This database allows for a user to look up a family and its immediate friends. The program should store the data in a hashtable to ensure quick access time. The objective of this assignment is to learn how to implement and use a hashtable. Since this is the objective, you...

  • 1. You will develop a Python program to manage information about baseball players. The program will...

    1. You will develop a Python program to manage information about baseball players. The program will maintain the following information for each player in the data set: player’s name (string) team identifier (string) games played (integer) at bats (integer) runs scored (integer) hits (integer) doubles (integer) triples (integer) homeruns (integer) batting average (real) slugging percentage (real) The first nine items will be taken from a data file; the last two items will be computed by the program. The following formulas...

  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

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