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 2 of 2

Thread: Java - Inheritance help!

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java - Inheritance help!

    Hi, can I please get some help with the following task. I've started the coding, although I have some of the methods I'm not sure on how to do (i.e the toString and equals)

    The task and other provided are below.
    Task.doc

    public class Employee
    {
        private String name;
     
        public Employee()
        {
     
        }
     
        public Employee(String employeeName)
        {
            name = employeeName;
        }
     
        public Employee(Employee Employee)
        {
     
        }
     
        public String getName()
        {
            return name;
        }
     
        public void setName(String employeeName)
        {
            this.name = employeeName;
        }
     
        public String toString()
        {   
             return name;
        }
     
        public boolean equals(Employee Object)
        {
     
        }
    }

    public class HourlyEmployee extends Employee
    {
        private double wageRate;
        private double hours;
     
        public HourlyEmployee()
        {
     
        }
     
        public HourlyEmployee(String name, double employeeWageRate, double employeeHours)
        {
            super(name);
            wageRate = employeeWageRate;
            hours = employeeHours;
        }
     
        public HourlyEmployee(Employee HourlyEmployee)
        {
     
        }
     
        public double getRate()
        {
            return wageRate;        
        }
     
        public double getHours()
        {
            return hours;
        }
     
        public double getPay()
        {
     
            return wageRate*hours;
        }
     
        public void setHours(double employeeHours)
        {
            this.hours = employeeHours;
        }
     
        public void setRate(double employeeWageRate)
        {
            this.wageRate = employeeWageRate;
        }
     
        public String toString()
        {
            return "Sam\n$50.5 per hour for 40.0 hours\n";
        }
     
    //     public boolean equals(Object)
    //     {
    //         
    //     }    
    }

    public class SalariedEmployee extends Employee
    {
        private double salary;
        public SalariedEmployee()
        {
     
        }
     
        public SalariedEmployee(String name, double employeeSalary)
        {
            super(name);
            salary = employeeSalary;
        }
     
        public SalariedEmployee(Employee SalariedEmployee)
        {
     
        }
     
        public double getSalary()
        {
            return salary;
        }
     
    //     public double getPay()
    //     {
    //         return ;
    //     }
     
        public void setSalary(double employeeSalary)
        {
            this.salary = employeeSalary;
        }
     
    //     public String toString()
    //     {
    //         return 
    //     }
     
    //     public boolean equals(Object Employee)
    //     {
    //         
    //     }
    }

    public class EmployeeDemo
    {
        public static void main(String[] args)
        {
            ArrayList<Employee> emplist = new ArrayList<Employee>();
            emplist.add(new SalariedEmployee("Josephine", 100000));
            emplist.add(new HourlyEmployee("Sam",50.50, 40.0));
     
            //this loop shows you how to call a public method in a superclass that all subclasses inherit from.   
            //this also shows how to call methods of individual sub classes.
            for(Employee aEmp: emplist)
            {
                System.out.println(aEmp.getName());
                    if(aEmp instanceof SalariedEmployee)
                    {
                        System.out.println("$" + ((SalariedEmployee) aEmp).getSalary() + " per year");
                    }
                    else if(aEmp instanceof HourlyEmployee)
                    {
                        System.out.print("$" + ((HourlyEmployee) aEmp).getRate() + " per hour for ");
                        System.out.println(((HourlyEmployee) aEmp).getHours() + " hours");
                    }
                    System.out.println();
            }
            //the following will produce the same output as above.
            //Note that System.out.println() calls the toString() method initially defined in class Object but
            //overridden by SalariedEmployee and HourlyEmployee classes.
            for(Employee aEmp: emplist)
            {
                System.out.println(aEmp +"\n");
            }
        }
    }
    Attached Images Attached Images


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java - Inheritance help!

    Way too much code. This is what we need:

    - a description of what the toString() method should do
    - your current code for the toString() method
    - any questions or problems you're having with code you've posted

Similar Threads

  1. java - inheritance
    By Shreyas123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 10th, 2013, 02:06 AM
  2. java inheritance
    By farheen_zafar1 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 24th, 2013, 01:58 PM
  3. Java Inheritance problem
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2013, 07:34 PM
  4. [SOLVED] Java inheritance
    By maple1100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 10:42 PM
  5. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM

Tags for this Thread