Results 1 to 7 of 7
  1. #1
    Schaput is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default Inheritance Program

    I need to write a class titled employee. This class needs to hold data for an employee's name, ID#, and their date of hire. Then i need to write a class called production worker, that extends employee. this class will hold weather they work shift 1, or 2, and their pay rate.
    After that i need to write a class called teamleader that extends production worker and this is going to hold how many training hours are needed to be a team leader, how many the employee has, and how much of a bonus a team leader gets.
    This all needs to be outputted in the main program.

    Also, i need to set 'get' and 'set methods in the program.

    Sorry if i am not very clear with what i am trying to accomplish, its probably because i am completely lost with what i have to do.
    Thanks for the help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    First off, many here don't take too well to folks who dump the entire assignment, without showing any work as many will assume that the poster wants someone else to do the assignment for them. I will assume that this isn't you, that you don't want someone to do it all for you.

    Secondly, our track record for helping folks who state they are "completely lost" is poor to dismal as there is only so much a forum can do in this situation. We can't put knowledge in your head; only you can do that. Often the best we can do is to point you to the most basic tutorials online and suggest that you start reading. Also, most in this situation are far better off going to their teacher and asking for some one-on-one face to face help.

    That being said, perhaps we can help you if you have some basic knowledge here, enough to understand what we are suggesting, and perseverance. The first thing you need to do is to try to create this on your own as best you can, then come back with your code and with specific questions (not the "I'm completely lost" kind) that are answerable in the limited format of a Java forum. By posting your code, we can have a better idea of just where you're making bad assumptions or what Java subjects you need to read up on in the tutorials.

    I wish you much luck.
    Last edited by Fubarable; 11-13-2008 at 10:59 PM.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Your best bet would be to draw diagrams of your classes and methods and how they work with one another. If you haven't learned enough to do this effectively, most people on the boards would advise you to read more material and get help from a tutor or instructor.

  4. #4
    Schaput is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    I have a program written, apologies if i made it sound like i was trying to leech off everyone here. Sadly, i realize i am not able to access these forums on my test so would much rather learn for myself, :) haha.
    Anyways, i have 3 classes written as described in my first post. However, none of these classes use the 'extends' command. Given i get the same results as far as i can tell, its not the way my professor would like me to get this done.

    Java Code:
    public class employee {
    
    
    
     public static void main(String[] args) {
      
      productionWorker a = new productionWorker();
      teamleader b = new teamleader();
      a.setName("Steve");
      a.setId("XXX-X");
      a.setDate("10/10/2008");
      b.setReq(20);
      b.setCom(22);
      b.setBonus(2500.0);
      
    
     
      System.out.println("Employee Name: " + a.getName());
       System.out.println("Employee ID: " + a.getId());
       System.out.println("Employee Hired: " + a.getDate());
       
       System.out.println("Required Training Hours: " + b.getReq());
       System.out.println("Completed Training Hours: " + b.getCom());
       if ((b.getReq()) > (b.getCom()))
             {
         System.out.println(a.getName() + " does not get a bonus this year");
       }else{
              System.out.println(a.getName() + " does get a bonus this year worth, "+ b.getBonus() + " dollars.");
    }
    
    }
    }
    Java Code:
    public class productionWorker {
    
     private String name;
     private String idnum;
     private String dateHired;
     
     
     public void setName(String n){
      name = n;
     }
     public String getName(){
      return name;
     }
     public void setId(String id){
       idnum = id;
     }
     public String getId(){
       return idnum;
     }
     public void setDate(String date){
       dateHired = date;
     }
     public String getDate(){
       return dateHired;
     }
       
    }
    Java Code:
    public class teamleader{
      
    
    private int req;
     private int com;
    private double bonus;
     
     
     public void setReq(int r){
      req = r;
     }
     public int getReq(){
      return req;
     }
     
     public void setCom(int c){
      com = c;
     }
     public int getCom(){
      return com;
     }
     
     public void setBonus(double b){
      bonus = b;
     }
     public double getBonus(){
      return bonus;
     }
     
     }
    I guess i just dont understand why my code isnt good enough, haha. How would i turn there three parts of code into 3 sets of code using inheritance? Thanks.

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Have you been taught the "has a" and "is a" relationships? inheritence uses "is a". Think of it this way. A german shepard IS A dog and a dog IS A mammal. so you would have something like

    class german shepard() extends dog
    .....
    class dog() extends mammal
    ....
    class mammal()

    You need to figure out the hierarchy of your classes and how they relate to each other and then figure out things they all share(like name, id, etc)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Have you been taught the "has a" and "is a" relationships?...
    Very valid points.
    You need to figure out the hierarchy of your classes and how they relate to each other and then figure out things they all share(like name, id, etc)
    This I think was already spelled out in detail by the instructor in the assignment as written in the original post.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, one thing at a time. Let's just start with the employee class (which should be spelled "Employee"). The requirements are:
    This class needs to hold data for an employee's name, ID#, and their date of hire.
    You've got this:
    Java Code:
    public class employee
    {
    
      public static void main(String[] args)
      {
        productionWorker a = new productionWorker();
        teamleader b = new teamleader();
        a.setName("Steve");
        a.setId("XXX-X");
        a.setDate("10/10/2008");
        b.setReq(20);
        b.setCom(22);
        b.setBonus(2500.0);
    
        System.out.println("Employee Name: " + a.getName());
        System.out.println("Employee ID: " + a.getId());
        System.out.println("Employee Hired: " + a.getDate());
    
        System.out.println("Required Training Hours: " + b.getReq());
        System.out.println("Completed Training Hours: " + b.getCom());
        if ((b.getReq()) > (b.getCom()))
        {
          System.out.println(a.getName() + " does not get a bonus this year");
        }
        else
        {
          System.out.println(a.getName() + " does get a bonus this year worth, "
              + b.getBonus() + " dollars.");
        }
      }
    }
    but this isn't a "real" class but more of a driver program, a program that runs other classes. Suggestions:

    Get rid of the main method and in fact get rid of all of this. Understand that Employee will not have a main method. It needs fields, variables to hold name, ID#, and date of hire. So I recommend that you start over here and just add variables that can hold these quantities and post it back here. Without a decent Employee class, the whole project is hosed.

Similar Threads

  1. inheritance basic.
    By newmember in forum New To Java
    Replies: 1
    Last Post: 08-02-2008, 10:35 PM
  2. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 07:08 PM
  3. help with inheritance in hibernate
    By valery in forum JDBC
    Replies: 1
    Last Post: 08-06-2007, 09:44 PM
  4. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 01:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •