Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Inheritence and Polymorphism

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Inheritence and Polymorphism

    I am to create a program that works as follows:
    1. Write a java class (Employee.java) that encapsulates an employees first name, last name, SSN. Implement the apporpriate get and set methods along with toString() methods. Implement a method called getEarnings() which simply returns the value 0.0

    Employee.java
    public class Employee
    {
        // instance variables
        protected String fName;
        protected String lName;
        protected double ssn;
        protected double earnings;
     
        public Employee()
        {
            earnings = 0.0;
        }
        public Employee(String first, String last, double socialSec)
        {
            fName = first;
            lName = last;
            ssn = socialSec;
        }
        public void setFirstName(String f_Name)
        {
            fName = f_Name;
        }
        public String getFirstName()
        {
            return fName;
        }
        public void setLastName(String l_Name)
        {
            lName = l_Name;
        }
        public String getLastName()
        {
            return lName;
        }
        public void setSsn(double social)
        {
            ssn = social;
        }
        public double getSsn()
        {
            return ssn;
        }
        public double getEarnings()
        {
            return earnings;
        }
        public String toString()
        {
            return ("The employees name is " + getFirstName() + " " + getLastName() + ". The employees social security number is " + getSsn());
        }
    }

    2. Create a java class (SalariedEmployee.java) which is a special type of Employee with the following additional attribute: weeklySalary. Provide appropriate get, set, and toString() methods. Implement a new version of getEarnings() mehtod that returns the earnings of the salaried Employee.

    SalariedEmployee.java
    public class SalariedEmployee extends Employee
    {
        private double weeklySalary;
     
        public SalariedEmployee()
        {
            super();
            earnings = 0.0;
        }
        public SalariedEmployee(double weeklySal)
        {
            super();
            if(weeklySal > 0)
                weeklySalary = weeklySal;
            else
                weeklySalary = 0;
        }
        public void setWeeklySalary(double salary)
        {
            if(salary > 0)
                weeklySalary = salary;
            else
                weeklySalary = 0;
        }
        public double getWeeklySalary()
        {
            return weeklySalary;
        }
        public double getEarnings(double numberWeeks)
        {
            if(numberWeeks>0)
                earnings = weeklySalary*numberWeeks;
            else
                earnings = 0;
            return earnings;
        }
        public String toString()
        {
            return super.toString() + ". The weekly salary is " + getWeeklySalary();
        }
    }

    3. Create another java class (hiredEmployee.java) which is another special type of Employee with the additional attributes: wage and hours. Provide appropriate set and get methods along with toString() method. Also implement a getEarnings() mehod that reflects earning of HiredEmployee.

    HiredEmployee.java:
    public class HiredEmployee extends Employee
    {
        private double wage;
        private double hours;
     
        public HiredEmployee()
        {
            super();
            wage = 0.0;
            hours = 0;
        }
        public HiredEmployee(double w, double h)
        {
            super();
            if(w > 0 && h > 0)
            {
                wage = w;
                hours = h;
            }
            else
            {
                wage = 0;
                hours = 0;
            }
        }
        public void setWage(double nWage)
        {
            if(nWage > 0)
                wage = nWage;
            else
                wage = 0;
        }
        public double getWage()
        {
            return wage;
        }
        public void setHours(double nHours)
        {
            if(nHours > 0)
                hours = nHours;
            else
                hours = 0;
        }
        public double getHours()
        {
            return hours;
        }
        public double getEarnings()
        {
            earnings = wage * hours;
            return earnings;
        }
        public String toString()
        {
            return super.toString() + ". The employees wage is " + getWage() + ". The employees hours are " + getHours();
        }
    }

    4. Create a java class (CommissionEmployee.java) with additional attributes: grossSales and commissionRate. Provide get, set, and toString() mehtods. Provide a newer version of getEarnings() method that returns the earnings.

    CommissionEmployee.java
    public class CommissionEmployee extends Employee
    {
        protected double grossSales;
        protected double commissionRate;
     
        public CommissionEmployee()
        {
            super();
            grossSales = 0.0;
            commissionRate = 0.0;
        }
        public CommissionEmployee(double gS, double cR)
        {
            super();
            if(gS > 0 && cR > 0)
            {
                 grossSales = gS;
                 commissionRate = cR;
            }
            else
            {
                grossSales = 0;
                commissionRate = 0;
            }
        }
        public void setGrossSales(double nGS)
        {
            if(nGS > 0)
                grossSales = nGS;
            else
                grossSales = 0;
        }
        public double getGrossSales()
        {
            return grossSales;
        }
        public void setCommissionRate(double nCS)
        {
             if(nCS > 0)   
                commissionRate = nCS;
             else
                commissionRate = 0;
        }
        public double getCommissionRate()
        {
            return commissionRate;
        }
        public String toString()
        {
            return super.toString() + ". The employees gross sales are " + getGrossSales() + ". The employees commission rate is " + getCommissionRate();
        }
        public double getEarnings()
        {
            earnings = commissionRate * grossSales;
            return earnings;
        }
    }

    5. Implement a specialized type of CommissionEmployee (BasePlusCommissionEmployee) which has an additional attribute: base salary. Proved get, set, and toString() methods. Implement the getEarnings() mehtod that returns the earnings.

    BasePlusCommissionEmployee.java
    public class BasePlusCommissionEmployee extends CommissionEmployee
    {
        private double baseSalary;
     
        public BasePlusCommissionEmployee()
        {
            super();
            baseSalary = 0.0;
        }
        public BasePlusCommissionEmployee(double bS)
        {
            super();
            if(bS > 0)
                baseSalary = bS;
            else 
                baseSalary = 0;
        }
        public void setBaseSalary(double base)
        {
            if(base>0)
                baseSalary = base;
            else
                baseSalary = 0;
        }
        public double getBaseSalary()
        {
            return baseSalary;
        }
        public double getEarnings()
        {
            earnings = commissionRate * grossSales + baseSalary;
            return earnings;
        }
        public String toString()
        {
            return super.toString() + ". The employees base salary is " + getBaseSalary();
        }
    }


    Here's the part I am stuck on:
    Write a client program with a static method called generateEmployees() that returns a random list of 10 different types of Employee objects. You could use an array or ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. I want to populate the each element of the array with an integer 1-4 each representing an Employee class. Then after determining which is which, the method will generate random data to put into the employee object that is created. As these items are generated, add them to the data structure(array that I'm using). The method should return this data.
    In the same application class, I am to implement the main() method. Then call the generateEmployees() static method and using a for loop print the details of each of the employees along with their earning on the terminal window.

    Here's what I have so far for my client program:
    public class EmployeeTest
    {
        public static void generateEmployees()
        {
            int[] employed = new int[10];
            for(int i=0; i<employed.length; i++)
            {
                employed[i] = (int)Math.random();
                if(employed[i] < 1 || employed[i] > 4)
                    employed[i] = (int)Math.random();
            }
            System.out.println(employed[0]);
            System.out.println(employed[1]);
            System.out.println(employed[2]);
            System.out.println(employed[3]);
            System.out.println(employed[4]);
            System.out.println(employed[5]);
            System.out.println(employed[6]);
            System.out.println(employed[7]);
            System.out.println(employed[8]);
            System.out.println(employed[9]);
        }
     
        public static void main(String[] args)
        {
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Inheritence and Polymorphism

    First thing I see:
    generateEmployees() that returns a random list
    The method returns a list so it should not be void.
    use an array or ArrayList to store the employee objects
    Your choice of what to fill and return.

    One way to randomly call different constructors to generate objects would be to use a switch statement and have each case of the switch generate one type of object. Use an random number with the switch to select which object generating code to call.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Inheritence and Polymorphism

    Math.random() returns a double between 0 and 1, java has a class Random that might be more appropriate for your use. Try looking at its javadoc.

    Rolf

Similar Threads

  1. Inheritence and problems with static methods.
    By Sylis in forum What's Wrong With My Code?
    Replies: 15
    Last Post: November 4th, 2012, 01:13 AM
  2. Polymorphism.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2012, 09:28 AM
  3. Problem with inheritence? arguments not applicable
    By rbk in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2011, 09:37 AM
  4. Polymorphism test
    By speedycerv in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 07:15 AM
  5. Polymorphism issue
    By LDM91 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 02:26 PM