• 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

Getting the wrong result

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I get this result



I am trying to calculate the bonus 2 employees will recieve based on monthly contributions, if the monthly contribution is >= initial commitment *3, but after I try to print the total bonus I get 0.0???
Please help
 
Ryan Gordon
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





including this hopefully it helps
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you have two classes with methods which print zero.
You also have badly‑indented code in the first block, which will confuse you about what the code is doing. You will have to go through the code; you have so many poorly‑named variables and 0s floating around, it is not obvious how much of the code will actually run. I can see some if blocks which will not be entered.
 
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
You have a method in your Employees class named
You have a method in your TheBonus class nameds
When you use calculate1.computeBonus(); in you main method you are calling the computeBonus from the Employees class.

To call the computeBonus from TheBonus call you would use either

or
 
Ryan Gordon
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do the methods print zero?
This code



gives this error



if i use return computeBonus(cmnt, qs);

i get this error



I tried this at the bottom of thebonus class.


 
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

Ryan Gordon wrote:Why do the methods print zero?

What you have is this:

totalBonus is zero and at no other place is it different.
 
Ryan Gordon
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought it became different in thebonus class after all the if statement calculations?
 
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

Ryan Gordon wrote:
This code

i get this error :


What are the values of cmnt and qs when you pass them into the function?

Please note that you are currently not using the recommended style guide and you are missing getters and setters as well.
 
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

Ryan Gordon wrote:I thought it became different in thebonus class after all the if statement calculations?


If you are able to successfully call computeBonus(cmnt, qs[]); then you will get some values changed or at least written to the screen.
 
Ryan Gordon
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the values of cmnt and qs when you pass them into the function?
What do you mean by this, sorry I dont understand.

Please note that you are currently not using the recommended style guide and you are missing getters and setters as well.
What is the recommended style guide? Which getters and setters am I missing?

Thank you again for your help so far
 
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
I'm going to break this into a few separate posts to keep things somewhat separate.

Ryan Gordon wrote:Which getters and setters am I missing?


You do not appear to have any getters or setters for your class and everything is "public".
This may work, but it is not recommenced.

Setters and getters allow to you encapsulate your object/class.
You make object variables private and you wrap public methods around them to set and get the values.
Here is a link explaining them in detail:
https://coderanch.com/t/463484/java/explanation-getter-setter-methods-difference

Here is an example of simple getters and setters, which do not use your code:

More logic could/should be added to verify values. For instance right now age can be negative and the name values can be null.
With this Person object I could then do something like:

You can put whatever complex logic you want in your object. This sample one is only adding two strings together using the getFullName method.

Given this example your could rewrite your code to eliminate one of your Employee classes.
 
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

Ryan Gordon wrote:What are the values of cmnt and qs when you pass them into the function?
What do you mean by this, sorry I dont understand.


When you are calling a method, for instance

you are supplying values to that method. Both cmnt and qs[] should have values by the time you call computeBonus(cmnt, qs[]).
If either cmnt or qs[] do not have values when you call your computeBonus(cmnt, qs[]) then you could run into problems.
It depends on how you have setup the computeBonus method. If this method does not check for valid values then you will get errors.

Ryan Gordon wrote:What is the recommended style guide?


