• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are My Classes In Order

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
money calculations on double is dangerous!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pay pay1
Pay pay2
Pay pay3
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is where I begin to start pulling my hair...like I said...I am a newbie.






 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have given you enough hints!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic