• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble creating a helper class to do some calculations  RSS feed

 
Stef Weidert
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my original code:




I need to replace the calculation "int awayFromTheNumber = playerGuess.length- jeallyBeans;" with a helper class.

The first method will be a static method and should be named findLowestDifference(). This method determines which guess is the closest to the actual number of jelly beans in the jar. It will accept as an argument the array that holds the guesses, and it will return an int that represents the smallest absolute difference between the any of the guesses and the actual number of jelly beans.

The second method needs to identify which person (or persons) guess is the closest by calling another static method called reportWinner(). Now, reportWinner() will be a static void method. It will not return a value. It will simply determine who the winner (or winners) are by comparing each player’s difference from the actual number of jelly beans to the smallest difference value that was obtained by calling the method findLowestDifference(). I need to match the index number or numbers of the array elements containing the guesses to the index numbers of the array containing the names of the people who made the guesses.

If anyone can offer help it would be greatly appreciated!
 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you having trouble with? Making a helper class? Making methods? We can only help when we know what to help with.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have been back to your post and edited it, breaking some of the too‑long lines and adding code tags, and you can see how much better it looks. You can read about breaking lines in this style guide.

I am afraid your methods are far too long. you should particularly have a very short main method. Our FAQ says two lines, but I think you can get it down to one line. I have found few people who recommend a shorter main method than me
I think you are going to ahve to redesign the whole thing, I am afraid. Consider the classes you are going to need. I can see two just looking, but you will need a Game class to run the whole thing. That makes three. You can start the game like thisI know that arrays are themselves objects, but avoid parallel arrays like the plague. They are error‑prone, and you can usually design a class which incorporates the data of those parallel arrays and have an array of that class.
A few style things:
  • Use spaces, not tabs for indenting.
  • Only ever use the \n character if somebody tells you they want CR. Use %n and the printf method or similar instead.

  • I think you should use something other than Math.random for finding (pseudo‑)random ints. Look at this recent thread, where various options and possible errors are discussed.
    Helper classes? I suspect that is the same us what I call a utility class. Give it a private constructor which you never call, because you never need any instances. Give it no instance members: all members static. Although it is usually a bad idea to think how you want to do something at an early stage, I think a utility class will be a good and useful idea. You should find it easy to create, now I have given you some info.

    How are you going to write a method to find the smallest difference from the jar's contents? If you can't work that out, consider how you can iterate an array and find the index of the smallest element. That is a simpler but quite similar problem.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stef Weidert wrote: . . .
    I think there are two serious logic errors in that line. I also think they cancel each other out.
     
    Stef Weidert
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm sorry I am rather new to this. I am having problems creating the methods. I know I have to take the random number- the playerGuess (or other way around depending if the guess is lower or higher than the random number generated). Then figure out which guess is the smallest ( or exactly on) and export them as the winner. Thanks for viewing!
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And what will happen if two people both have a winning guess?
     
    Stef Weidert
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    if two people get the same amount of numbers away from the random number generated both of their names should be displayed such as

    There were 1500 jelly beans in the jar.

    The winners are:

    Mike with a guess of 1475, which is 25 away from the actual number of jelly beans.
    Tony with a guess of 1525, which is 25 away from the actual number of jelly beans.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stef Weidert wrote: . . . I am having problems creating the methods. . . .
    No, the problem you have got is straight in front of you. It is a piece of very sophisticated plastic and silicon and it is interfering with your thought patterns. Get rid of said sophisticated hardware and find the one bit of hardware in the room which is more sophisticated still. It lives about an inch behind your eyes and is called a brain. You can get the brain to work by writing, drawing, sketching etc, for which you need a sheet of paper, a pencil and an eraser. That last piece of hardware is by far the most important.

    And it might be better if your brain is the only one in the room. Then you can curse the process to your heart's content without anybody hearing
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stef Weidert wrote:if two people get the same amount of numbers . . .
    In 15″ I thought of at least two ways to implement that. But try implementing one winner only at first, then consider how to add multiple winners later.
     
    Stef Weidert
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If I work it out on paper it looks like this:

    Random Number (rn): 1000
    User Guesses { (a)250,(b)500,(c)750,(d)1025}
    Calculation
    (rn) 1000-(a) 250=750
    (rn) 1000-(b) 500=500
    (rn) 1000-(c) 750=250
    (d) 1025-(rn) 1000=25
    Answers
    {750,500,250,25}
    Lowest to highest
    {25,250,500,750}

    Lowest difference = 25
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is a good start
    But you need to work out how are going to find the smallest difference. What if you had 400 guesses on that paper rather than 4? Write down how you are going to find it. Remember scribbling on the paper and erasing it is permitted.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see you are sorting your array. That is one way to do it, but there are other ways.
     
    Stef Weidert
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I used asort to sort the values from low to high that way no mater how many values it will still give the lowest one (from what I understand). Here is part of my code but I think if this is a helper class I have unnecessary code but I am not sure.


     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That code looks confused, and I don't think it will work. I am not sure it will run to completion. It looks like code where you have tried to write everything all at once, whereas you ought to try it in little bits. Each little bit becomes a method.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote:That code . . . I am not sure it will run to completion. . . .
    Correction. I am sure that code will not run to completion.

    Back to the paper and pencil, please.
     
    Stef Weidert
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I added my code to my calculations. I think I may have went through more steps than needed but I am not sure if I really did anything.

    Random Number (rn): 1000
    User Guess Array { (a)250,(b)500,(c)750,(d)1025}
    Calculation
    Random Number(jellyBeans)-Guess(playersGuess)= firstCalcArray
    (rn) 1000-(a) 250=750
    (rn) 1000-(b) 500=500
    (rn) 1000-(c) 750=250
    (d) 1025-(rn) 1000=25
    Answers-firstCalcArray
    {750,500,250,25}
    Lowest to highest-
    sortLowestDifference= assort(firstCalcArraySorted)
    {25,250,500,750}

    Lowest difference-firstCalcArray[0] = 25
     
    Stef Weidert
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Any ideas if I should generate the random number in the original class or the helper class? If I call it in the original class is there a way to call it to do calculations in the helper class, if I call it in the helper class is there a way to export it as "There were____ jelly beans in the jar"?
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stef Weidert wrote:Any ideas if I should generate the random number in the original class or the helper class? . . .
    Get the design of classes going and you will probably be able to work out the answer to that easily.
    To the other questions: all “yes”.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!