aspose file tools*
The moose likes Beginning Java and the fly likes Are My Classes In Order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Are My Classes In Order" Watch "Are My Classes In Order" New topic
Author

Are My Classes In Order

Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
I have now taken my PayCheck Program and instead of having one script, I have now two.

The first script includes the methods used to assist in the PayCheck Program. I have created 3 methods inside the class:


Now the second script is the actual main program that will call the methods. An instance of the class ,PayCheckMethods, is declared, called "aWage":



Please tell me if me if my code looks correct, and if there are any tips you guys could recommend me? Please don't ask me to tip the waiter. I just want a drink that's all! lol
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

money calculations on double is dangerous!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and you can also move all three method to a utility class and the static method take an Employee object as an argument?
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
Seetharaman Venkatasamy wrote:money calculations on double is dangerous!


Can you please point me in the right direction where to get information about BigDecimal, and also the equivalent to NextDouble (when prompting the user)

Checked out the net and I saw so much information...very little on an equivalent to NextDouble...
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

I would separate the core pay check fields into another class (called PayCheck) and then rename PayCheckMethods to PayCheckUI.

PayCheckUI would then handle user interaction and use PayCheck as part of its underlying model.

The methods display() and promptUser() would belong to PayCheckUI whilst calcPay() would belong to PayCheck.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Jason Smit wrote: . . . Can you please point me in the right direction where to get information about BigDecimal,
Not the faintest idea where you will find anything at all, but if you are really desperate you can try here. Or click the words in your post which have been underlined; they turn into links automatically.
and also the equivalent to NextDouble (when prompting the user)

Checked out the net and I saw so much information...very little on an equivalent to NextDouble...
In which case this will be of no use to you
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
Campbell Ritchie wrote:
Jason Smit wrote: . . . Can you please point me in the right direction where to get information about BigDecimal,
Not the faintest idea where you will find anything at all, but if you are really desperate you can try here. Or click the words in your post which have been underlined; they turn into links automatically.
and also the equivalent to NextDouble (when prompting the user)

Checked out the net and I saw so much information...very little on an equivalent to NextDouble...
In which case this will be of no use to you


I appreciate all help Sheriff, really I do. But I am stuck on getting my program to work. Can you please tell me if I am leaving anything out of the program?



Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
another try...



now getting this error:
C:\java_work\PayCheck>java mainPayCheckProg
Exception in thread "main" java.lang.NullPointerException
at PayCheckMethods.<init>(PayCheckMethods.java:28)
at mainPayCheckProg.main(PayCheck.java:17)

C:\java_work\PayCheck>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Jason Smit wrote:another try... . . .
That phrase should strike terror and loathing into your heart. It makes it sound as if you were guessing.

Your formula is (min(h, m) + max(h - m, 0) × o) × p
. . . where h is hours earned, m is maximum hours before paying overtime, p is payrate and o is multiplier for overtime.
I suggest there is a simpler way to do it. If h > m, then overtime is payable. You cannot simply use > on BigDecimals, so you rely on its being Comparable.
Oh, I see you are having a different problem. You have an uninitialised BigDecimal which you are trying to use. But you don’t need that doubleValue in the first place. You should remove all those doubles from your code. You require precise arithmetic, so you get rid of all floating-point values and use BigDecimal throughout.

You also have a problem about object orientation. You appear to have two lots of variables. One lot which are instance fields, and the other which are local variables to the main method. You ought not to have all that code in the main method in the first place. You can reduce your main method to this:-The fields about hours and payRate should be instance fields, and you don’t need those local variables at all. That will require this sort of constructor, with a few spelling errors corrected . . . and a suitable toString method.
Notice I have used some names different from yours. I am sure you can sort that out. But I think the name PayCheckMethods is not a good name for a class; please improve that class name.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Sorry, I misread your code badly and misinterpreted some bits. Please ignore what I struck out in the previous post. I am very sorry about that mistake.

Alternative to NumberForma: the printf method or String#format. You would want something like $%.2f to get money into dollar format. I think that rounds ¢0.5 up to ¢1, but I am not certain. Look in the java.util.Formatter class for details of the % tags, also look in the Java Tutorials.
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
Campbell Ritchie wrote:Sorry, I misread your code badly and misinterpreted some bits. Please ignore what I struck out in the previous post. I am very sorry about that mistake.

Alternative to NumberForma: the printf method or String#format. You would want something like $%.2f to get money into dollar format. I think that rounds ¢0.5 up to ¢1, but I am not certain. Look in the java.util.Formatter class for details of the % tags, also look in the Java Tutorials.


Howdy Sheriff Ritchie

Should I rather ignore your code in your previous example?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Thank you for not getting annoyed about my mistake.

The code I wrote is all right; the mistakes were all in the comments. You will have to change some names to get it to match yours; for example you had a BigDecimal called wages and I called it pay. That sort of difference should be really obvious, however. You can use the Comparable interface, which I linked to earlier, to decide which BigDecimal is larger. Beware: you get false from this first comparisonThere is a lot of controversy about whether the first line should print true or false, but I think it is correct to print false. Look in the BigDecimal documentation; it’s explained there, but very briefly.
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
The final version of the PayCheck program. Seems to be working so far. (Any tips let me know)



