UOP stinks!
If it were not for this forum, I don't think I would be able to pass this class. The examples the book gives do not correlate with the actual assignments. If you want to learn java, do not try and learn it from UOP.
Calling for help once again. same batcode same batplace.
I am tasked with writing a class that holds employee info. I have done that, and now need the program to read input from the user and store it in the class...
Only, the example the book gives throws a compilation error:
non-static method setEmpName(java.lang.String) cannot be referenced from a static context
empinfo.setEmpName( newName );
Here is the code:
import java.util.Scanner;// Calls scanner for input
publicclass Payroll
{
// Begin main method
publicstaticvoid main( String args[] )
{
// Create scanner to obtain input
Scanner input =new Scanner( System.in );
// Display Program title and description.
System.out.println("Payroll Program Part 2." );
System.out.println("This program will calculate the weekly pay for an employee." );
System.out.println();//outputs blank line
// Create Employee Info object
empinfo employee =new empinfo("Paul", 40, 10 );
// Display inital info
System.out.printf("The initial parameters are: \n" );
employee.displayInfo();
System.out.println();
// Prompt for initial employee name
System.out.printf("Please enter the employee's name: " );
String newName = input.next();//Read input and assign to variable
empinfo.setEmpName( newName );
System.out.println();
}// End method main
}// End class Payroll
and here is the code of the class:
publicclass empinfo
{
private String empName;//Employee name for this class
privatedouble empHours;
privatedouble empRate;
//constructor initializes name
public empinfo( String name,double hours,double rate)
{
empName = name;
empHours = hours;
empRate = rate;
}
// method to set the empName
publicvoid setEmpName( String name )
{
empName = name;//store the empName
}//end method
//method to set empHours
publicvoid setempHours (double hours )
{
empHours = hours;//store empHours
}//end method
//method to set empRate
publicvoid setempRate (double rate )
{
empRate = rate;// store empRate
}//end method
//method to retrieve empRate
publicdouble getEmpRate()
{
return empRate;
}//end method
// method to retirev empHours
publicdouble getEmpHours()
{
return empHours;
}//end method
// method to retrieve empName
public String getEmpName()
{
return empName;
}//end method
//method to retrieve earnings
publicdouble getEarnings()
{
return empRate * empHours;
}
// Display the employee info
publicvoid displayInfo()
{
System.out.printf("The employee's name is: %s\n", getEmpName() );
System.out.printf("%s worked %.2f hours this week.\n", getEmpName(), getEmpHours() );
System.out.printf("%s's hourly rate is $%.2f\n", getEmpName(), getEmpRate() );
System.out.printf("%s earned $%.2f this week.", getEmpName(), getEarnings() );
}//end method displayInfo
}//end class empinfo

