Question

1) Introduction to Objects (with Constructors and Properties) We need to answer the question which are...

1) Introduction to Objects (with Constructors and Properties)

We need to answer the question which are below.
and instruction are given down starting with part(b).

Just we need to copy paste code file and follow the instruction and answer related to them.

a) Enter the following program.Answer The questions below the codes, Notice that it consists of two classes that will go into the same project. Please put each class into its own code file (use Lab5_1 for the Project name and Lab5_1_1 / Lab5_1_2 for the Code File names). This program focuses on classes, objects and properties. Class EmployeeDemo is a static class that contains Main().

//Lab5_1_1

//code file

using System;
public static class EmployeeDemo
{
    public static void Main()
    {
        // Create Employee objects utilizing different constructors
        Employee emp1 = new Employee();
        // Employee emp2 = new Employee("Joy Rogers", 10127, "HR", "Manager");
        // Employee emp3 = new Employee("Mark Jones", 10065);

        // Use Properties to assign values to data fields
        // emp1.Name = "Susan Meyers";
        // emp1.IdNumber = 10012;
        // emp1.Department = "Accounting";
        // emp1.Position = "Vice President";

        // emp3.Department = "IT";
        // emp3.Position = "Support";

        // Use Properties to output data from Employee objects
        Console.WriteLine("Employee 1: {0}, {1}, {2}, {3}",
           emp1.Name, emp1.IdNumber, emp1.Department, emp1.Position);
        // Console.WriteLine("Employee 2: {0}, {1}, {2}, {3}", 
        //     emp2.Name, emp2.IdNumber, emp2.Department, emp2.Position);
        // Console.WriteLine("Employee 3: {0}, {1}, {2}, {3}", 
        //     emp3.Name, emp3.IdNumber, emp3.Department, emp3.Position);
        Console.ReadLine();
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Lab5_1_2

// Code File

using System;   // Code File Lab5_1_2.cs
public class Employee   // this is a dynamic class (no “static”)
{
   private int idNumber;
   private string name, department, position;

   public Employee() // Constructor 1: No Arg
   {
      Name = "";
      IdNumber = 0;
      Department = "";
      Position = "";
   }
 
   public Employee(string eName, int eIdNum) // Constructor 2: Two Arg
   {
      Name = eName;
      IdNumber = eIdNum;
      Department = "";
      Position = "";
   }
 
   // Constructor 3: Four Arg
   public Employee(string eName, int eIdNum, string eDept, string ePos)
   {
      Name = eName;
      IdNumber = eIdNum;
      Department = eDept;
      Position = ePos;
   }
 
   public string Name   // Property for priviate data - name
   {
      get { return name; }
      set { name = value; }
   }
 
   public int IdNumber  // Property for priviate data - idNumber
   {
      get { return idNumber; }
      set { idNumber = value; }
   }
 
   public string Department     // Property for priviate data – department
   {
      get { return department; }
      set { department = value; }
   }

  public string Position        // Property for priviate data - position

  {
     get { return position; }
     set { position = value; }
  }
}

PDF Questions

b) Build and execute the program (F5) … resolve any syntax mistakes (if you make any) until the program compiles. What is the output? Which constructor from the Employee class was invoked to instantiate emp1 object? Why are the contents of the emp1 object appear to be empty when printed?

c) Uncomment Lines 12-15 in the EmployeeDemo class to render them active (i.e, remove the // from in front of the C# commands). Compile and execute the program (F5). What is the output? Why is the output for Employee 1 different that in Part (b)?

d) Uncomment Lines 8, 23, 24 in the EmployeeDemo class to render them active, execute the program (F5). What is the output? Which constructor from the Employee class was invoked to instantiate emp2 object?

e) Uncomment Lines 9, 25 and 26 in the EmployeeDemo class to render them active, execute the program (F5). What is the output? Which constructor from the Employee class was invoked to instantiate emp3 object?

f) Uncomment Lines 17 and 18 in the EmployeeDemo class to render them active, execute the program (F5). What is the output? Why is the output for Employee 3 different that in Part (e)?

g) In Line 13, change IdNumber to idNumber in the EmployeeDemo class (basically you are substituting a public property for a private name).

