The objective of the lab is to take the UML Class diagram and enhance last week\
ID: 669515 • Letter: T
Question
The objective of the lab is to take the UML Class diagram and enhance last week's Employee class by making the following changes:
Create a static variable called numEmployees that holds an int and initialize it to zero. This will allow us to count all the Employee objects created in the main class.
Increment numEmployees in all of the constructors
Add overloaded versions of setDependents and setAnnualSalary that accept strings. This way, we will have two "set" methods for both dependents and annual salary; one that accepts a string, and one that accepts its default data type.
Deliverables
Due this week:
Capture the Console output window and paste it into a Word document.
Zip the project folder files.
Put the zip file and screen shots (Word document that contains programming code and screen shots of program output) in the Dropbox.
iLAB STEPS
STEP 1: Understand the UML Diagram
Back to Top
The following attribute has been added:
The following behaviors have been added:
STEP 2: Create the Project
Back to Top
You will want to use the Week 2 project as the starting point for the lab.
STEP 3: Modify the Employee
Back to Top
Using the UML Diagrams from Step 1, code the changes to the Employee class.
Create a static numEmployees variable and initialize it to zero.
Increment numEmployees by 1 in each of the constructors.
Create an overloaded setDependents method and this time make the parameter a string.
Create an overloaded setAnnualSalary method and this time make the parameter a string.
Remember that you will have to convert the string in the above two "set" methods to the data type of the attribute.
Make the getNumEmployees a static method. (This way, you can call it with the class name instead of an object name.)
Be sure you follow proper commenting and programming styles (indentation, line spacing, etc.).
STEP 4: Modify the Main Method
Back to Top
In the Main class, create code statements that perform the following operations. Be sure you follow proper commenting and programming styles (header, indentation, line spacing, etc.). Note that several of the steps below were accomplished in last week's assignment. New steps are in bold.
Create an Employee object using the default constructor.
Prompt for and then set the first name, last name, and gender. Consider using your getInput method from Week 1 to obtain data from the user for this step as well as Step 3.
Prompt for and then set dependents and annual salary using the new overloaded setters.
Using your code from Week 1, display a divider that contains the string "Employee Information".
Display the Employee Information.
Display the number of employees created using getNumEmployees. Remember to access getNumEmployees using the class name, not the Employee object.
Create a second Employee object using the multi-arg constructor, setting each of the attributes with the following values: "Mary", "Noia", 'F', 5, 24000.0
Using your code from Week 1, display a divider that contains the string "Employee Information".
Display the employee information for the second Employee object.
Display the number of employees created using getNumEmployees. Remember to access getNumEmployees using the class name, not the Employee object.
STEP 5: Compile and Test
Back to Top
When done, compile and run your code.
Then, debug any errors until your code is error-free.
Check your output to ensure that you have the desired output, modify your code as necessary, and rebuild.
STEP 6: Screen Prints
Back to Top
Capture the Console output window and paste it into a Word document. The following is a sample program output.
THIS IS WHAT I GOT FROM WEEK 2 LAB
#include <iostream>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
const double MIN_SALARY = 50000;
const double MAX_SALARY = 250000;
const int MAX_DEPENDENTS = 10;
const int MIN_DEPENDENTS = 0;
const char DEFAULT_GENDER = 'N'; //N stands for not identified
const int NUMBER_WEEKS = 52;
class Employee
{
string firstName;
string lastName;
char gender;
int dependents;
double annualSalary;
public:
Employee()//default constructor
{
firstName = "";
lastName = "";
gender = 'N';
dependents = 0;
annualSalary = 50000;
}
//create a parameterized construct, not required and shown only for demonstration
Employee(string firstName, string lastName, char gender, int dependents, double salary)
{
//use the THIS keyword to distinguish between the class attributes and the parameters
this->firstName = firstName;
this->lastName = lastName;
this->gender = gender;
this->dependents = dependents;
this->annualSalary = salary;
}
//Accessors and mutators, one for each class attribute
string getFirstName()
{
return firstName;
}
void setFirstName(string name)
{
firstName = name;
}
string getLastName()
{
return lastName;
}
void setLastName(string name)
{
lastName = name;
}
char getGender()
{
return gender;
}
void setGender(char gen)
{
switch (gen)
{
case 'f':case 'F': case 'M':case 'm':
gender = gen;
break;
default:
gender = DEFAULT_GENDER;
}
}
int getDependents()
{
return dependents;
}
void setDependents(int dep)
{
if (dep >= MIN_DEPENDENTS && dep <= MAX_DEPENDENTS)
{
dependents = dep;
}
else if (dep < MIN_DEPENDENTS)
{
dep = MIN_DEPENDENTS;
}
else
{
dependents = MAX_DEPENDENTS;
}
}
double getAnnualSalary()
{
return annualSalary;
}
void setAnnualSalary(double salary)
{
if (salary >= MIN_SALARY && salary <= MAX_SALARY)
{
annualSalary = salary;
}
else if (salary < MIN_SALARY)
{
annualSalary = MIN_SALARY;
}
else
{
annualSalary = MAX_SALARY;
}
}
double calculatePay()
{
return annualSalary/NUMBER_WEEKS;
}
void displayEmployee()
{
cout<<"Employee Information ";
cout<<"____________________________________________________________ ";
cout<<"Name: " <<firstName << " " << lastName << " ";
cout<<"Gender: " << gender << " ";
cout<<"Dependents: " << dependents << " ";
cout<<"Annual Salary: " << setprecision(2)<<showpoint<<fixed<<annualSalary << " ";
cout<<"Weekly Salary: " << setprecision(2)<<showpoint<<fixed<<calculatePay();
}
};
/****************************************************************
* Description: Employee test main()
*
* This program tests the Employee class by creating an object
* of the class and performing the following operations:
*
* 1. Create an Employee object using the default constructor.
* 2. Prompt for and then set the first name, last name, gender, dependents, and annual salary. (Remember that you have to convert gender, dependents, and annual salary from strings to the appropriate data type.)
* 3. Display the employee information.
* 4. CalculatePay to determine the weekly pay and store the returned value in pay.
* 5. Create a second Employee object using the default constructor, setting each of the attributes with appropriate valid values
*
* Programmer: Prof.Nana Liu
* Course: CIS247C
* Week 2 Lab Assignment
* Date: 07/21/2011
* ****************************************************************/
void DisplayApplicationInformation()
{ cout<<"Welcome to your first Object Oriented Program--Employee Class"
<<"CIS247C, Week 2 Lab"
<<"Name: Prof.Nana Liu";
}
void DisplayDivider(string message)
{cout<<" *************** " + message + " ********************* ";}
string GetInput( string message)
{ string mystring;
cout<<"Please enter your "<<message;
getline(cin, mystring);
return mystring;
}
void TerminateApplication()
{ cout<<" The end of the CIS247C Week2 iLab. ";}
int main()
{
//create two employee objects
Employee employee1; //declare and instantiate the object variable
char gender;
string str;
//always provide some type of application information to the user--don't leave them cold!
DisplayApplicationInformation();
//use a utility method to keep a consistent format to the output
DisplayDivider("Employee 1");
//access the employee objects members using the DOT notation
employee1.setFirstName(GetInput("First Name "));
employee1.setLastName(GetInput("Last Name "));
str = GetInput("Gender ");
gender = str.at(0);
employee1.setGender(gender);
employee1.setDependents(atoi( GetInput("Dependents ").c_str()));
employee1.setAnnualSalary(atof(GetInput("Annual Salary ").c_str()));
employee1.displayEmployee();
DisplayDivider("Employee 2");
//create a second employee object this time use the default constructor
Employee employee2("Mary", "Noia", 'F', 2, 150000);
employee2.displayEmployee();
TerminateApplication();
}
Explanation / Answer
#include #include #include #include using namespace std; const double MIN_SALARY = 50000; const double MAX_SALARY = 250000; const int MAX_DEPENDENTS = 10; const int MIN_DEPENDENTS = 0; const char DEFAULT_GENDER = 'N'; //N stands for not identified const int NUMBER_WEEKS = 52; //Employee class creation class Employee { string firstName; string lastName; char gender; int dependents; double annualSalary; static int numEmployees; //wk3 adds public: Employee() // Default constructor created { firstName = ""; lastName = ""; gender = 'N'; annualSalary = 50000; } Employee(string firstName, string lastName, char gender, int dependents, double salary) { this->firstName = firstName; this->lastName = lastName; this->gender = gender; this->dependents = dependents; this->annualSalary = annualSalary; } //Setters and Getters for each class attributes string getFirstName() { return firstName; } void setFirstName(string name) { firstName = name; } string getLastName() { return lastName; } void setLastName(string name) { lastName = name; } char getGender() { return gender; } void setGender(char gen) { switch (gen) { case'f': case'F': case'M': case'm': gender = gen; break; default: gender = DEFAULT_GENDER; } } int getDependents() { return dependents; } void setDependents(int dep) { if (dep >= MIN_DEPENDENTS && depRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.