Results 1 to 3 of 3
  1. #1
    i202i20 is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Inheritance Help

    I need help with programming i am so lost.

    I tried upload my program but is did not work. So I am posting it here any help with it I would appreciate it.
    Java Code:
    //PayrollSystemTest.java
    // Employee hierarchy test program
    import javax.swing.JOptionPane;
    public class PayrollSystemTest 
    {
       public static void main( String args[] ) 
       {
        // create four-element Employee array
          Employee employees[] = new Employee[ 4 ];
          String first = "";
          String last = "";
          String ssn = "";
          String salaryString;
          double salary = 0;
         String wageString;
          double wage = 0;
          String hoursString;
          int hours = 0;
          String salesString;
          double sales = 0;
          String rateString;
          double rate = 0;
          String empTypeString = "";
          int empType = 0;
          int count;
          
         for(count = 0; count < 5; count++)
          {
             empTypeString = JOptionPane.showInputDialog(null, "Enter an employee type number(1-5):", JOptionPane.QUESTION_MESSAGE); 
             empType = Integer.parseInt(empTypeString);
             
             switch(empType)
             {
             case 1:
                first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
                last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
                ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
                salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
                salary = Double.parseDouble(salaryString);
                break;
                
             case 2:
                first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
                last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
                ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
                wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
                wage = Double.parseDouble(wageString);
                hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
                hours = Integer.parseInt(hoursString);
                break;
                
             case 3:
                first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
                last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
                ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
               salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
                sales = Double.parseDouble(salesString);
                rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
                rate = Double.parseDouble(rateString);
                break;
                
             case 4:
                first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
                last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
                ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
                salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
                sales = Double.parseDouble(salesString);
                rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
                rate = Double.parseDouble(rateString);
                salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
                salary = Double.parseDouble(salaryString);
                break;
                
             default:
                JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
                break;
             }
          
                  switch (empType)
                            {
                                  case 0: Employees[0] = new SalariedEmployee(first, last, ssn, salary);
                                  break;
                                  case 1: Employees[1] = new HourlyEmployee(first, last, ssn, wage, hours);
                                  break;
                                  case 2: Employees[2] = new CommissionEmployee(first, last, ssn, sales, rate);
                                  break;
                                  case 3: Employees[3] = new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary);
                                  break;
                                  
          // create subclass objects
          SalariedEmployee salariedEmployee = 
             new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
          HourlyEmployee hourlyEmployee = 
             new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
          CommissionEmployee commissionEmployee = 
             new CommissionEmployee( 
             "Sue", "Jones", "333-33-3333", 10000, .06 );
          BasePlusCommissionEmployee basePlusCommissionEmployee = 
             new BasePlusCommissionEmployee( 
             "Bob", "Lewis", "444-44-4444", 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;
          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;
                double oldBaseSalary = employee.getBaseSalary();
                employee.setBaseSalary( 1.10 * oldBaseSalary );
                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() ); 
       } // end main
    
    
    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
    
          System.out.printf( 
             "Date object constructor for date %s\n", toString() );
       } // 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.\n", 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.\n", testDay );
       
          return 1;  // maintain object in consistent state
       } // end method checkDay
    
       // return day
       public int getDay()
       {
          return day;
       } // end method getDay
    
       // return month
       public int getMonth()
       {
          return month;
       } // end method getMonth
    
       // return year
       public int getYear()
       {
          return year;
       } // end method getYear
    
       // 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
    
    // Employee.java
    // Employee abstract superclass.
    public abstract class Employee 
    {
       private String firstName;
       private String lastName;
       private String socialSecurityNumber;
       private String birthDate;
       
       // three-argument constructor
       public Employee( String first, String last, String ssn )
       {
         firstName = first;
          lastName = last;
          socialSecurityNumber = ssn;   
       } // end three-argument Employee constructor
       // set first name
       public void setFirstName( String first )
       {
          firstName = first;
       } // end method setFirstName
       // return first name
       public String getFirstName()
       {
          return firstName;
       } // end method getFirstName
       // set last name
       public void setLastName( String last )
       {
          lastName = last;
       } // 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
       // return String representation of Employee object
       public String toString()
       {
          return String.format( "%s %s\nsocial security number: %s", 
             getFirstName(), getLastName(), getSocialSecurityNumber() );
       } // end method toString
       
       // abstract method overridden by subclasses
       public abstract double earnings(); // no implementation here
    } // end abstract class Employee
    // CommissionEmployee.java
    // CommissionEmployee class extends Employee.
    public class CommissionEmployee extends Employee 
    {
       private double grossSales; // gross weekly sales
       private double commissionRate; // commission percentage
       // five-argument constructor
       public CommissionEmployee( String first, String last, String ssn, 
          double sales, double rate )
       {
          super( first, last, ssn );
          setGrossSales( sales );
          setCommissionRate( rate );
       } // end five-argument CommissionEmployee constructor
       // set commission rate
       public void setCommissionRate( double rate )
       {
          commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
       } // end method setCommissionRate
       // return commission rate
       public double getCommissionRate()
       {
          return commissionRate;
       } // end method getCommissionRate
       // set gross sales amount
       public void setGrossSales( double sales )
       {
          grossSales = ( sales < 0.0 ) ? 0.0 : sales;
       } // end method setGrossSales
       // return gross sales amount
       public double getGrossSales()
       {
          return grossSales;
       } // end method getGrossSales
       // calculate earnings; override abstract method earnings in Employee
       public double earnings()
       {
          return getCommissionRate() * getGrossSales() * 4;
       } // end method earnings
       // return String representation of CommissionEmployee object
       public String toString()
       {
          return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f", 
             "commission employee", super.toString(), 
             "gross sales", getGrossSales(), 
             "commission rate", getCommissionRate() );
       } // end method toString
    } // end class CommissionEmployee
    // BasePlusCommissionEmployee.java
    // BasePlusCommissionEmployee class extends CommissionEmployee.
    public class BasePlusCommissionEmployee extends CommissionEmployee 
    {
       private double baseSalary; // base salary per week
       // six-argument constructor
       public BasePlusCommissionEmployee( String first, String last, 
          String ssn, double sales, double rate, double salary )
       {
          super( first, last, ssn, sales, rate );
          setBaseSalary( salary ); // validate and store base salary
       } // end six-argument BasePlusCommissionEmployee constructor
       // set base salary
       public void setBaseSalary( double salary )
       {
          baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative
       } // end method setBaseSalary
       // return base salary
       public double getBaseSalary()
       {
          return baseSalary;
       } // end method getBaseSalary
       // calculate earnings; override method earnings in CommissionEmployee
       public double earnings()
       {
          return getBaseSalary() * 4 + super.earnings();
       } // end method earnings
       // return String representation of BasePlusCommissionEmployee object
       public String toString()
       {
          return String.format( "%s %s; %s: $%,.2f", 
             "base-salaried", super.toString(), 
             "base salary", getBaseSalary() );
       } // end method toString   
    } // end class BasePlusCommissionEmployee
    public class HourlyEmployee extends Employee 
    {
       private double wage; // wage per hour
       private double hours; // hours worked for week
       // five-argument constructor
       public HourlyEmployee( String first, String last, String ssn, 
          double hourlyWage, double hoursWorked )
       {
          super( first, last, ssn );
          setWage( hourlyWage ); // validate and store hourly wage
          setHours( hoursWorked ); // validate and store hours worked
       } // end five-argument HourlyEmployee constructor
       // set wage
       public void setWage( double hourlyWage )
       {
          wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
       } // end method setWage
       // return wage
       public double getWage()
       {
          return wage;
       } // end method getWage
       // set hours worked
       public void setHours( double hoursWorked )
       {
          hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
             hoursWorked : 0.0;
       } // end method setHours
       // return hours worked
       public double getHours()
       {
          return hours;
       } // end method getHours
       // calculate earnings; override abstract method earnings in Employee
       public double earnings()
       {
          if ( getHours() <= 40 ) // no overtime
             return getWage() * getHours() * 4;
          else
             return (40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5) * 4;
       } // end method earnings
       // return String representation of HourlyEmployee object
       public String toString()
       {
          return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", 
             super.toString(), "hourly wage", getWage(), 
             "hours worked", getHours() );
       } // end method toString
    } // end class HourlyEmployee
    // SalariedEmployee.java
    // SalariedEmployee class extends Employee.
    
    public class SalariedEmployee extends Employee 
    {
       private double weeklySalary;
    
       // four-argument constructor
       public SalariedEmployee( String first, String last, String ssn, 
          double salary )
       {
          super( first, last, ssn ); // pass to Employee constructor
          setWeeklySalary( salary ); // validate and store salary
       } // end four-argument SalariedEmployee constructor
       
       // set salary
       public void setWeeklySalary( double salary )
       {
          weeklySalary = salary < 0.0 ? 0.0 : salary;
       } // end method setWeeklySalary
    
       // return salary
       public double getWeeklySalary()
       {
          return weeklySalary;
       } // end method getWeeklySalary
    
       // calculate earnings; override abstract method earnings in Employee
       public double earnings()
       {
          return getWeeklySalary() * 4;
       } // end method earnings
    
       // return String representation of SalariedEmployee object
       public String toString()
       {
          return String.format( "salaried employee: %s\n%s: $%,.2f", 
             super.toString(), "weekly salary", getWeeklySalary() );
       } // end method toString
    } // end class SalariedEmployee
    Last edited by JosAH; 04-17-2013 at 07:49 AM. Reason: added [code] ... [/code] tags

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Inheritance Help

    Do you, by any chance, have a question?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default Re: Inheritance Help

    And your question is?

    Few people will sift through code without at least some idea of what it is they should be looking at.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. inheritance
    By FREDISCO in forum New To Java
    Replies: 2
    Last Post: 03-27-2012, 02:11 AM
  2. JPA Inheritance
    By videanuadrian in forum Advanced Java
    Replies: 0
    Last Post: 01-11-2011, 06:30 AM
  3. Inheritance
    By terahawks in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 09:58 AM
  4. Inheritance
    By Nerijus in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 03:47 AM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12: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
  •