This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Find total annual salary project

 
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm completely new to programming of any type. My goal is a java app that takes 2 fixed variable (salary and commission %) plus a user input for total annual sales. Finds the product of annual sales and commission% then adds that result to the fixed salary to output a total annual salary.
This is where I'm at right now. Am I on the right track? pointers on fixing whats here?

Thank you

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of thoughts:
If you name is "Artemis Ehnis" then why do the JavaDoc comments state that the author is "scottjensen"
What is the purpose of these lines, is this a test case?

The code that you've provided does not run as it is, you are missing a couple of }.
You appear to be doing okay, but it's kind of hard to tell. We can tell more once you have the program doing a bit more.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to CodeRanch.

I hope that you will find that there are many people well versed in Java and most of them are willing and able to help out.
Please note, that we generally do not provide full working solutions, especially in the Beginning Java area.
Instead we provide hints and code snippets  so that the end user can discover the solution on their own.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name discrepancy comes from there account creation. There was a ScottJensen already so I made a random name for my account. This is why my javadoc had my name and my account has a different name. I understand the concern though and thank you.
To answer the snippet I was trying to make sure those variables stayed static and nothing would change them. I realize that was not the way to do that.
I have since re-written my code and have it working. My only issue is the output is lacking spaces and I don't know how to fix that.

 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want finer control over how text is display then you should look into use the printf method https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
You should not do this

but maybe you meant to do this

The double quote with nothing in it is a waste of resources, with a space surrounding by the double quote you can get some formatting.

It appears as though you supplied code snippet is missing a couple of } at the end.
In the future, please supply those so that one can copy the full code and run it. Sometimes missing a } is the source of the problem.
 
Marshal
Posts: 80656
477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

I can see you are running into trouble. You are mixing things in a static context and things in an instance context. Get as much out of the static context as you can. Get as much code out of the main method as you can: we can see it is already too long and you have only shown us its beginning. Look here. You have made things worse between your first post and your second by moving the fields into the main method.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect the space is what I was missing and that I was also forgetting to copy the last }} . Now it all seems to be running correctly. I need to eventually figure out how to change totalPay to output as a dollar amount.
Thank you for the quick responses it's allowing me to see the little things I'm missing.


 
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pete Letkeman wrote:


Pete's way is good, and I would recommend it. As a point of contrast, this is how I'd do it using the printf() method (print-formatted).
Where
"%s" prints a String
"%.2f" prints a float or double with 2 decimal places
"%n" prints a new-line
 
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are more improvements could be made.

Did you learn about the methods use? In case yes, you are lacking that. All code is crammed to main method without attaching any context to its separate parts.

I'm also quite disappointed to see a suggestion of formula to be used in specified requirements, which is:
(total annual sales * Commission %)+salary=total annual income

Even though for most it looks trivial, I'd say much more readable is a form of other way round:
salary + (total annual sales * Commission %) = total annual income

And more correct explanation could be:
total annual income = salary + (total annual sales * Commission %)

And that is mainly because base salary is agreed upfront, while the commission might be earned at the end of sales period and might be not. Not sure if you get an idea what I have in mind, but such way of following suggested form of formula makes program cryptic, much harder to read and much harder to understand when you introduce more parts like that.

But probably it is just an unfortunate of the given instructions.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone.
I'm going to see if I can figure out accomplish the suggestion from @Campbell Ritchie.
@Carey Brown I can now see how that works. I spent some time last night trying to set that up I had figured out the %s for a string, but got lost in the others.
I have learned more in 1 day here than in 2 weeks of class.
 
Campbell Ritchie
Marshal
Posts: 80656
477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you want a Worker object with name, salary, etc. as fields. This is of course a dreadful oversimplification. The object should have methods like calculateTotalEarnings. You may have such methods already, only with a different name. Consider making the toString method return a description including total pay. If you use the String#format() method, it uses % tags; they are exactly the same tags as you would use for System.out.printf.
Consider making the commission (3½%) a final static field, but only if it can be regarded as a constant for all workers.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about using methods and classes in this program.
If I build a class run and a class main should an commission object be in the run or the main?

 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The traditional and recommended way of launching an application is to have a small class that has a main method in it.  The main method itself would have only one or two lines that launch the app, and that's it.

