Create a class called Employee that contains 3 pieces of information as instance
ID: 3541758 • Letter: C
Question
Create a class called Employee that contains 3 pieces of information as instance
variables: first name (datatype string), last name (datatype string), monthly salary.(
datatype decimal).
Your class should contain a constructor that initializes the 3 instance variables. It should
also contain a property for each instance variable with get and set accessors. The
property for salary should verify that the salary is greater than 0. If a negative value is
passed in, the set accessor should set the salary to 0.
Create a second class EmployeeTest that creates two employee objects using the
constructor that you developed in the Employee class. Display the name and salary for
each Employee. Use the set property for salary to give each employee a 10% raise,
Display the name and salary for each employee again.
Explanation / Answer
using System.IO;
using System;
public partial class Employee
{
private string fname;
private string lname;
private double salary;
public Employee(string firstname, string lastname, double sal)
{
fname=firstname;
lname=lastname;
if(sal<0)
salary=0;
else
salary=sal;
}
public void setFname(string firstname)
{
fname=firstname;
}
public void setLname(string lastname)
{
lname=lastname;
}
public void setSalary(double sal)
{
if(sal<0)
{
salary=0;
}
else
salary=sal;
}
public string getFname()
{
return fname;
}
public string getLname()
{
return lname;
}
public double getSalary()
{
return salary;
}
}
class EmployeeTest
{
static void Main()
{
// Read in every line in the file.
Employee emp1=new Employee("test 1","test 1",10000);
Employee emp2=new Employee("test 2","test 2",20000);
Console.WriteLine("Employee 1 Details:");
Console.WriteLine("Name:"+emp1.getFname()+" "+emp1.getLname()+" salary:"+emp1.getSalary());
Console.WriteLine("Employee 2 Details:");
Console.WriteLine("Name:"+emp2.getFname()+" "+emp2.getLname()+" salary:"+emp2.getSalary());
emp1.setSalary(emp1.getSalary()+emp1.getSalary()*0.1);
emp2.setSalary(emp2.getSalary()+emp2.getSalary()*0.1);
Console.WriteLine("Employee 1 Details:");
Console.WriteLine("Name:"+emp1.getFname()+" "+emp1.getLname()+" salary:"+emp1.getSalary());
Console.WriteLine("Employee 2 Details:");
Console.WriteLine("Name:"+emp2.getFname()+" "+emp2.getLname()+" salary:"+emp2.getSalary());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.