Page 1 of 3 123 LastLast
Results 1 to 20 of 49
  1. #1
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default calling to superclass/constructors questions.

    hey guys, im doing a lesson on Polymorphism, and i have a basic question, not really regarding polymorphism, but the basic of a constructor question.

    i have an entire group of java programs,
    Employee.java (abstract) then 4 classes
    commissionemployee.java
    salariedemployee.java
    hourlyemployee.java
    basepluscommission.java
    that all extend Employee. the program functions, shows the name, ssn, salary and i need to add functionality to also show the birthdate. i have added in the employee class here:

    Java Code:
    public abstract class Employee 
    {
       private String firstName;
       private String lastName;
       private String socialSecurityNumber;
       private String birthDate;
    
       // six-argument constructor
       public Employee( String first, String last, String ssn,
       		int month, int day, int year )
       {
          firstName = first;
          lastName = last;
          socialSecurityNumber = ssn;
          birthDate = new Date ( month, day, year);
       } // end six-argument Employee constructor
    i haven't worked with "Date" before, do i declare it like i did ok? and in one of the subclasses, how do i set up the constructor, here are 2 different ways, not sure which is right, or if either is:

    im guessing this:
    Java Code:
    public class CommissionEmployee extends Employee 
    {
       private double grossSales; // gross weekly sales
       private double commissionRate; // commission percentage
    
       // six-argument constructor
       public CommissionEmployee( String first, String last, String ssn, [COLOR="Red"]String birthDate[/COLOR], 
          double sales, double rate, )
       {
          super( first, last, ssn, [COLOR="Red"]birthDate[/COLOR] );
          setGrossSales( sales );
          setCommissionRate( rate );
          
       } // end six-argument CommissionEmployee constructor

    and not this:
    Java Code:
    public class CommissionEmployee extends Employee 
    {
       private double grossSales; // gross weekly sales
       private double commissionRate; // commission percentage
    
       // six-argument constructor
       public CommissionEmployee( String first, String last, String ssn,[COLOR="Red"] int month, int day, int year[/COLOR], 
          double sales, double rate, )
       {
          super( first, last, ssn, [COLOR="Red"]month, day, year[/COLOR] );
          setGrossSales( sales );
          setCommissionRate( rate );
          
       } // end six-argument CommissionEmployee constructor
    Last edited by hayden06f4i; 12-14-2010 at 03:48 AM.

  2. #2
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    or should it actually say "private Date birthdate;" when i declare variables?

  3. #3
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    im not sure how or where, but i will also need some sort of method for date like getMonth, getDay, getYear, right?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    birthDate should be a Date.
    I'd create a constructor that takes a Date object.

    As for the getters, you just want a getBirthDate, which returns the Date. You don't want to be worrying about display stuff in here.

    Depending on how you are creating these objects will determine how you get that Date (or how you display it), but that should not be something your model is concerned with.

  5. #5
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    there is so much code, i can't really post it all here, but there is a PayrollTest.java application that i need to modify as well, that contains the main method for everything.

    so it should be declared like this:

    private Date birthDate;

    and not:

    private String birthDate;

    ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    Well, a birthDate sounds like a Date to me.
    There's no "should be", though, since there may be a very good reason to declare it as a String. I just can't think of one straight away.

  7. #7
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    so after debugging countless errors, im trying to figure out what im missing so far here:

    Java Code:
    // Fig. 10.4: Employee.java
    // Employee abstract superclass.
    
    public abstract class Employee 
    {
       private String firstName;
       private String lastName;
       private String socialSecurityNumber;
       private Date birthDate;
    
       // six-argument constructor
       public Employee( String first, String last, String ssn,
       		int month, int day, int year )
       {
          firstName = first;
          lastName = last;
          socialSecurityNumber = ssn;
          birthDate = new Date ( month, day, year);
       } // end six-argument Employee constructor
    
       // set first name
       public void setFirstName( String first )
       {
          firstName = first; // should validate
       } // end method setFirstName
    
       // return first name
       public String getFirstName()
       {
          return firstName;
       } // end method getFirstName
    
       // set last name
       public void setLastName( String last )
       {
          lastName = last; // should validate
       } // end method setLastName
    
       // return last name
       public String getLastName()
       {
          return lastName;
       } // end method getLastName
    
       // set social security number
       public void setSocialSecurityNumber( String ssn )
       {
          socialSecurityNumber = ssn; // should validate
       } // end method setSocialSecurityNumber
    
       // return social security number
       public String getSocialSecurityNumber()
       {
          return socialSecurityNumber;
       } // end method getSocialSecurityNumber
       
       // set birth date
       public void setBirthDate( int month, int day, int year )
       {
          birthDate = new Date( month, day, year );
       } // end method setBirthDate
    
       // return birth date
       public Date getBirthDate()
       {
          return birthDate;
       } // end method getBirthDate
    
    
       // return String representation of Employee object
       @Override
          public String toString()
       {
          return String.format( "%s %s\n%s: %s\n%s: %s", 
             getFirstName(), getLastName(), 
             "social security number", getSocialSecurityNumber(), 
             "birth date", getBirthDate() );
       } // end method toString
    
    
       // abstract method overridden by concrete subclasses
       public abstract double earnings(); // no implementation here
    } // end abstract class Employee
    im getting these errors:

    Java Code:
    c:\SimplyJava\PayrollSystem10.8>javac *.java
    Employee.java:9: cannot find symbol
    symbol  : class Date
    location: class Employee
       private Date birthDate;
               ^
    Employee.java:64: cannot find symbol
    symbol  : class Date
    location: class Employee
       public Date getBirthDate()
              ^
    Employee.java:18: cannot find symbol
    symbol  : class Date
    location: class Employee
          birthDate = new Date ( month, day, year);
                          ^
    Employee.java:60: cannot find symbol
    symbol  : class Date
    location: class Employee
          birthDate = new Date( month, day, year );
                          ^
    4 errors

    how exactly do i use "Date"? do i have to import it?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    Yes, you have to import it.
    The API is also quite handy.

    I did suggest passing a Date object into the constructor as well.

  9. #9
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    i added this code into my Employee.java file, which im assuming is the equivalent to importing, only more legwork?
    Java Code:
    public class Date 
    {
       private int month; // 1-12
       private int day; // 1-31 based on month
       private int year; // any year
    
       // constructor: call checkMonth to confirm proper value for month; 
       // call checkDay to confirm proper value for day
       public Date( int theMonth, int theDay, int theYear )
       {
          month = checkMonth( theMonth ); // validate month
          year = theYear; // could validate year
          day = checkDay( theDay ); // validate day
    
       } // end Date constructor
    
       // utility method to confirm proper month value
       private int checkMonth( int testMonth )
       {
          if ( testMonth > 0 && testMonth <= 12 ) // validate month
             return testMonth;
          else // month is invalid 
          { 
             System.out.printf( 
                "Invalid month (%d) set to 1.", testMonth );
             return 1; // maintain object in consistent state
          } // end else
       } // end method checkMonth
    
       // utility method to confirm proper day value based on month and year
       private int checkDay( int testDay )
       {
          int[] daysPerMonth = 
             { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
       
          // check if day in range for month
          if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
             return testDay;
       
          // check for leap year
          if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
               ( year % 4 == 0 && year % 100 != 0 ) ) )
             return testDay;
       
          System.out.printf( "Invalid day (%d) set to 1.", testDay );
          return 1;  // maintain object in consistent state
       } // end method checkDay
       
       // return a String of the form month/day/year
       public String toString()
       { 
          return String.format( "%d/%d/%d", month, day, year ); 
       } // end method toString
    } // end class Date

    otherwise, for cleaner/clearer/reduced code, i can just import it using?

    import java.util.*;????

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    Use the proper Date class.
    One of the major things about programming in general is not reinventing the wheel.

    And import the class itself, not '*'.
    So:
    Java Code:
    import java.util.Date;

  11. #11
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Talking

    Quote Originally Posted by Tolls View Post
    Yes, you have to import it.
    The API is also quite handy.

    I did suggest passing a Date object into the constructor as well.
    Quote Originally Posted by Tolls View Post
    Use the proper Date class.
    One of the major things about programming in general is not reinventing the wheel.

    And import the class itself, not '*'.
    So:
    Java Code:
    import java.util.Date;
    in going back to the book and REreading the instructions it says to use CLASS Date of fig8.7 (study from 2 chapters ago) and that is what i put in there for code, which shows the date as requested in a format like this:

    6/22/1982

    when i did the "import" instead i get this:

    Sun Jun 22 00:00:00 EDT 1919

    so there is still that question, i have one more to add to the mix after i get this settled ;)

  12. #12
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    Did you put the Date.class and the Employee.class in the same directory?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    OK.
    You didn't say that.
    And you didn't do that in your code either.

    So create that Date class and use that.

  14. #14
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    second question:

    ok to the hierarchy of this program is this:

    Employee.java (superclass)

    and 4 subclasses that all inherit from Employee.

    and 1 "test" program that has the main method.

    i have the first portion done, now i need it, so it ask the user to "enter current month" 1 -12, and if the month entered is one of the employees birthday then they get a $100 bonus. so here is what im thinking

    i need the if statement in the PayrollTest main method right?

    the if statement should be something like reading the line the user enters converting it from string to int? and comparing that to the int month in the employees birthday?

    if ( currentMonth = currentEmployee.getMonth() )
    {

    System.out.printf(
    "earned $%,.2f\n\n", currentEmployee.earnings() "plus earned $100 bonus."
    }

    am i on the right track?

  15. #15
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    OK.
    You didn't say that.
    And you didn't do that in your code either.

    So create that Date class and use that.

    sorry it didn't strike me until i returned to reread it.....i evidently misunderstood that part in my first 75 times reading the instructions...;)

  16. #16
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    MORE DETAILS:

    here is my current cmd line output: (in black, what in red i need to add)

    Java Code:
    c:\SimplyJava\PayrollSystem10.8>javac *.java
    
    c:\SimplyJava\PayrollSystem10.8>java PayrollSystemTest
    Date object constructor for date 12/25/1968
    Date object constructor for date 11/10/1982
    Date object constructor for date 9/4/1980
    Date object constructor for date 6/22/1982
    Employees processed individually:
    
    salaried employee: John Smith
    social security number: 111-11-1111
    birth date: 12/25/1968
    weekly salary: $800.00
    earned: $800.00
    
    hourly employee: Karen Price
    social security number: 222-22-2222
    birth date: 11/10/1982
    hourly wage: $16.75; hours worked: 40.00
    earned: $670.00
    
    commission employee: Sue Jones
    social security number: 333-33-3333
    birth date: 9/4/1980
    gross sales: $10,000.00; commission rate: 0.06
    earned: $600.00
    
    base-salaried commission employee: Bob Lewis
    social security number: 444-44-4444
    birth date: 6/22/1982
    gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
    earned: $500.00
    
    [COLOR="Red"]Enter Current Month (1-12)[/COLOR]
    
    Employees processed polymorphically:
    
    salaried employee: John Smith
    social security number: 111-11-1111
    birth date: 12/25/1968
    weekly salary: $800.00
    earned $800.00 [COLOR="Red"]plus $100 birthday bonus[/COLOR]
    
    hourly employee: Karen Price
    social security number: 222-22-2222
    birth date: 11/10/1982
    hourly wage: $16.75; hours worked: 40.00
    earned $670.00
    
    commission employee: Sue Jones
    social security number: 333-33-3333
    birth date: 9/4/1980
    gross sales: $10,000.00; commission rate: 0.06
    earned $600.00
    
    base-salaried commission employee: Bob Lewis
    social security number: 444-44-4444
    birth date: 6/22/1982
    gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
    new base salary with 10% increase is: $330.00
    earned $530.00
    
    Employee 0 is a SalariedEmployee
    Employee 1 is a HourlyEmployee
    Employee 2 is a CommissionEmployee
    Employee 3 is a BasePlusCommissionEmployee
    
    c:\SimplyJava\PayrollSystem10.8>

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    Quote Originally Posted by hayden06f4i View Post
    second question:

    ok to the hierarchy of this program is this:

    Employee.java (superclass)

    and 4 subclasses that all inherit from Employee.

    and 1 "test" program that has the main method.

    i have the first portion done, now i need it, so it ask the user to "enter current month" 1 -12, and if the month entered is one of the employees birthday then they get a $100 bonus. so here is what im thinking

    i need the if statement in the PayrollTest main method right?

    the if statement should be something like reading the line the user enters converting it from string to int? and comparing that to the int month in the employees birthday?

    if ( currentMonth = currentEmployee.getMonth() )
    {

    System.out.printf(
    "earned $%,.2f\n\n", currentEmployee.earnings() "plus earned $100 bonus."
    }

    am i on the right track?
    Pretty much, though I would do this in its own method rather than trying to squeeze this logic into main(). Then call it from main() (or wherever).

  18. #18
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    does you statement hold true even after seeing my output post above?

  19. #19
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Pretty much, though I would do this in its own method rather than trying to squeeze this logic into main(). Then call it from main() (or wherever).
    first for simplicity sake let me try to put it in main() so that i have the reasoning correct, here is my code (red portions are modifications that are what im working on)

    Java Code:
    // Fig. 10.9: PayrollSystemTest.java
    // Employee hierarchy test program.
    [COLOR="Red"]import java.util.Scanner; //program uses Scanner[/COLOR]
    
    public class PayrollSystemTest 
    {
       public static void main( String[] args ) 
       {
          // create subclass objects
          SalariedEmployee salariedEmployee = 
             new SalariedEmployee( "John", "Smith", "111-11-1111", 12,25,1968, 800.00 );
          HourlyEmployee hourlyEmployee = 
             new HourlyEmployee( "Karen", "Price", "222-22-2222", 11,10,1982, 16.75, 40 );
          CommissionEmployee commissionEmployee = 
             new CommissionEmployee( 
             "Sue", "Jones", "333-33-3333", 9,4,1980, 10000, .06 );
          BasePlusCommissionEmployee basePlusCommissionEmployee = 
             new BasePlusCommissionEmployee( 
             "Bob", "Lewis", "444-44-4444", 6,22,1982, 5000, .04, 300 );
    
          System.out.println( "Employees processed individually:\n" );
          
          System.out.printf( "%s\n%s: $%,.2f\n\n", 
             salariedEmployee, "earned", salariedEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n",
             hourlyEmployee, "earned", hourlyEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n",
             commissionEmployee, "earned", commissionEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n", 
             basePlusCommissionEmployee, 
             "earned", basePlusCommissionEmployee.earnings() );
    
          // create four-element Employee array
          Employee[] employees = new Employee[ 4 ]; 
    
          // initialize array with Employees
          employees[ 0 ] = salariedEmployee;
          employees[ 1 ] = hourlyEmployee;
          employees[ 2 ] = commissionEmployee; 
          employees[ 3 ] = basePlusCommissionEmployee;
          
          [COLOR="Red"]System.out.println ( "Enter the current month ( 1 -12 ):" );
          
          // create scanner to obtain input from user
    		Scanner input = new Scanner ( System.in );
    	
    	  int currentMonth; // current month input by user
    	  
    	  currentMonth = input.nextInt ();
    	  
    	  if ( currentMonth == employees.getMonth() )
    	  {[/COLOR]
          System.out.println( "Employees processed polymorphically:\n" );
          
          // generically process each element in array employees
          for ( Employee currentEmployee : employees ) 
          {
             System.out.println( currentEmployee ); // invokes toString
    
             // determine whether element is a BasePlusCommissionEmployee
             if ( currentEmployee instanceof BasePlusCommissionEmployee ) 
             {
                // downcast Employee reference to 
                // BasePlusCommissionEmployee reference
                BasePlusCommissionEmployee employee = 
                   ( BasePlusCommissionEmployee ) currentEmployee;
    
                employee.setBaseSalary( 1.10 * employee.getBaseSalary() );
    
                System.out.printf( 
                   "new base salary with 10%% increase is: $%,.2f\n",
                   employee.getBaseSalary() );
             } // end if
    
             System.out.printf( 
                "earned $%,.2f\n\n", currentEmployee.earnings() );
             [COLOR="Red"]System.out.print ( "plus a $100 birthday Bonus." );[/COLOR]
          } // end for
    
          // get type name of each object in employees array
          for ( int j = 0; j < employees.length; j++ )
             System.out.printf( "Employee %d is a %s\n", j, 
                employees[ j ].getClass().getName() ); 
          [COLOR="Red"]} //end if[/COLOR]
          
         [COLOR="Red"] else if ( currentMonth != employees.getMonth() )
    	  {[/COLOR]
          System.out.println( "Employees processed polymorphically:\n" );
          
          // generically process each element in array employees
          for ( Employee currentEmployee : employees ) 
          {
             System.out.println( currentEmployee ); // invokes toString
    
             // determine whether element is a BasePlusCommissionEmployee
             if ( currentEmployee instanceof BasePlusCommissionEmployee ) 
             {
                // downcast Employee reference to 
                // BasePlusCommissionEmployee reference
                BasePlusCommissionEmployee employee = 
                   ( BasePlusCommissionEmployee ) currentEmployee;
    
                employee.setBaseSalary( 1.10 * employee.getBaseSalary() );
    
                System.out.printf( 
                   "new base salary with 10%% increase is: $%,.2f\n",
                   employee.getBaseSalary() );
             } // end if
    
             System.out.printf( 
                "earned $%,.2f\n\n", currentEmployee.earnings() );
          } // end for
    
          // get type name of each object in employees array
          for ( int j = 0; j < employees.length; j++ )
             System.out.printf( "Employee %d is a %s\n", j, 
                employees[ j ].getClass().getName() ); 
          [COLOR="Red"]} //end if[/COLOR]
       } // end main
    } // end class PayrollSystemTest

  20. #20
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    what id like to do it get the reasoning down, and get it functional, then understand how to make it more streamlined after if possible.

    as currently constituted i get 2 errors, on each IF line cannot find symbol regarding the "month"

    i have tried using employees.month and employees.getMonth()

Page 1 of 3 123 LastLast

Similar Threads

  1. subclass calling superclass method
    By stackptr89 in forum New To Java
    Replies: 10
    Last Post: 11-22-2010, 08:40 PM
  2. Calling subclassed methods from constructors
    By arefeh in forum New To Java
    Replies: 7
    Last Post: 01-22-2010, 01:22 AM
  3. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 08:29 AM
  4. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 01:07 AM
  5. SuperClass of an Object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 03:51 PM

Posting Permissions

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