I apologize if this is difficult to understand... i am a beginning programmer and my first major project in my programming class is to create a payroll application. I have created my main class and have created an "object" to import into the main class to calculate base pay. How do i use the variables in the base pay class in another class? I am trying to make it so the user only has to input the data once instead of everytime a different function is called.
does this make sense??
Let me know if you don't understand and i can do my best to explain further...
Danielle Proulx wrote:What do you mean by code tags? . . .
This. I shall try to edit your post since you are new, so you can see how much better it looks.
And welcome to the Ranch
Joined: Oct 13, 2005
Why are you using StringTokenizer? Read its documentation, which tells you not to use it. Should you need to split Strings, try the String#split() method.
If you are being assessed, don't use the * format for imports.
I think you ought to create a class to represent the employee (as Fred has already said) and move much of that code out of the main method. A main method should be short, preferably very short. similarly, why are you not creating objects to represent pay?
Since you how to use try-catch, don't declare that main throws an Exception.
Joined: Oct 21, 2010
I am using string tokenizer b/c that is what the teacher has taught us... i will try your method as this appears to be the preferred method for new programs...
also, if i am understanding your question properly (i.e. objects for pay)... i'm not creating an object b/c the user is being prompted to enter that themselves. Is that what you mean??
You're probably going to hate to hear this, but...I'd probably throw most of this code away.
In OO programming, you generally want to think about 'real world' things - a person, and employee, a business, a pizza...whatever. In your case, you have something that might be called an "Employee". an employee might have a first name, a last name, an hourly rate, and a number of hours worked - although that last one is questionable...
So what I would do is define a class called "Employee" with variables that hold those values.
I'd then create a constructor that lets me pass in those values, or at the very least write getters and setters that let me, well, get and set those values.
In your main(), you could do all your prompting to get user input. You'd then create an instance of that object with those values.
Assuming you do NOT put the hours worked in (since that is something that could change often), you'd then prompt the user for those hours.
I would then have a method in my Employee class that computed the gross pay for the week. You'd pass it in the hours, it would do the multiplication, and then return the result back to your main.
One other point...generally, when dealing with money, you should represent it as an integer type variable in the atomic unit. In the U.S., that should be in pennies. Once you have it, you can change the formatting to display it however you want. If you store it as a float-type variable, you'll get some weird results like a gross salary of "$1,273.2300000000324332"
Joined: Oct 21, 2010
Great thanks for the info... i will try it this way and see how far i get. Once i have made a little progress can i post for review?? I know we shouldn't post more than like 15 lines if i read correctly. is there a way to send the whole file to give me pointers?
This site is awesome... i have only been doing this for like 2 months and am really trying to learn how to do everything efficiently/properly! This is an amazing resource for someone like me.
Joined: Oct 13, 2005
Agree with Fred.
You post as much code as you need to post. If you need 60 lines to demonstrate a problem, you post 60 lines. But don't post 6000! And don't try writing 15 lines at once. Write 5 lines, compile, run, repeat.
I suggest you point out to your instructor what I said about StringTokenizer.
subject: i guess using variables from one class into another class all in the same package?