Description
CIS 247A Week 3 iLab Overloaded Methods and Static Methods Variables
Scenario and Summary
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 an overloaded method of CalculatePay that will accept a new, or modifed, annual salary
value. We will then have two versions of the CalculatePay method (1) that uses the current value of the annual salary for the calculation and (2) one that updates the annual salary with a new value before the calculation. - Using properties add accessor (get) and mutator (set) methods to access each of the private attributes and to validate the attribute values before they are set.
iLAB STEPS
STEP 1: Understand the UML Diagram
- Analyze and understand the object UML diagram, which models the structure of the program.
- There are no design changes to the Presentation Tier from the previous project and InputUtilities and ApplicationsUtilities classes are used without modification (except for changing the Application Information).
- The default values for each of the attributres have been declared as a constants, which is indicated by the ALL_CAPS in the name, and the attributes are then set using the default values
- Each of the attributes have been specified as private.
- The accessors (get) and mutators (set) are not shown on the class diagram, but it is ASSUMED that each private attribute has a corresponding property that contains the get and set methods.
- The “static” modifier for the numEmployees attribute means that there is only one copy of the variable that is then shared by all the objects of the class.
- There is a second CalculatePay method that overloads the existing CalculatePay method
- While not shown on the class diagram, the property for numEmployees will only have a get method, which means it will be a “read only” method. (A property with only and set method is a “wrute-only” property).
STEP 2: Create the Project
You will want to use the Week 2 project 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 the InputUtilities,
ApplicationUtilities, Employee, and Program classes from your project folder from last week‟s lab. - The namespaces for the classes should all be “Employee”, but you should verify that the namespaces for all the classes are the same.
- Update the program information in the ApplicationUtilities.DisplayApplicationInformation method to reflect your name, current lab, and program description.
- Build and execute the 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
Be sure you follow proper commenting and programming styles (header, indentation, line spacing, etc.) that are specified in the Programming Conventions and Standards guide. Using the Employee class diagram as a guide modify the Employee class:
- Add the constants to the class using the following as an example:
public const double MIN_SALARY = 20000; - In the default constructor, update assignment statements to use the defined constants
- Change all the employee class attributes to private.
- Create a private static numEmployees variable and initialize it to zero
- Increment numEmployees by 1 in each of the constructors
- For each private attribute, create a well-named property that contains the get and set
methods. The get method of the property only needs to return the value of the attribute; but the set method of each property needs to validate the provided value using the following validation rules:
a. If the provided first or last values are empty, or a null value, then set the name to DEFAULT_NAME.
b. If the provided gender value is „F‟, „f‟, „M‟, or „m‟ set the value; otherwise set the value to DEFAULT_GENDER.
c. If the provided dependent value is between the MIN_DEPENDENTS and MAX_DEPENDENTS (inclusive) then set dependent to the provided value; if the provided value is less than MIN_DEPENDENTS set the dependents to MIN_DEPENDENTS; else if provided value is greater than MAX_DEPENDENTS set the dependents to MAX_DEPENDENTS.
d. If the provided salary value is between the MIN_SALARY and MAX_SALARY (inclusive) the set the annualSalary to the provided value; if the provided value is less than MIN_SALARY set the annualSalary to MIN_SALARY; else if provided value is greater than MAX_SALARY set the annualSalary to MAX_SALARY.
e. For the numEmployee attribute create a property called NumberEmployees that only contains a “get” method, external objects should NOT be allowed modify the numEmployee value. Since numEmployees is a static method, the property must be declared as static. - In the parameterized constructor, change statements that set the attributes so that the properties are used, which ensures that attributes are validated prior to be set.
- Create the overloaded CalculateWeeklyPay method that accepts a double “modifiedSalary” argument. The method shall update the annualSalary attribute (use the AnnualSalary property to ensure the value is valid), and then return the updated weekly pay based on the new annual salary value.
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.) and use the ApplicationUtiltities methods to logically separate the operations in the output.
To access a property of an object/class, you continue to use the DOT notation; however, a property DOES NOT require the parenthesis and you just use the assignment operator (=) to set or get the value, which makes using propertys very easy. For example to set the first name, the statement would look something like:
1
|
employee1.FirstName = “John” |
To get the full name, the statement would look look something like:
1
|
theName = employee1.FirstName + ” ” + employee1.LastName |
Notice, there is no use of parenethese, only the assignment operator, when using properties.
The Main method code from the previous week‟s lab performed the following operations, ensure that your project correctly implements these operations before moving on the new operations for this week.
- Display the program information.
- Create an Employee object using the default constructor.
- Prompt for and then set the first name, last name, gender, dependents, and annual salary.
Remember to use the appropriate methods in the InputUtilties class to prompt for and retreive the values. - Display the employee information.
- Create a second Employee object using the multi-argument constructor using data of your
choosing that is the correct type and within the valid ranges for each of the attributes. - Display the Employee information for the second employee object.
- Terminate the application
Once your code is working and implements the previous week‟s operations, modify the code to implement the following new requirements (updated code should implement all previous requirements except as noted below).
- After the first employee information is provided, display the number of employees created.
- Prompt the user to provide an updated annual salary for employee1, retrieve the value and
invoke the overloaded CalculateWeeklyPay, and then display only the updated weekly pay. - Create a third Employee object using the parameterized constructor setting each of the attributes with the following values: “Sue”, “Smith”, „F‟, 15, 500000.0
- Display the employee information for the third Employee object and verify that the dependents and annual salary values have been set to the maximum values by the properties. If not, make sure you change the parameterized constructor to use the properties to set the attributes.
- Display the number of employees created.
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. The following shows some sample output, but your output may look different.
STEP 6: Submit Deliverables
Before you post your lab in the dropbox, copy your entire program into a Notepad file and post that. I do not need you to zip the project or give me screen shots of the output.
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.
Reviews
There are no reviews yet.