The Artima Developer Community
Sponsored Link

Java Answers Forum
a java error I don't understand ?

1 reply on 1 page. Most recent reply: Nov 25, 2009 2:16 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
June G

Posts: 1
Nickname: javalearne
Registered: Nov, 2009

a java error I don't understand ? Posted: Nov 24, 2009 7:53 AM
Reply to this message Reply
Advertisement
Ok my assignment was to create a base class call Employee that contain variables name, number and yearOfHire. they must have a constructor that initialize to get the variables from the user and setter + getter methods. Then I got to derive another class from this class, called ProductionEmployee
which have a instance variable baseHourlyPayRate and a method, calcPayrat(), which would get the yearOfHire from Employee class and subtract this from current year to get years of service. Then I had to use a loop and add a dollar for every year of service and return the value.
After that I had to create a driver program to see if it works.
However, I'm getting an error called : non-static method getYearOfHire() cannot be referenced from a static context.
What does that mean ? and how do I go about solving it ?
Here are my codes :
/**
 * 
 * create class Employee
 */
public class Employee
{
    private int name;
    private double number;
    private double yearOfHire;
    
    // NUMBER
    
    public void setNumber (double num)
    {
        number = num;
    }
    
    public double getNumber ()
    {
        return number;
    }
    
    // YEARS HIRED
    
    public void setYearOfHire (double yoh)
    {
        yearOfHire = yoh;
    }
    
    public double getYearOfHire ()
    {
        return yearOfHire;
    }
    
    // NAME
    
    public void setName ( int nam)
    {
        name = nam;
    }
    
    public int getName ()
    {
        return name;
    }
    
    // CLASS 2
    
    private class ProductionEmployee
    {
        private double baseHourlyPayRate;
        
    // HOURLY PAY RATE WITHIN CLASS 2
    
        public void setBaseHourlyPayRate ( double bhPR)
        {
            baseHourlyPayRate = bhPR;
        }
        
        public double getBaseHourlyPayRate ()
        {
            return baseHourlyPayRate;
        }
        
        
        // METHOD TO ADD A DOLLAR FOR EVERY YEAR
        public static void calcPayRate ( )
        {
           int yearsHired = Employee.getYearOfHire( ) - 2009; // HERE IS THE ERROR. <---This very line 
            
            for ( int index = 0; index <= yearsHired; index ++)
            {
               hourPay = ProductionEmployee.getBaseHourlyPayrate( ) ++;
            }
        }
    }
}
 
 
/**
 * 
 * Test Employee class
 */
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
 
