• Post Reply Bookmark Topic Watch Topic
  • New Topic

help on assignment  RSS feed

 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a basic assignment calculating pay and I am trying to figure out how to get eclipse to read each individual method(person a and person b) I have created in my main method and display the final pay.
It is only working through the first method and I am just curious what I can do? THANKS!! here is code...
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line that prints the final pay is in the else block. Person b has a pay greater than 8, so only that else if will be executed. If you want the final pay to be printed no matter what, you'll have to move it outside the if-else blocks.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The first thing you could do, and it would be a great favor to everyone who would like to help you, is to format your code properly. Here is a guide to proper indentation of Java code: http://www.javaranch.com/styleLong.jsp

In Eclipse, you can auto-format your code by pressing Ctrl+Shift+F (or Cmd-Shift-F on a Mac).

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:The line that prints the final pay is in the else block. Person b has a pay greater than 8, so only that else if will be executed. If you want the final pay to be printed no matter what, you'll have to move it outside the if-else blocks.


I don't think that's right. Look at the code again and the corresponding message. Based on the message that "person must be payed at least 8 dollars" it seems like the condition being checked is incongruent with the message: If the person is payed more than 8 dollars, then print a message that says "a person needs to be payed at least 8 dollars" - doesn't make sense, right?

Jordan, it's not very helpful to just say "It doesn't work." Rather, say what you're expecting to happen and then what's actually happening. Give details, like what output are you expecting and what the actual output is.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. you were right Jason I fixed the error with the pay being less than 8 instead of greater. How would I get the program to print the name of each person along with the final pay?
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan King wrote:How would I get the program to print the name of each person along with the final pay?


Google is your friend. There are many examples for System.out.println that will lead you to your answer. Please show some effort before asking questions.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you'll need to give each Person a name to start with. I'd also suggest restructuring your class to remove those print statements and provide an appropriate method to calculate pay. Here's something to start with:



Whether or not you should be throwing exceptions like that or just calculate things anyway is another discussion, but IMO it beats silently doing nothing and printing to the console.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joshua this was helpful. Still pretty new to java and programming but its always great to get some direction from people who are willing to share the knowledge.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it was certainly nice of Jason to help you out with a detailed answer, we encourage students and other learners to do their own work. We're all happy to guide newbies but we also don't want to deprive them of the joy and satisfaction of getting over the hump on their own. The rule of thumb is to help by giving hints and useful tips and tricks but don't spoon feed.

Also, by giving/accepting answers that are too detailed, you may have inadvertently violated any code of conduct agreements and/or policies that you/the student has explicitly or implicitly agreed to when you signed up for the class you are taking. My son goes to The Ohio State University and is taking a Java programming class and I have to restrain myself so that he can stay within his course Policy on Cooperation, Collaboration, and Professional Ethics

So please understand that when we seem to be holding back, it's not that we don't want to share information, it's because we want to help learners learn how to help themselves. Does that make sense?
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:

Whether or not you should be throwing exceptions like that or just calculate things anyway is another discussion, but IMO it beats silently doing nothing and printing to the console.

Exceptions should be thrown only in exceptional conditions. An IllegalArgumentException is a RuntimeException which is usually an indication of a programming error where the programmer did not check for conditions that should be met before calling the method that threw the exception. NullPointerException is another example of a RuntimeException that indicates a programming error.

Invalid data does not necessarily warrant a RuntimeException to be thrown, especially not with exception messages like "Worked too many hours" and "Does not meet minimum wage requirements" -- those are more along the lines of what are called "business rules". If you were to check for and say that "negative numbers are not allowed for payment calculations" that would be more in line with a condition where an IllegalArgument exception is warranted.

In this program, it would probably be more appropriate to make checks with if statements, as Jordan did, to see whether the data fell within the "business rules" enforced by the program, then display the appropriate messages if it didn't.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Invalid data does not necessarily warrant a RuntimeException to be thrown, especially not with exception messages like "Worked too many hours" and "Does not meet minimum wage requirements" -- those are more along the lines of what are called "business rules". If you were to check for and say that "negative numbers are not allowed for payment calculations" that would be more in line with a condition where an IllegalArgument exception is warranted.

In this program, it would probably be more appropriate to make checks with if statements, as Jordan did, to see whether the data fell within the "business rules" enforced by the program, then display the appropriate messages if it didn't.


That's a fair point, though I disagree on displaying messages as was done in the original. A method should do only one thing. Calculating pay and printing to the console are two things. You also should not allow for the construction of an object that is in an invalid state.

All that considered, one possibility is that the constructor in my example should take just a name, and the calculation takes both time worked and pay rate. This seems a bit odd to me, though.

If you want to avoid RuntimeExceptions, then calculate pay might throw checked exceptions that properly line up with the domain (one for working too many hours and one for not being paid enough). The client programmer would then be forced to handle that situation where pay could not be calculated because the arguments were invalid with respect to the business rules.

If you want to avoid exceptions entirely, then a double return type is insufficient: the only way you could communicate that the calculation was not performed would be through magic numbers being returned, and that's pretty awful. You'd need some sort of tri-state return to cover the three cases: worked too much, paid too little, and the calculation for pay worked.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:That's a fair point, though I disagree on displaying messages as was done in the original. A method should do only one thing. Calculating pay and printing to the console are two things. You also should not allow for the construction of an object that is in an invalid state.