In your situation, the launching class might be called MainApp which would run TotalAnnnualSalary.  This might have a method called calculate and there you would use a Commission object.  (Remember, an object is an instance of a class.)  Where your Commission class (the file) is would depend on the size of your app, but for a little project having everything in one package is fine.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
Yes I was trying to make a TotalCompensation (main) and a EmployeeSalary (run) that would do all the variables and do the math.
Just wanted to make sure the main was only the execution part and would display the output, while the run accepted the input and did all the calculations on the variables.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there a quick and clean way to allow input and output as $xxx.xx ?

I just want to make sure I don't get an exception if the user puts a $ and , in the input for total sales.
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I've had to move on to the next part which I've seen in other threads that I don't see working.
Now we add in a commission requirement, a bonus, and a table to show potential gains for higher sales.
I've been struggling with making mine work. I thought i had everything except for the tables and my main class, but still not sure.
I would like some feedback.



 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods should do one thing and the name of the method should clearly state the intent.
You have a method computePay() that actually does three things: Prompts for sales, computes the pay, and prints a response. I would suggest breaking these responsibilities into separate methods. It might be handy to have a couple of private fields (no getters): commissionEarned, and bonusEarned, and anualPay.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey I can do that..

Any suggestions on a video that walks through an actual implementation of Scanner? not just a single class? I still don't know where to put the input and how to pass it across the classes. I've tried a couple things and still no joy.
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Artemis Ehnis wrote:Any suggestions on a video that walks through an actual implementation of Scanner? not just a single class? I still don't know where to put the input and how to pass it across the classes. I've tried a couple things and still no joy.


Because this Scanner issue comes up quite often, many developers create a utility class to handle this. As a utility all methods are static. Here's a bare bones one. Add this class to your project and then all your other classes can use Keyboard. To use it in your code it would be something like this:
or

 
Campbell Ritchie
Marshal
Posts: 80656
477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For all nextXXX() methods except next(), I include a loop to verify that the token parsed is of the correct type, or nextLine() isn't returning the empty String. Rob Spoor taught me ages ago that you can completely avoid input mismatch exceptions with that technique. It is also quite easy to enhance those methods to accept (numeric) values within a specified range.
Since Java9, it has been possible to get a Stream<String> from a Scanner too. Maybe you should enhance that by requiring a sentinel value for stopping the Stream with takeWhile().
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method computePay() has quite a few syntax errors in it. The formatting is also bad and this hides some of the errors.  Please post code that will compile.  If you need help finding syntax errors, we can help, but try it yourself first, then post what you get.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently I have something in the wrong place. I've fixed syntax errors and separated things out.
Now I can run the project without errors, but I get no request for input and no output other than run: 0.0 build successful (total time: 0 seconds)
I believe I need to move the scanner input into the main class. Should the if else statements be in the main class as well? Would it be better to make a employee class that has a constructor for the employee and compensation class with a compensation constructor?
I really want to learn the right way, but have zero background and the material I've been given is the certificate study guide and you folks.
Thank you for feedback and patients.
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please re-post the code in its current state.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructor class


main class


Carey's keyboard class

 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where do you call the method to get the sales?

CompensationApp.java
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have expected this method to have a conditional, like this

 
Liutauras Vilda
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking to this thread and can't follow to be honest. I don't think OP will ever solve this exercise/project that way.

If we call variables as methods in comments, that suggests that need to delete everything and start over with very small steps. And perhaps only after revising slides and class examples.

There are lots of methods but they never being called, or at least I don't see them called. Then OP mysteriously trying to access default sales field. I'm confused.
 
Liutauras Vilda
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

I don't think you need any keyboard utility classes here. In my opinion you need simple things which You can understand. That understanding comes when you trying in practice one or another technique without copying and pasting stuff and assuming it is OK because somebody more experienced gave you.

I also disagree with Carey here, I don't think it helps for OP to show such indentation (especially when he's a starter), at least I cannot follow it either. There are lots of stuff in that class OP doesn't need in that project, I think it just adds confusion, which is already too much in OP's head.

If OP would learn only a good indentation and formatting in this project getting lowest mark for everything else - I'd say that would be a bigger achievement than submit such work which is suspicious. Why all those getters setters if everything is meant to be printed from same class...
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Liutauras Vilda I really appreciate your response. I was trying to pick what i was asking for out of the comments, which some did take me deeper down a rabbit hole.
I have taken your advice and went back and started all over. It compiles now and works for the most part. Stuck on the if else outputs. For some reason it is only giving me my first print command and not running the rest of the process.

