jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes i guess using variables from one class into another class all in the same package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i guess using variables from one class into another class all in the same package?" Watch "i guess using variables from one class into another class all in the same package?" New topic
Author

i guess using variables from one class into another class all in the same package?

Danielle Proulx
Greenhorn

Joined: Oct 21, 2010
Posts: 6
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...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Can you post what you have done so far? Don't forget to use code tags.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11255
    
  16

usually, you create classes to mirror real-life things. For example, you might have an 'employee' class. It may have variables in it to store things like 'name', 'salary', 'start date', etc.

The object can have 'getter' methods - a method that lets you 'get' the value of the variable. Assuming you have a reference to that object, you could call the getters to get the values.

OR...maybe you don't need to. Your 'employee' class could also have a method you could call that returns the base pay.

Staphan is correct - post your code here so we can see what you're doing, and we'll gladly make suggestion (probably more than you want)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Danielle Proulx
Greenhorn

Joined: Oct 21, 2010
Posts: 6
What do you mean by code tags? Like single line comments stating what i am trying to do??
Danielle Proulx
Greenhorn

Joined: Oct 21, 2010
Posts: 6
here is my code...

main class



base pay class


Danielle Proulx
Greenhorn

Joined: Oct 21, 2010
Posts: 6
Sorry about all the posts... here is the assignment just in case you don't understand what i am tyring to do...

Thanks in advance for all the help!!!

http://www.umsl.edu/~lawtonb/423D/3806_ASG.html

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
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.
Danielle Proulx
Greenhorn

Joined: Oct 21, 2010
Posts: 6
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??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11255
    
  16

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"
Danielle Proulx
Greenhorn

Joined: Oct 21, 2010
Posts: 6
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i guess using variables from one class into another class all in the same package?