"Write the definition for a new class named Employee that stores the following data for a single employee:
* The name of the employee (a String)
* The name of the employee’s supervisor (also a String)
* The employee’s annual salary (a double)
As with the other classes defined in this book, you should make sure that the instance variables containing these values are private and instead provide get and set methods to retrieve or change any of the values." (Roberts ch 6, problem 8)
/* File: Employee * ---------------------------- * Class to store information about employees, retrieve that information, and reset the information. * How you'd use this class: * a) Enter in a new employee, ie " emp = new Employee('Bob Cratchit', 'Ebenezer Scrooge', 25.00) " * b) get information, ie " name = emp.getName() " * c) set information, ie " emp.setSupervisor('Mickey Mouse') " */ public class Employee { public Employee (String name, String supervisor, double salary) { //LOCAL variables. Exist just for the run of this constructor method. The client passes name/suvervisor/salary information // in initially and it immediately gets used to set the instnace variable value. employeeName = name; supervisorName = supervisor; employeeSalary = salary; } //Getter methods: public String getName() { return employeeName; } public String getSupervisor() { return supervisorName; } public double getSalary() { return employeeSalary; } //Setter methods: These have no return type, they just reset the instance variables public void setName(String newName) { employeeName = newName; } public void setSupervisor(String newSupervisor) { supervisorName = newSupervisor; } public void setSalary(double newSalary) { employeeSalary = newSalary; } // INSTANCE variables: Stuff that we're keeping track of for the lifetime of the object. // It gets set using the temp (aka local) variables in the constructor private String employeeName; private String supervisorName; private double employeeSalary; }This chapter in the book gets students started with writing their own classes. The problem was very simple, but it's good refresher on how objects work see the difference between instance variables and local variables in action.
It took me a while to get used to the fact that each piece of information is represented 3 times:
* In the constructor method, the client of this class creates a new instance of the "Employee" class and sets the initial name, supervisor and salary values. These are passed through as local variables that exist just for the life of the constructor method and instantly get stored to the instance variable.
* Instance variables that store the name, supervisor and salary values for the lifetime of the object.
* The getter method returns the name/supervisor/salary information from the current value of the instance variable, so clients of the class can store the information in a variable of their own. The setter methods update the instance variable based on new values that the client of the class passes (using a local value that exists just for the life of the setter method).
Once I got a firm hold on why name/supervisor/salary information is distinct when represented in the local variables versus the instance variables, I got a better understanding of how classes work and how you'd write a class that other programs can use.
Very energetic blog, I enjoyed tyat a lot. Will there be a part 2?
ReplyDeleteHere is my website :: sex
h6f00i0f23 t7i62w9v81 f0o91w8v66 t7j77f9p16 v4x41x5a24 f8k19x0i92
ReplyDelete