Scenario and Summary The objective of the lab is to take the UML Class diagram a
ID: 3633111 • Letter: S
Question
Scenario and SummaryThe 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 files.
Put the zip file and screen shots (Word document) in the Dropbox.
i L A B S T E P S
Understand the UML Diagram
Image Description
What is depicted is a single column table with 3 rows. In the first row is the large word "Employee".
In the row beneath this is a list of private parameters, indicated with a "minus" symbol before them (but no space after). These are:
-firstName : String
-lastName : string
-gender : char
-dependents : int
-annualSalary : double
-static numEmployees: int = 0
In the third and final row beneath this is another list of public parameters, indicated with a "plus" symbol before them (but no space after). These are:
+Employee()
+Employee(in first : string, in last : string, in gen : char, in dep : int, in salary : double)
+CalculatePay() : double
+DisplayEmployee() : void
+static GetNumEmployees() : int
+GetFirstName() : string
+SetFirstName(in first : string) : void
+GetLastName() : string
+SetLastName(in first : string) : void
+GetGender() : char
+SetGender(in gen : char) : void
+GetDependents() : int
+SetDependents(in dep : int) : void
+GetAnnualSalary() : double
+SetAnnualSalary(in salary : double) : void
+SetAnnualSalary(in salary : string) : void
Press the ESC key to close the image description and return to lecture
Image Description
The following attribute has been added:
- static numEmployees: int = 0
The following behaviors have been added:
+ static GetNumEmployees( ) : int
+ SetDependents(in dep : string) : void
+ SetAnnualSalary(in salary : string) : void
STEP 2: Create the Project
You will want to use the Week 2 Employee class, as the starting point for the lab. To do this, you will want to create a new project by following these steps:
Create a new project named "CIS247_WK3_Lab_LASTNAME". An empty project will then be created.
Delete the default Program.cs file that is created.
Click on Project->Add Existing Item…. Select the .cs files containing your Employee AND Program code from your project folder from last week's lab.
Before you move on to the next step, build and execute the Week 3 project.
For each week's assignments you will follow these steps create a new project that reuses the program from the previous week.
STEP 3: Modify the Employee
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
Since C# supports properties, you can replace the get and set methods for firstName, lastName, gender, and dependents you created last week, with properties. The use of properties is standard in C# programs.
If you are using set methods instead of properties, create an overloaded SetDependents method and, this time, make the parameter a string.
If you are using set methods instead of properties, 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 GetNumEmployees a static method. This way, you can call it with the class name instead of an object name. Ex.
Employee.GetNumEmployees(); //This is OK
instead of:
Employee myEmployee = new Employee();
myEmployee.GetNumEmployees(); //This is an ERROR
Adjust the output of the method DisplayEmployee so that at the end, the total number of employees created is displayed. Remember to access GetNumEmployees using the class name and not the Employee object.
Be sure you follow proper commenting and programming styles (indentation, line spacing, etc.).
STEP 4: Modify the Main Method
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. Use your properties or setter methods. 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.
Consider using your code from Week 1 to 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
Consider using your code from Week 1 to 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
When done, compile and execute your code. Debug 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.
Your output should resemble the following:
On-screen output display:
*********************** Employee Information **********************
First Name: John
Last Name: Doe
Gender: M
Dependents: 7
Annual Salary: $32,500.00
Weekly Pay: $625.00
total employees: 1
*********************** Employee Information **********************
First Name: Mary
Last Name: Noia
Gender: F
Dependents: 5
Annual Salary: $24,000.00
Weekly Pay: $461.54
Total Employees:2
Press the ESC key to close the image description and return to lecture
Image Description
STEP 6: Submit Deliverables
Capture the output window and paste it into a Word Document.
Put the zip file and screen shots (Word document) in the Dropbox.
Submit your lab to the Dropbox located on the silver tab at the top of this page. For instructions on how to use the Dropbox, read these Step-by-Step Instructions or watch this Dropbox Tutorial.
See Syllabus "Due Dates for Assignments & Exams" for due date information.
Explanation / Answer
//Header files
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Employee
{
private:
//Private members of class Employee
string firstName;
string lastName;
string gender;
int dependents;
double salary;
static int numEmployees;
public:
//Public member funtions of class Employee
//Default constructor
Employee()
{
firstName="Not-Given";
lastName="Not-Given";
gender="U";
dependents=0;
salary=20000;
numEmployees +=1;
}
//parameterised constructor
Employee(string firstName,string lastName,
string gender,int dependents,double salary)
{
numEmployees +=1;
this->firstName =firstName;
this->lastName =lastName;
this->gender =gender;
this->dependents =dependents;
this->salary =salary;
}
//Method to prompt the user to enter Employee data
void getData()
{
cout<<"Enter firstName ";
cin>>firstName;
setFirstName(firstName);
cout<<"Enter lastName ";
cin>>lastName;
setLastName(lastName);
cout<<"Enter Gender ";
cin>>gender;
setGender(gender);
cout<<"Enter Dependents ";
cin>>dependents;
setDependents(dependents);
cout<<"Enter Salary ";
cin>>salary;
setAnnualSalary(salary);
}
//setter methods
void setFirstName(string firstName)
{
this->firstName=firstName;
}
void setLastName(string lastName)
{
this->lastName =lastName;
}
void setGender(string gender)
{
this->gender =gender;
}
void setDependents(int dependents)
{
this->dependents =dependents;
}
void setAnnualSalary(double salary)
{
this->salary =salary;
}
//getter methods
string getFirstName()
{
return firstName;
}
string getLastName()
{
return lastName;
}
string getGender()
{
return gender;
}
int getDependents()
{
return dependents;
}
double getAnnualSalary()
{
return salary;
}
//To calculate
double calculatePay()
{
return getAnnualSalary()/52;
}
void display()
{
displayDivider();
cout<<setw(15)<<"FirstName :"<<getFirstName()<<endl;
cout<<setw(15)<<"LastName :"<<getLastName()<<endl;
cout<<setw(15)<<"Gender :"<<getGender()<<endl;
cout<<setw(15)<<"Dependents :"<<getDependents()<<endl;
cout<<setw(15)<<setprecision(2)<<showpoint<<fixed<<
"AnnualSalary :"<<getAnnualSalary()<<endl;
cout<<setw(15)<<setprecision(2)<<showpoint<<fixed<<
"Weekly Pay:" <<calculatePay()<<endl<<endl;
}
//method for Heading for Employee Information
void displayDivider()
{
cout<<" EMPLOYEE INFORMATION"<<endl;
cout<<"****************************************"<<endl;
}
static int getNumEmployees()
{
return numEmployees;
}
};
int Employee::numEmployees=0;
int main()
{
//Creating Employee object
//Prompt user to enter Employee data
Employee obj;
//calling getData method
obj.getData();
obj.display ();
//Creating parameterised constructor
Employee obj2("Mary", "Noia", "F", 5, 24000.0);
obj2.display();
int numEmp=Employee::getNumEmployees();
cout<<"Number of employees :"<<numEmp<<endl;
system("pause");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.