Results 1 to 20 of 49
- 12-14-2010, 03:15 AM #1
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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
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.
- 12-14-2010, 03:35 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
or should it actually say "private Date birthdate;" when i declare variables?
- 12-14-2010, 03:50 AM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
im not sure how or where, but i will also need some sort of method for date like getMonth, getDay, getYear, right?
- 12-14-2010, 11:48 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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.
- 12-14-2010, 03:25 PM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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;
?
- 12-14-2010, 03:51 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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.
- 12-14-2010, 05:25 PM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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
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?
- 12-14-2010, 05:32 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Yes, you have to import it.
The API is also quite handy.
I did suggest passing a Date object into the constructor as well.
- 12-14-2010, 05:48 PM #9
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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.*;????
- 12-14-2010, 05:50 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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;
- 12-14-2010, 06:02 PM #11
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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-14-2010, 06:11 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
Did you put the Date.class and the Employee.class in the same directory?
- 12-14-2010, 06:11 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
OK.
You didn't say that.
And you didn't do that in your code either.
So create that Date class and use that.
- 12-14-2010, 06:20 PM #14
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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?
- 12-14-2010, 06:24 PM #15
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
- 12-14-2010, 06:29 PM #16
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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>
- 12-14-2010, 06:29 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
- 12-14-2010, 06:51 PM #18
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
does you statement hold true even after seeing my output post above?
- 12-14-2010, 10:33 PM #19
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
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
- 12-14-2010, 10:35 PM #20
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
Similar Threads
-
subclass calling superclass method
By stackptr89 in forum New To JavaReplies: 10Last Post: 11-22-2010, 08:40 PM -
Calling subclassed methods from constructors
By arefeh in forum New To JavaReplies: 7Last Post: 01-22-2010, 01:22 AM -
superclass and subclass
By mr idiot in forum New To JavaReplies: 19Last Post: 01-03-2009, 08:29 AM -
Calling methods from superclass
By moaxjlou in forum New To JavaReplies: 7Last Post: 12-11-2008, 01:07 AM -
SuperClass of an Object
By Java Tip in forum Java TipReplies: 0Last Post: 12-06-2007, 03:51 PM
Bookmarks