Run the program (F5). What happens and why?

n) In Line 4 of the Employee class (note the different file here), change private to public. Run the program (F5). What happens and why? Is it a good idea to solve the problem this way? Why?

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

b) Here is the output:

The default constructor (first one) is called as there is no parameter passed.

public Employee()
{ Name = "";
IdNumber = 0;
Department = "";
Position = "";
}

The object appeared to be empty when printed because we did not set the values of object properties.

c) Here is the output:

The output is different from the b) as now we assigned values to the object properties using below code.

emp1.Name = "Susan Meyers";
emp1.IdNumber = 10012;
emp1.Department = "Accounting";
emp1.Position = "Vice President";

d) Here is the output:

The third parameterized constructor was invoked to instantiate emp2 object as 4 parameter are passed.

public Employee(string eName, int eINum, strng eDept, string ePs)
{
Name = eName;
IdNmber = eIdNum;
Department = eDept
Position = ePos;
}

e) Here is the output:

The second parameterized constructor was invoked to instantiate emp3 object as 2 parameter are passed.

public Employee(string eName, int eIdNum)
{
Name = eName;
IdNumber = eIdNum;
Department = "";
Position = "";
}

f) Here is the output:

The output is different from the e) as now we assigned values to the object emp2 remaining properties using below code which were not set in the constructor.

emp3.Department = "IT";
emp3.Position = "Support";

g) It throws a compile-time error as idNumber is inaccessible due to its protection level. Private properties are only accessible within the class.

h) Changing line 4 to public removes the error that we were getting in g). This is because public members are accessible outside the class. This not a good idea to solve the problem as private members created for internal working only and allowing access to them will create problems in the program

Add a comment
Know the answer?
Add Answer to:
1) Introduction to Objects (with Constructors and Properties) We need to answer the question which are...
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
  • Create a C# Console program. Add a class named Employee that has the following public fields:...

    Create a C# Console program. Add a class named Employee that has the following public fields: • Name. The name field references a String object that holds the employee’s name. • IDNumber. The IDNumber is an int that holds the employee’s ID number. • Department. The department field is a String that holds the name of the department where the employee works. • Position. The position field is a String that holds the employee’s job title. Once you have written...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • Using C++, Write a class named Employee that has the following member variables: name. A string...

    Using C++, Write a class named Employee that has the following member variables: name. A string that holds the employee's name. idNumber. An int variable that holds the employee's ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee's job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name,...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • Create a c++ code and answer number 1 Homework Ch. 13 - Employee Class 30 points...

    Create a c++ code and answer number 1 Homework Ch. 13 - Employee Class 30 points Design a class named Employee that has the following attributes: * Name ID Number- (Employee's] Identification Number " Department-the name of the department where the employee works " Position-the employee's job title The class should have the following constructors: A constructor that accepts values for all the member data as arguments A constructor that accepts the following values as arguments and assigns them to...

  • Write a C++ program Write a class named Employee that has the following member variables: name...

    Write a C++ program Write a class named Employee that has the following member variables: name A string that holds the employee name idNumber An int to hold employee id number department A string to hold the name of the department position A string to hold the job position The class will have three constructors: 1. A constructor that accepts all the internal data such as: Employee susan("Susan Meyers", 47899, "Accounting", "Vice President"); 2. A constructor that accepts some of...

  • PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property...

    PYTHON Task 1 Create a class called Window It has 2 public properties 1 private property 1 constructor that takes 3 arguments pass each argument to the appropriate property Task 2 In a new file import the Window class Instantiate the Window object Output the two public properties Task 3 Alter the Window class in Task 1 Add an accessor and mutator (the ability to access and change) to the private property Task 4 Create a class named Instrument Give...

  • Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person...

    Concepts Tested in this Program: Class Design Constructors Objects Inheritance Program:   Design a class named Person and its two subclasses, Student and Employee. Make Faculty and Staff subclasses of Employee. A Person object has a name, address, phone number, and email address (all Strings). A Student Object has a class status (freshman, sophomore, junior, or senior). Define the status as a final String variable. An Employee Object has an office number, salary (both ints ), and a date hired. Use...

  • C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employee...

    C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...

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