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

[6673 byte] By [PaulDoca] at [2007-11-26 18:50:57]
# 1

That should be employee.setEmpName(newName). "empinfo" is the class name as you've defined it. It's not a static method (nor should it be). You should use the instance ("employee") to call that method.

This kind of mistake would be harder to make if you followed Java naming conventions. Class names start with an upper-case letter.

UOP? Is that University of Pennsylvania, the Ivy League school? It seems like a pretty good school.

paulcwa at 2007-7-9 6:25:02 > top of Java-index,Java Essentials,New To Java...
# 2

Thank you. I am getting tired and frustrated, (obviously). This makes perfect sense and works.

i'll change the class name to be uppercase.

The school I am referring to is University of Phoenix. But I would probably run into this from any online university. It is really difficult to learn even this basic stuff (for me anyway).

Thanks again.

Rgds,

Paul

PaulDoca at 2007-7-9 6:25:02 > top of Java-index,Java Essentials,New To Java...
# 3

one thing i would HIGHLY recommend to you, is make sure your

reference books were made like... this year. Because the older books

that teach you java, teach you either :

1.) inneficient ways(that were updated by Sun) of accomplising a task

or 2.) just a flat out wrong way to write something(which were also udated by Sun)

honestly... i use google.com as my reference. A good point of reference can mean a world of difference.

I have like 4 books that are nearly worthless, except for teaching basics of syntax...

Good luck to ya :)

SomeDudea at 2007-7-9 6:25:02 > top of Java-index,Java Essentials,New To Java...