There is more then one style guide, some more complex/complete then others. The big things are:
  • all variables should start with a lower case letter e.g. int age; String school;
  • multi word variables should use upper case for each word after the first e.g. String firstName; String streetAddress;
  • all method names should start with a lower case letter we e.g. setFirstName(); onKeyPressed();
  • all class names should start with an upper case letter e.g. class CodeRancher{}, class SchoolTeacher{}
  • Spacing helps with readability, so indent correctly using either four spaces or a single tab for each nested level. You should not have two } aligned, one on top of the other

  • If you Google for "simplified java style guide" you can find more then one however, the five basic rules listed above should be enough for beginners.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you, I tried one of the things you said so far, I plan on starting fresh on this and using the getters and setters. I am currently running into problems with the array portion for my get/set.




    I then run into this error, is using an array in get/set not allowed or is it something else?


    By the way thank you for all your help so far, I am learning and i appreciate that.
     
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's some lines of code which you wrote which are valid Java but are misleading:



    Look at the first line: the unwary reader could get the impression that "qs" is a double variable. But it isn't... it's a double[] variable. So it's better to declare it so it actually looks like one:



    Likewise it looks like you're passing a double value to the setqs method; but you aren't, you're passing a double[] value. So better code:



    And the third fragment: are you returning a double value? Having read my comments about the first two fragments, you should be able to answer that.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I got up to this point so far




    Im getting all these errors, It seems like the code is correct by going from your example but Im getting identifier expected errors




    Thank you again
     
    Paul Clapham
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why are those lines of code in the Employee class?
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello, that was a mistake I should have put that in my main method. I have this code now and its fine expect I am having trouble with Emp1.setqs



    My array is a double but it is returning an int? How can I resolve this?
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You have probably been told already that qs is a bad name for your array; the names of variables shou‍ld make it obvious what they mean.
    Where is the variable qs which you are getting the compiler errors about?
    If you are passing four ints, what is the method supposed to receive as a parameter? Remember, you wrote the method, so you decided what parameters it would take. Did you declare it as double[] or double...?
     
    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
    If you introduce the main method after

    You can be close to being done
    Here is a start

    This line Emp1.setqs(5000, 7000, 4000, 8000); is expecting an array and you are not providing an array. You are providing four different values.
    This line Emp1.setbonus(qs * .03); is expecting a value for "qs". But you have not defined a variable qs inside of the main function.
    If qs is an array then you cannot simply multiply an array by a value. You have to multiply each element of the array by that value.

    Are you able to paste all your code in your response so that we can see what exactly you have now?
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator



     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I changed qs to quarterlysavings so that it makes more sense.
     
    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
    This line is incorrect

    that is not how you create an array, wrong brackets, missing "new" and that is not now you pass a value to the setqs method. You are missing brackets.
    This is a bit better

    You cannot multiply an array like that. You have to multiply all of the elements of the array by the value.
    I would put this multiplying logic inside of the Employee class.

    Once you have the figured out then you should be almost done.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How do you multiply the elements of the array by the value?

    I tried adding the math login in the top of employee class



    and i get








    same error





    All the stuff that both of you explained to me is making sense, but arrays are really causing me problems, thank you again for all your help so far I appreciate it.
     
    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

    Ryan Gordon wrote:How do you multiply the elements of the array by the value?


    This page https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html talks about and shows using the for loop to get the elements of the array.
    Then you need to multiply those values.
     
    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
    This does not work and will not work

    You must work on each individual element in the array.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator







    I don't know why I'm getting this error please help.
     
    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

    should be

     
    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
    You are not looping through the your array.
    What if you do this:

    quarterlysavings.length is the number of elements in the array.
    j is the index of the array element
    quarterlysavings[j] is the individual array element
    If you do your loop like that then you do not have to change the loop if the size of the array changes.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I did as you said and that part is good, but when i have just return totalbonus;

    it gives me this error

     
    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
    Yes, you cannot return a value from any function when you declare the function as void.
    Here is your code with the spacing fixed:

    If you replace

    with

    you will not get that error any more.
    Or you could wrap this up in a method all by itself kind of like

    This line of code

    probably does not do what you want it to.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I did as you said and I am still getting errors







     
    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
    Sorry, I had a typo

    should have been

    That fixes one error

    There error on this line

    means that
    Emp1.getTotalBonus()
    is expecting a value. You could try
    Emp1.getTotalBonus(0)
    and the error would disappear, but you may not get the results you want.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you, It compiles but now gives an ouput of
    0.0
    0.0
     
    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
    The method getTotalBonus() is defined in the Employee class.
    This would mean that you need to look at what this method is doing and how it's doing it.
    A few hours ago it was this

    But I suspect that has changed now that getTotalBonus requires a double.
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh I forgot to ask, why do we use j in the loop instead of i ?
     
    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
    I do use i, but for this editor when I tried that it became italics.
    Try it, putting this in this editor and seeing what happens
    means array element at i
    When I can't use i, I use j or k
     
    Ryan Gordon
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator








    I thought this was the appropriate way to use get and set but it is not allowing me to???
     
    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
    You can name any method almost anything, within certain compiler rules that is

    "getProperty" and "setProperty" generally are used for the private variables of the class like firstName and lastName.
    There are times when you can combine two properties to make a third property like fullName.

    When you have a method which calculates a value you could use something like

    Or something like

    A property is a the value of something at a point in time. For instance a the color could be a property of eyes and it's value could be blue.
    This value could possibly change from object to object.

    A method could be used to calculate a value.
    For instance a method could be used to calculate the total area of a triangle. This could change depending on the properties and it's calculated differently for a triangle and a rectangle.

    If you need to calculate something using decision statements or loops or by checking the value of one or more property then you most likely want a method and not a getter or a setter.

    The error that you are currently getting states that the variable getCommitment is not part of the Emp1 object. Emp1 is an instance of the Employee class.
    Do you have a variable named getCommitment or do you have a method named getCommitment in the Employee class?
    If you have a method named getCommitment then you need to call that method with the correct brackets, possibly like
     
    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
    Okay, I have an idea. What is we put together everything you have that works correctly so far and deal with with we know.
    One: We know that we need an class to handle some employee information
    Two: We need to sum up the yearly savings
    Three: We need to calculate the bonus based off of quarterly savings and a min value and a percentage
    Four: We need to test out the code to see if it works

    You have already completed part 1 in one or more of the previous postings in this thread.
    You have the formulas for both part 2 and part 3
    Part 4 shows you how you can test your object out in the same class.

    As is this code runs, but you still need to complete it. I suggest completing one part at a time and testing it.
    I would suspect that some/most of this has been covered in a textbook or during class if this is indeed a homework assignment.

    You will need to change/remove all of the return statements as they are there only to make the program run

    This currently outputs something like this

    when these two commands are run
    javac Employee.java
    java Employee


    I hope this helps put you on the correct path.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic