Re: Can not figure out what I am doing wrong
RedGrittyBrick wrote:
Start with this:
class Employee
{
private double payrate;
private double hoursworked;
private String name = "blanknow";
public Employee ()
{
payrate = 0.0;
hours = 0.0;
}
}
Or even this:
public class Employee
{
private double payrate;
private double hoursworked;
private String name = "blanknow";
}
which has exactly the same effect, but is accessible from "outside" thanks to
the "public" keyword.
As a second step, add "getPayrate()", "getHoursworked()" and "getName()"
methods. These should return the values of the similarly-named "private"
variables. They should be public methods.
As a third step, add a "main()" method that uses the "get...()" methods to
display the values of the "private" variables. (BTW, those "private"
variables are called "instance variables".) "getPayrate()" should return 0.0,
as should "getHoursworked()". "getName()" should return what you expect.
As a fourth step, add "setPayrate()", "setHoursworked()" and "setName()"
public methods. Pay attention to the advice others have given you.
Fifth, use those "set...()" methods in the "main()" method.
Don't proceed to step "n+1" until you are sure you got step "n" right. At
each step, make sure the class compiles before you try to run it. Make sure
it runs before you declare the step complete.
Make *extra* sure you spell the same thing the same way everywhere it's used.
For example, don"t spell a variable "payrate" one place and "rate" another.
Watch out for missing blanks, as was previously mentioned.
Upper- and lower-case count!
Loose blocks of code simply contained in braces inside a class without a
surrounding method will not do what you seem to think, as was mentioned before.
This is all what others have told you, but I hope that providing just a wee
bit more detail might be useful.
- Lew