public class EmployeeTest
{
    public static void main (String [] args)
    {
        
        String input; //user input
        double inp; 
        int inpu;
        
        Employee employee1 = new Employee ( ); // Declares Employee object.
        Employee productionEmployee = new Employee ( ); // ProductionEmployee
        
        //  Data for Employee
        //get name
        input = JOptionPane.showInputDialog ("Please enter your name: ");
        inpu = Integer.parseInt(input);
        inpu = employee1.setName( );
        //get number
        do
        {
        input = JOptionPane.showInputDialog ("Please enter a number between 1 and 9999 : ");
        try 
        {
            inp = Double.parseDouble (input);   
        }
        catch (NumberFormatException e)
        {
            input = JOptionPane.showInputDialog ("Please enter a number between 1 and 9999 : ");
            inp = Double.parseDouble (input);   
        }
        } // END OF DO LOOP
        while (inp < 1 || inp < 9999);  // IF NUMBERS DON'T  CORRESPOND
        inp = employee.setNumber( );
        
        // get year of hire
       do
       {
        input = JOptionPane.showInputDialog (" Please enter the year you were hired: ");
        try
        {
          inp = Double.parseDouble(input);
        }
        catch (NumberFormatException e)
        {
          input = JOptionPane.showInputDialog (" Please enter the year you were hired: ");
               inp = Double.parseDouble(input);
            }
        } //     end of do loop
        while (  inp < 1999 || inp > 2009 );  // if year is less then 1999 or more then 2009 rerun.
        inp = employee.setYearOfHire( );
        
        // *************************************Data for ProdutionEmployee****************************************8
        
                //get name
        input = JOptionPane.showInputDialog ("Please enter your name: ");
        inpu = Integer.parseInt(input);
        inpu = employee.setName( );
              //get number
        do
        {
        input = JOptionPane.showInputDialog ("Please enter a number between 1 and 9999 : ");
        try 
        {
            inp = Double.parseDouble (input);   
        }
        catch (NumberFormatException e)
        {
            input = JOptionPane.showInputDialog ("Please enter a number between 1 and 9999 : ");
            inp = Double.parseDouble (input);   
        }
        }   // END OF DO LOOP
        while (inp < 1 || inp < 9999);            // IF NUMBERS DON'T  CORRESPOND
        inp = productionEmployee.setNumber( );
              // get year of hire
       do
       {
        input = JOptionPane.showInputDialog (" Please enter the year you were hired: ");
        try
        {
          inp = Double.parseDouble(input);
        }
        catch (NumberFormatException e)
        {
          input = JOptionPane.showInputDialog (" Please enter the year you were hired: ");
               inp = Double.parseDouble(input);
            }
        } //     end of do loop
        while (  inp < 1999 || inp > 2009 );  // if year is less then 1999 or more then 2009 rerun.
        inp = productionEmployee.setYearOfHire( );
             // get pay per hour. min $10
        do
        {
        input = JOptionPane.showInputDialog ("Please enter amount paid per hour: ");
        try
        {
            inp = Double.parseDouble(input);
        }
        catch (NumberFormatException e)
        {
             input = JOptionPane.showInputDialog ("Please enter amount paid per hour: ");
             inp = Double.parseDouble(input);
            }
        } // end Do loop
        while ( inp < 10 );
       inp = employeeProduction.setBaseHourlyPayRate ( );
        
        calcPayRate ( empoyeeproduction );
        
        //display for employee
        
        JOptionPane.showMessageDialog (null, "Hello, " + (employee.getName())+"\n" + "Your number is: " + (employee.getNumber())+"\n"+"Hired in year: " + (employee.getYearOfHire()));
 
            // I stopped here to test the code out before proceding.        
        // display for Production Employee
 
    } // end CLASS
} //end MAIN
        
        


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: a java error I don't understand ? Posted: Nov 25, 2009 2:16 AM
Reply to this message Reply
Um, there's a whole bunch of problems in your code, not just the "static" one.

You've created the class ProductionEmployee inside Employee. Therefore, it does not extend Employee but is a component of it. The two employees below are just employees regardless of the fact that you have named one productionEmployee.
Employee employee1 = new Employee ( ); // Declares Employee object.
Employee productionEmployee = new Employee ( ); // ProductionEmployee
Instead, you should have an Employee class with no reference to ProductionEmployee and a separate ProductionEmployee class that extends Employee.
public class ProductionEmployee
extends Employee
{
   // Your addional ProdEmp code goes here.
   // The basic Employee methods and fields are included automatically
   // by the "extends" clause above.
}
The declarations above then become:
Employee employee1 = new Employee ( ); // Declares Employee object.
ProductionEmployee productionEmployee = new ProductionEmployee ( ); // ProductionEmployee
You now have two employees, one of whom is a production employee.

As to the "static" problem: A static method is available to all Employees (in this case). A non-static method is only available to individually named employees (such as employee1 and productionEmployee above. In the line you highlighted you are calling the method getYearOfHire but you are not identifying whose year of hire you want. One solution would be to rewrite the method as follows:
/**
 * Add a dollar to the given pay rate for each year of service. 
 * @param yearOfHire The year in which the employee was hired.
 * @param hourlyPayRate The employee's pay rate (in dollars).
 * @return The employee's modified pay rate.
 */ 
public static int calcPayRate(int yearOfHire, int hourlyPayRate)
{
   int yearsHired = yearOfHire - 2009;
   int newPayRate = hourlyPayRate;  
   
   for ( int index = 0; index <= yearsHired; index ++)
   {
      newPayRate++;
   }
   return newPayRate;
}
You would then need to modify the rest of you code to use his modified method appropriately.

Flat View: This topic has 1 reply on 1 page
Topic: write/read two byte arrays to one file-  help! Previous Topic   Next Topic Topic: Novice to Java needs help.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use