And of course the main program...

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
You ought to have created a Pay class and had several instance of it, rather than deleting the information every time you have a new pay rate.
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
Campbell Ritchie wrote:You ought to have created a Pay class and had several instance of it, rather than deleting the information every time you have a new pay rate.


My brain understands better with code or image or diagrams ;)

examples please lol
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Pay pay1
Pay pay2
Pay pay3
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
That CalcPay method is mighty difficult to read. It's highly unusual to break a method invocation into separate lines like that, with parentheses hanging out all alone. It makes them look, on first viewing, like code blocks.

You don't get bonus points in Java for packing everything into a single statement. Why not calculate the pay for regular hours and overtime hours separately, and then take their sum?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8244
    
  23

Jason Smit wrote:Can you please tell me if I am leaving anything out of the program?...

I split your enormous line 47 because it was screwing up the windowing software here. Hope you don't mind.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
This is where I begin to start pulling my hair...like I said...I am a newbie.






Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8244
    
  23

Jason Smit wrote:This is where I begin to start pulling my hair...like I said...I am a newbie.

Not quite sure what your problem is now, but you should definitely follow Dennis' advice and break up some of those huge calls. I would also get into the habit of writing methods that take arguments and return values; it makes them more self-contained (Google "loose coupling").
For example:and then you can call it from elsewhere with something like:
BigDecimal pay = calcPay(hoursWorked, payPerHour);

It may seem like more code, but do you see how much clearer it is?

The above is by no means the only way of doing it, I just provide it as an example.

HIH

Winston
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
Winston Gutkowski wrote:
Jason Smit wrote:This is where I begin to start pulling my hair...like I said...I am a newbie.

Not quite sure what your problem is now, but you should definitely follow Dennis' advice and break up some of those huge calls. I would also get into the habit of writing methods that take arguments and return values; it makes them more self-contained (Google "loose coupling").
For example:and then you can call it from elsewhere with something like:
BigDecimal pay = calcPay(hoursWorked, payPerHour);

It may seem like more code, but do you see how much clearer it is?

The above is by no means the only way of doing it, I just provide it as an example.

HIH

Winston


I will work on the writing methods that take arguments and return values. Your example makes me a little confused. Here is why.

I have an example of a method which returns a value:



This example returns "interest". "Interest" is a variable of type "double". The parameter of method "getInterest" has a variable called "rate" of type "double". This example returns a variable "interest" of type "double".

My point is why does your example return just "wages" when the parameters include BigDecimal hours and BigDecimal payRate. Should it not return "hours" and "payRate"?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Jason Smit wrote: . . . . My point is why does your example return just "wages" when the parameters include BigDecimal hours and BigDecimal payRate. Should it not return "hours" and "payRate"?
sorry for delay; I have been busy, and didn’t understand the problem yesterday.

No, the method is being used as a function which calculates pay from the pay rate and hours worked. It is like a calculator where you put two alues on screen and get one result. That one result is the return value.
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20


This is the main program. It does not seem to run. Also I got this error:
Exception in thread "main" java.lang.NullPointerException
at PayCheckMethods.CalcPay(PayCheckMethods.java:38)
at mainPayCheckProg.main(PayCheck.java:21)


I am not how to call this method (i.e. CalcPay)? Any advice where I am going wrong??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
You need to write object-oriented programming, as I told you last week. You need to set up those hours and payRate fields in the constructor and you need to keep them well-hidden inside your class (private access). You don’t allow the main method to “see” them. Then you simply have to pass the data in, and get a display out.
What you are doing is instantiaing a class (no-arguments constructor: dangerous), getting its fields (should be private, so you can’t get them) which turn out to be null, and then trying to use those null values: result =
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8244
    
  23

Jason Smit wrote:I am not how to call this method (i.e. CalcPay)? Any advice where I am going wrong??

Well, first: it should be calcPay(), not CalcPay(). It's only a convention, but it's very widely used (and often required by companies), so I'd get into the habit if I were you.

Second: You seem to be concentrating far too much on language minutiae rather than thinking about what you're class is supposed to be doing.

My advice:
1. Turn your computer OFF.
2. Write down in English, what you want your class to do.
3. (Only when you have written and understand exactly what is needed) turn your computer back on and refactor your class accordingly.

I know it's a great temptation, but jumping straight into Java code is usually a very bad way to go. Make sure you understand the problem (or the requirements) before you write a line of code.

Winston
Jason Smit
Greenhorn

Joined: Apr 19, 2012
Posts: 20
Winston Gutkowski wrote:
Jason Smit wrote:I am not how to call this method (i.e. CalcPay)? Any advice where I am going wrong??

Well, first: it should be calcPay(), not CalcPay(). It's only a convention, but it's very widely used (and often required by companies), so I'd get into the habit if I were you.

Second: You seem to be concentrating far too much on language minutiae rather than thinking about what you're class is supposed to be doing.

My advice:
1. Turn your computer OFF.
2. Write down in English, what you want your class to do.
3. (Only when you have written and understand exactly what is needed) turn your computer back on and refactor your class accordingly.

I know it's a great temptation, but jumping straight into Java code is usually a very bad way to go. Make sure you understand the problem (or the requirements) before you write a line of code.

Winston


Thanks for the advice. Yeah, it is such as habit to jump into code before thinking about the problem. Will concetrate on the problem and plan/analyse before code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
We have given you enough hints!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are My Classes In Order