And that's a fair point. In a program this small, however, it's not much of concern, especially since the programmer is still trying to wrap his head around more basic concepts.

All that considered, one possibility is that the constructor in my example ...

Yeah, about giving examples, I like your helpfulness but consider what I said about letting them do their work and providing too much detail. It's fine to do that when they have shown some effort but I think it's a little premature and could skirt the border on aiding and abetting.

If you want to avoid RuntimeExceptions, then calculate pay might throw checked exceptions that properly line up with the domain (one for working too many hours and one for not being paid enough). The client programmer would then be forced to handle that situation where pay could not be calculated because the arguments were invalid with respect to the business rules.

Eh, you're dredging up arguments fought a while back about giving developers a choice vs forcing them to handle exceptions which they just throw back up the chain anyway. Again, identifying truly exceptional cases and how to handle them is subject to lengthy and often passionate discussions. IMO, using if statements and avoiding exception throwing is a sound strategy in most cases where business rules are involved. But let's not have that talk in front of the kiddies just yet. It'll probably just confuse them. Take it over to General Computing or somewhere where more experienced hands will be amused.

If you want to avoid exceptions entirely, then a double return type is insufficient: the only way you could communicate that the calculation was not performed would be through magic numbers being returned, and that's pretty awful. You'd need some sort of tri-state return to cover the three cases: worked too much, paid too little, and the calculation for pay worked.

Not really sure I understand where you're heading with the magic numbers... are you thinking perhaps of an error code or status code of some kind? Again, let's take that somewhere other than Beginning Java or at least wait until OP asks about it. Otherwise, we're running the risk of digressing and confusing OP. Or is it too late to think about that now?
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Yeah, about giving examples, I like your helpfulness but consider what I said about letting them do their work and providing too much detail. It's fine to do that when they have shown some effort but I think it's a little premature and could skirt the border on aiding and abetting.


Very good point. Know thy audience and all that Looking at it again, I agree that I did provide more detail than I should have for this situation.

Junilu Lacar wrote:
Not really sure I understand where you're heading with the magic numbers... are you thinking perhaps of an error code or status code of some kind? Again, let's take that somewhere other than Beginning Java or at least wait until OP asks about it. Otherwise, we're running the risk of digressing and confusing OP. Or is it too late to think about that now?


Exactly. If you separate concerns properly, then you'd need to return something to indicate status. This is a point where I'm on the fence still. I definitely see your point about moving too quickly, but I am also worried about breeding bad habits. Separation of concerns is very important, and to brush it off in a "toy program," especially where it is obviously being violated, makes me feel uneasy. I'd rather drive home that point before a beginner gets comfortable with a particular (wrong) style.

Let's try and put the lid back on this box, though. Jordan: ignore most of my rambling (for now at least) Take a look at the Google search Junilu linked to earlier and play around with printing values out (I think one of the tutorials there also talks about formatted strings). Figuring out how to deal with output properly is a good first step that will help you write more complicated programs (so you can see the results of those programs).
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:
Exactly. If you separate concerns properly, then you'd need to return something to indicate status. This is a point where I'm on the fence still. I definitely see your point about moving too quickly, but I am also worried about breeding bad habits. Separation of concerns is very important, and to brush it off in a "toy program," especially where it is obviously being violated, makes me feel uneasy. I'd rather drive home that point before a beginner gets comfortable with a particular (wrong) style.

I'm all about driving. In fact, I'm known to wield a heavy hammer in some forums, especially when it comes to good/bad habits and style, so I'm right there with you on that, pardner.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay Junilu I do understand what you mean about figuring things out for myself and thanks for that, I'm just looking for pointers not the whole program laid out for me, and Jason I think it would be sufficient if the constructor took both the pay rate and hours but I probably should have used my main method for calculating the pay. Is this somewhere along the lines of being correct? I ask because I want to start with small bits of code so that in large software construction I will have developed the right habits. thanks for the help. ^_^
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I can't really see the main method calculating pay but perhaps I could create a method that takes pay and hours from my constructor and calculates it. I have the program working and ready to turn in, I'm just wondering what would help me more in the long run, using the main method or creating a separate method. also if it is appropriate to put each separate person method (a,b,c) into the main method. perhaps I am overthinking it at this stage?
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*put each separate person constructor
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jordan King wrote:Well I can't really see the main method calculating pay but perhaps I could create a method that takes pay and hours from my constructor and calculates it. I have the program working and ready to turn in, I'm just wondering what would help me more in the long run, using the main method or creating a separate method.

Generally you want your main method to be as light as possible. It's just the launching point of your application and should not really include any kind of logic. You could try creating a method that uses your people objects and figures out the pay given the people. Try and out and post it here!

Jordan King wrote:Also if it is appropriate to put each separate person [constructor] (a,b,c) into the main method. perhaps I am overthinking it at this stage?

Not sure what you mean by this. Can you show an example?
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it Jason thanks. I have removed all of the bugs in that program so I have started a new project. would it be alright to post the code in this topic or should I start a new topic?
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New project, new questions... New topic would be a good idea.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!