Click here to Skip to main content
13,895,584 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi, I'm on my first week of Object Oriented class using C#, so far I have learned the basic stuff but my assignment is complicated and I have to do the following:

Create a class called Employee that includes three pieces of information as data members
- a first name (type string), a last name (type string) and a monthly salary (type floating point).
Your class should have a constructor that initializes the three data members.
Provide a set and get functions for each data member.
Create two Employee objects and display each object's yearly salary.
Then give each Employee a 10% raise and display each Employee's yearly salary again

What I have so far is because I googled it and found something similar but I haven't been able to compile successfully, I've been using C# all the time

What I have tried:

public class Employee 
{
    
    private string firstName_;
    private string lastName_;
    private double monthlySalary_;
    
    public Employee(string first, string last, double mSalary) 
    {
        firstName = first;
        lastName = last;
        monthlySalary = mSalary;
    }
    //starts firstname string get set
    public string firstName
    {
        get
        {
             return firstName_;
        }    
        set
        {
            firstName_ = first;
        }
    }  //end firstname string get set

    //starts lastname string get set
    public string lastName
    {
        get
        {
            return lastName_;
        }
        set
        {
            lastName_ = last;
        }  
    }   //end lastname string get set

    //starts monthlySalary string get set
    public double monthlySalary
    {
        get
        {
            return monthlySalary_;
        }
        set
        {
            monthlySalary_ = mSalary;
        }
    }   //end of monthlysalary double get set
}

//employee test

public class Employeetest
{
    public static void Main(string[] args)
    {
        employee E1 = new employee ("John", "Doe", 3000.00);
        employee E1 = new employee ("Jane", "Doe", 4000.00);
        
        //display information
        System.Console.WriteLine("Last name is {0}", "First name is {1}, with a monthly salary of {2}",
        E1.getlastName, E1.getfirstName, E1.getmonthlySalary);

        System.Console.WriteLine("Last name is {0}", "First name is {1}, with a monthly salary of {2}",
        E2.getlastName, E2.getfirstName, E2.getmonthlySalary);
    }
    
}
Posted
Updated 11-Jan-19 23:07pm
Comments
0x01AA 11-Jan-19 16:06pm
   
"but I haven't been able to compile successfully": Please explain more in Details. E.g. any error message? Btw. the example has bad variable naming conventions, but I think that is not the main Problem at the Moment.

A big thumbs up: you are here since today and you tagged the code like it should be.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You have declared your three varaible names with trailing underscore characters. However, in your constructor you use names without underscores, hence the compiler errors. In your setter methods you should use the name value as this is the implied name of the parameter, thus:
public string lastName
{
    get
    {
        return lastName_;
    }
    set
    {
        lastName_ = value;
    }
}   //end lastname string get set

A minor point but properties, by convention, have names which begin with an upper case character, e.g. LastName rather than lastName.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Concerning your properties' setters, you are using undeclared variables (namely, first, last and mSalary). Replace all these undeclared variables in setters with the variable value and you should get rid of three compiling issues.

For the rest, I cannot imagine your teacher giving you an exercise without having exposed the key concepts to use for the exercise. You should ask your teacher if you have difficulties applying the course material to your exercise.

Kindly.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

public class Employee
{
    public Employee(string first, string last, double mSalary)
    {
        FirstName = first;
        LastName = last;
        MonthlySalary = mSalary;
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public double MonthlySalary { get; set; }

    public double YearlySalary { get { return MonthlySalary * 12; } }

    public override string ToString()
    {
        return string.Format("Last name is {0}, First name is {1}, with a Yearly salary of {2}", FirstName, LastName, YearlySalary);
    }
}

public class Employeetest
{
    public static void Main()
    {
        Employee E1 = new Employee("John", "Doe", 3000.00);
        Employee E2 = new Employee("Jane", "Doe", 4000.00);

        //display information
        System.Console.WriteLine(E1.ToString());
        System.Console.WriteLine(E2.ToString());
    }
}
   
v2
Comments
Member 14115626 12-Jan-19 11:08am
   
Thank you! If I'd like to multiply Monthly Salary by 12 so I can get the yearly salary, where do I put the * 12 multiplier?
RickZeeland 12-Jan-19 12:19pm
   
See the updated solution :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190306.1 | Last Updated 12 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100