Scenario and Summary
We begin our investigation of object-oriented programming by creating an object-oriented program with a class called Employee. You will create two objects based on the Employee class, along with a class that contains the main method. The attributes, constructors, and methods for this class must satisfy the requirements in Steps 1 through 3. After you create the objects, you will prompt the user for information and then display it. We will want to ensure that this first class is well-constructed and tested since we will extend this class in Labs 3 through 6.
STEP 1: Understand the UML Class Diagram
Analyze and understand the object UML diagram, which models the structure of the program. The first section specifies the attributes. The second section specifies the operations, and the first character specifies the access modifier value, where:
- “-” means that the class member is private
- “+” means that the class member is public.
- Each attribute has a default value assigned, and you will create private class constants to hold these values and initialize the values in the default constructor.
- The InputUtitlities and ApplicationUtilities classes are provided for you (see below) and are used by the Main method to display the standard application information and there are methods that will prompt, retrieve, and convert each of the different input data types. You will use these classes for each of the following weeks assignments. Directions on how to include these in your project are provided below.
- One final note, for this lab ONLY will the attributes be made public. When we discuss accessors and mutators (get and set) methods in Week 3 we will modify these attributes to be “private” and all access to these attributes will be done with the get and set methods, or properties.
Layer/Tiered Program Architecture
In your following courses you will learn about a design principle called Tiered or Layered Architecture. This is a design technique that professional program desigers use to manage the complexity of real world applications. We do not want to go into the details of a tiered design in this course, but we do want tolearn to program using the underling principles since using the tiered approach is a practical way to simpify the coding process that professional programmers use and you will discover the tiered approach enables us to make changes to an existing program much easier. The programming labs in this course will lead you through this process but let’s define the two standard tiers that we will use in this course.
- Presentaiton tier contains all the classes that will provide input/output operations and the main program. There should NOT be any actual data processing in the presentation tier and the presenation tier should only provide input/ouput operations and execution control. Once data is collected in the presentation tier it is passed into the logic tier for processing. Once the logic tier processes the data the logic tier passes the information back to the presentation tier for output.
- Logic tier contains all the classes that represent the business objects that are necessary to hold and process the application data. There should not be any user input/output operations in the Logic Tier and the Logic Tier should only process the data collected by operations in the presentation tier and then send the back the information operations in the presentation tier for display.
One last note. A tiered design is a “logical” design concept, which means that the physical files that hold the classes can exist in any location within your project. However, MS Visual Studio Solution Explorer provides tools to create folders and subfolders within your project that will allow us to store the classes for each teir into separate folders. However, will will NOT create these folders until Week 4 when we start adding more classes to the application. So, for now we just want to put the various files under the root project folder.
STEP 2: Create a New Project and Add Existing Classes
In the Week 1 lab you created a set of re-useable classes and methods and in order to demonstrate the power of re-usablity and to provide a common interface in all our programs, you will create a project and add some existing classes that contain re-useable methods that we will use throughout the rest of labs. In order to re-use the existing classes follow these steps:
- Go to the Doc Sharing area and download the Week 2 Utilities Class file, which is a zip file that contains two classes (1) ApplicationUtilities.cs and (2) InputUtilities.cs. Unzip the two files and place the files in a known location.
- Create a new project called “CIS247_WK2_Lab_LASTNAME”. An empty project will then be created.
- Right click the project name in the Solution Explorer, or select the Project Menu, and select “Add Existing Item”
- Navigate to the folder where you saved the (1) ApplicationUtilities.cs and (2) InputUtilities.cs class files, select the files, and then click add.
- The files will be added to your project.
- The final step is to change the name of the Namespace so all the classes in the project can
recognize each other (there are advanced ways around this, but for this course may be easiest to change the namespace. In order to make it easier for the follow on weeks, change the namespace to all the classes to “Employee”. The file list in the Solution Explorer will then look something like:
Since all the lab assignments through the rest of the course will re-use classes from the previous week’s assignments, make sure you become proficient in adding existing items–it will reduce the
amount of “busy work” you have to do each week.
STEP 3: Code the Employee class
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 provided Class Diagram as a guide code the Employee class in the new project (i.e., “Realize the UML Class diagram”).
- Add a new class called “Employee” to the project
- For each of the private attributes
- Create constants in the Employee class for the default values, and the default constuctor
shall set these default values to the appropriate attribute.
- The multi-argument constructor (parameterized constructor) should initialize all of the attributes using values passed in through the parameter list.
- As mentioned above, for this lab ONLY will the attributes be made “public” and we will access the attributes directly. Begininning in Week 3 ALL variable attributes of every class will be private and access will be done through the get and set methods.
- The CalculateWeeklyPay( ) method of the Employee class should return the value of annual salary divided by 52 (return annualSalary / 52;).
- Most classes will have a ToString method that collects the key class attributes into a single well formated string. The method will always be declared using the “override” keyword (this will be explained in Week 5) and the declaration of the ToString method for every class will look like the following:
public override string ToString()
- The ToString method will always return a string value that collects the key attributes, the following shows one technique that you can use to collect the information and return the formatted string:
override string ToString()
output = “============ Employee Information ============”;
output += “Name: ” + firstName + ” ” + lastName;
//rest of method omitted
- In order to print out currency values in currency format (dollars and cents) you can use the ToString method that each numeric variable has predefined. For example, to output the annualSalary in currency you would use the following:
output += “Annual Salary: ” + AnnualSalary.ToString(“C2′′);
STEP 4: Code the Main Program
In the Main class, create code statements that perform the following operations.
Remember, to access a public method (method or property) of an object, or class, use the DOT notation:
For example, to access the DisplayApplicationInformation of the ApplicationUtilities class, the
statement would look something like: ApplicationUtilities.DisplayApplicationInformation();
- 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
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. See Syllabus “Due Dates for Assignments & Exams” for due date information.