Main class


Employee Class
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You don't want the 'else'.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:
You don't want the 'else'.


Are you sure?  It looks okay to me.

Also, when I entered 100000 as the annual sales, I got several lines of output from 100,000 to 145,000.  What were you expecting?
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you take out the first 'else' then the commission and bonus will work but not the message saying that you don't get any commission.

If you swap A_SMALL_NUMBER and A_LARGE_NUMBER then the 'elses' will work but you'd have to rearrange the bodies of the if() to match.
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better yet, nest one if inside the other.
 
Liutauras Vilda
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Artemis Ehnis,

Looking to some of the constructs you had come up, to me seems that you've heard about Object Oriented programming. Am I right?

The thing is, that such exercise I think either needs to be solved all in one class with several methods (typical approach at the beginnin), or, if you learned about objects already - properly.
That properly means, not just to create more than one class, but to create classes which conveys exact ideas.

Now you have class Employee. But you also have some methods which are called getCompensationApp() and potentialTable() - none of them make sense to have them in Employee class.
But before I move on with my thoughts, if you could please answer how much you have learned about the objects.

I see you struggle a lot with this fairly simple exercise, and I believe you struggle not because you couldn't understand the logic, but simply because you don't understand how that logic is wired up. And that is because you assigned responsibilities incorrectly, what Employee supposed to know. Who supposed to calculate commision, etc... The success of such project depends pretty much how you design your application - and that is the whole beauty of object-oriented programming.

Which path to go, you need to tell us what kind of significance among other assignments this exercise has. Is it lab homework? Is it tutor marked assignment? Maybe final marked assignment? That way it would be easier to understand what is expected from you with this exercise and how much effort is needed.
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey I thought that was wrong. I pulled the else out and now it's working to that point. Now to work on the last piece which is an output table.

@Knute the output at the end is an assignment requirement that after showing what the persons annual compensation is it then returns a while loop showing compensation until the sales reach 50% more than current sales. Example if you sell 100,000 it then posts an output for every 5000 until it reaches 150,000. In this case it is messed up and is outputting over and over like it was the compensation portion not a table of possible.

@Liutauras you are correct. I understand the principals of object oriented languages. I am lost with the "wiring". Every tutorial or instruction I find is great for showing the pieces. They are just really lacking on how they work together. Having a reading issue does nothing when the material you are given is the certification manual not a beginners book. These are lab homework assignments and each week builds off the last. I just really want to know java and the class isn't really teaching, they expect a very moderate return from assignments. I'm working on trying to clean this up and make it not only work, but be correct. I'm also working on the next assignment which requires 2 employees and a comparison with their salaries. As for objects and classes I understand the purpose of a constructor and I only put the potentialTable at the bottom to get the assignment turned in. Now I will fix it in it's own class when there is more time.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Artemis Ehnis wrote:@Knute the output at the end is an assignment requirement that after showing what the persons annual compensation is it then returns a while loop showing compensation until the sales reach 50% more than current sales. Example if you sell 100,000 it then posts an output for every 5000 until it reaches 150,000. In this case it is messed up and is outputting over and over like it was the compensation portion not a table of possible.


Here's what I get when I run your program:
What's the problem with this?
 
Carey Brown
Saloon Keeper
Posts: 11062
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Were you looking for something more like this:
 
Artemis Ehnis
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output should be

"Your annual compensation is x"
that part is correct.. the last piece is a potential so it would be an output of
"If your sales had been "x" you could have earned "y"

Using your example if you sold 100,000 it would output 100,000 + salary = annual salary.
Then go on to give potentials;
if you had sold  "x" you would make "y"
105000   50367.5
110000   53850
.....

We just got into arrays this week so I hadn't tried an arrayTable which is exactly what i wanted, but hadn't learned yet. I would have been happy with just getting the text output right and cutting off the you have earned a commission all together in the outputs after the initial return.
I'm probably not making sense right now sorry.
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations Artemis Ehnis,

Your question has made it to our Journal  

Have a Cow!
 
reply
    Bookmark Topic Watch Topic
  • New Topic