Here is the requirements:
create a class include first name, last name, and monthly salary variables.
Create a set and get method for each instance variable.
What is the set method for(I read the book several times)? I don't quite get how to use it. How to create a set method for those three variable? When I executive the Employeetest, the year salary became 660.00000. How can I just limit it to 2 decimals?
Thank you so much.

class Employee

{
	private String firstName;
	private String lastName;
	private double monthlySalary;
	
	
	public Employee( String f, String l, double m )
	{
		firstName = f;
		lastName = l;
		monthlySalary = m;	
	}
	
	public String getFirstName()
	{
		return firstName;
	}
	
		public String getLastName()
	{
		return lastName;
	}
	
		public double getMonthlySalary()
	{
		return monthlySalary;
	}
	
	@Override
	public String toString()
	{
		return getFirstName() + getLastName();
	}
	

}
public class EmployeeTest
{
	public static void main(String[] args)
	{
		Employee employee1 = new Employee("Eric", "Jackson", 100);
		Employee employee2 = new Employee("Zerk", "Wiz", 50);
	
		System.out.printf("%s %s \n", employee1.getFirstName(), employee1.getLastName() );
		
		System.out.printf( "yearly salary is %f \n", employee1.getMonthlySalary()*12);
		
		System.out.printf("After 10 percent raising, the year salary is %f \n",employee1.getMonthlySalary()*12 * 1.1);
		
		System.out.printf("%s %s \n", employee2.getFirstName(), employee2.getLastName() );
		
		System.out.printf("yearly salary is %f \n", employee2.getMonthlySalary()*12);
		
		System.out.printf("After 10 percent raising, the year salary is %f",employee2.getMonthlySalary()*12 * 1.1);
	
	}

Recommended Answers

All 5 Replies

Set method or so often setter method is basically opposite of get method. Where in get method you are getting value of the variable with setter you do set it up. It is also conventional way how to provide access/option to change variable value without making this variable public.
So your following code

public Employee( String f, String l, double m )
	{
		firstName = f;
		lastName = l;
		monthlySalary = m;	
	}
	
	public String getFirstName()
	{
		return firstName;
	}

can be changed to

public Employee( String f, String l, double m )
	{
		setFirstName(f);
		setLastName(l);
		setMonthlySalary(m);	
	}
	
	public String getFirstName()
	{
		return firstName;
	}

	public void setFirstName(String f)
	{
		this.firstName = firstName;
        }

Where the other set methods follow above example

This process is also know as encapsulation, you can read more on it from Sun Certified Programmer for Java book pages 86-89

What is the difference after I change my code to

public Employee( String f, String l, double m )
	{
		setFirstName(f);
		setLastName(l);
		setMonthlySalary(m);
        }

By the way, when I run the Employee test, my decimal is like 660.000000, how to change decimal place into 2?

What is the difference after I change my code to

Did you read linked material? Put it simple, since you have setter methods it would be silly not to use them and instead do another direct assign. Doing such thing is also called "code duplication". {Want t learn more? Read Refactoring: Improving the Design of Existing Code}

By the way, when I run the Employee test, my decimal is like 660.000000, how to change decimal place into 2?

As for this you need to rad something on formatting. Like in your case DecimalFormat

Sorry for the decimal problem. I type something wrong. No wonder it keeps display the error message. About the setter method, I think I kind of understand the setter method now. After I use the set method, I can get rid off the

#
@Override
#
public String toString()
#
{
#
return getFirstName() + getLastName();

So, it won't display some weird memory location. It is quite useful. Thank you so much.

Not necessary. toString() method is a good way how to display data inside the object you failed to process and are often used when passing such object to loggers to record any errors/warning/info or other activities supported by logger in the application happening in application life time.
However this is sort of stuff you will learn when you start working or perhaps participating in an open source projects before your carrier

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.