




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





// 1. Data available directly. Discouraged! public class Employee { public String name; public double htInch; } // Using the Employee class Employee e1 = new Employee(); e1.name="Joe"; e1.htInch=72.5; System.out.println(e1.name+" is "+e1.htInch+" inches tall."); // 2. Data available through reader/writer methods only. Encouraged! public class Employee { private String name; private double htInch; // Readers (getters) public String getName(){return name;} public double getHtInch() {return htInch;} // Writers (setters) public void setName(String n){name=n;} public void setHtInch(double h) {htInch=h;} } // Using the Employee class Employee e1 = new Employee(); e1.setName("Joe"); e1.setHtInch(72.5); System.out.println(e1.getName()+" is "+e1.getHtInch()+" inches tall."); // 3. Add centimeter height. Data available directly. DANGER! public class Employee { public String name; public double htInch; public double htCm; } // Using the Employee class Employee e1 = new Employee(); e1.name="Joe"; e1.htInch=72.5; e1.htCm=e1.htInch * 2.54; System.out.println(e1.name+" is "+e1.htInch+" inches tall, and "+ e1.htCm+" cm tall."); // Allows inconsistency! e1.htInch=72.5; e1.htCm=50; // 4. Avoid inconsistency by making data private and using reader and (^1) Many of the course documentation standards are violated in these examples (e.g. no comments; multiple statements on a line). This is to save paper. Remember, when in doubt, do as I say, not as I do.
// writer methods. public class Employee { private String name; private double htInch; private double htCm; // Readers public String getName() {return name;} public double getHtInch(){return htInch;} public double getHtCm() {return htCm;} // Writers (assures consistency) public void setName(String n) {name=n;} public void setHtInch(double h){htInch=h; htCm=h2.54;} public void setHtCm(double h) {htCm=h; htInch=h/2.54;} } // Using the Employee class. No inconsistency possible. Employee e1 = new Employee(); e1.setName("Joe"); e1.setHtInch(72.5); System.out.println(e1.getName()+" is "+e1.getHtInch()+ " inches tall, and "+e1.getHtCm()+" cm tall."); // 5. Alternate version of 4. How is it different? What are the // tradeoffs? Would someone using this class or the previous class // notice any difference? public class Employee { private String name; private double htInch; // Readers public String getName() {return name;} public double getHtInch(){return htInch;} public double getHtCm() {return htInch2.54;} // Writers public void setName(String n) {name=n;} public void setHtInch(double h){htInch=h;} public void setHtCm(double h) {htInch=h/2.54;} } // Using the Employee class. No inconsistency possible. Employee e1 = new Employee(); e1.setName("Joe"); e1.setHtInch(72.5); System.out.println(e1.getName()+" is "+e1.getHtInch()+ " inches tall, and "+e1.getHtCm()+" cm tall.");
// 8. Creating an array of Employee objects. // Create the array of 10 references. Employee[] eList = new Employee[10]; // Create the 10 Employee objects; default values. for (int i=0; i<eList.length; i++) eList[i] = new Employee(); // Set two of the Employee objects. eList[0].setName("Joe"); eList[0].setHtInch(72.5); eList[1].setName("Sue"); eList[1].setHtCm(125); // 9. Adding static data and method to keep track of the number // of Employee objects created. public class Employee { private String name; private double htInch; // Constructors. Note each increases employee count. public Employee() {empCount++; name="No name"; htInch=0.0;} public Employee(String n, double h) {empCount++; name=n; htInch=h;} private static int empCount=0; public static int getEmpCount() {return empCount;} // Readers public String getName() {return name;} public double getHtInch(){return htInch;} public double getHtCm() {return htInch*2.54;} // Writers public void setName(String n) {name=n;} public void setHtInch(double h){htInch=h;} public void setHtCm(double h) {htInch=h/2.54;} } // Using the static information. This works no matter how many // Employee objects have been created – even zero. System.out.println("There have been "+Employee.getEmpCount()+ " employees created so far. "); Employee e1 = new Employee(); System.out.println("There have been "+Employee.getEmpCount()+ " employees created so far. ");
// 10. Adding a toString() method. public class Employee { private String name; private double htInch; // Constructors public Employee() {empCount++; name="No name"; htInch=0.0;} public Employee(String n, double h) {empCount++; name=n; htInch=h;} private static int empCount=0; public static int getEmpCount();{return empCount;} // Readers public String getName() {return name;} public double getHtInch(){return htInch;} public double getHtCm() {return htInch*2.54;} // Writers public void setName(String n) {name=n;} public void setHtInch(double h){htInch=h;} public void setHtCm(double h) {htInch=h/2.54;} // toString public String toString() { return name+"\’s height is "+htInch+" inches or "+getHtCm()+" cm. ";} } // Using toString(). Employee e1 = new Employee("Joe", 72.5); System.out.println(e1); // Automatic call to e1.toString(). // 11. Creating garbage. Employee e1 = new Employee("Joe", 72.5); e1 = new Employee("Sue", 65.5); // Joe’s object is now garbage; there are no references pointing at // it. But not to worry. Java’s automatic garbage collector will // periodically reclaim garbage.
// 13. Creating a new class by inheriting Employee. public class SalEmployee extends Employee { // Salary private double salary; // Constructor uses parent constructor. public SalEmployee(String n, double h, double s) { super(n,h); // Call parent constructor for name and height. salary = s; } // Default constructor public SalEmployee() { super(); // Call default constructor of parent. salary = 0.0; } // Salary reader public double getSalary(){return salary;} // Salary writer public void setSalary(double s){salary = s;} // toString. Note use of parent methods. public String toString() { return getName() + "\’s height is " + getHtInch() + " inches, and salary is " + salary; } } // Using the SalEmployee class SalEmployee s1 = new SalEmployee("William", 72, 125000); System.out.println(s1); // Reset name and salary. s1.setName("Bill"); s1.setSalary(200000); System.out.println(s1);
// 14. Polymorphism // Create an Employee array Employee[] eList = new Employee[5]; // Fill in the array with Employee and SalEmployee objects. // Note that it is ok to assign a child object to a parent reference. eList[0] = new Employee("Larry", 62); eList[1] = new SalEmployee("Bud", 65, 15000); eList[2] = new Employee("Curly", 67); eList[3] = new SalEmployee("Lou", 70,25000); eList[4] = new Employee("Moe", 66); // Appropriate method gets called depending on type of object. for (int i=0; i<eList.length(); i++) System.out.println(eList[i]);