• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

please help with returning a String representation of each Ball's Path.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been running in circles for a few days on this code...I am sure its something simple that I just can't find.  This is an assignment and the professor is pretty specific about methods, etc.  I will post his instructions, just in case i am interpreting something incorrectly.

I will also post my code thus far....  I finally have the Path class returning the correct string.  I have moved to the Bean Machine class and I need the path to print a string of "LRLLRL"  for every ball.... I currently get one continuous string of variables for the amount of balls I drop... but I need them to all be individual strings that I can later search for occurrences of R and L.    I just can't get past this part... i can't move on until this gives the right result...  Thank you so very much in advance for your time and patience.

***************************instructions*********************************************************

The Problem Statement ‐ Path
Start by creating a class named Path.  This class will need to keep up with a single String variable which 
will hold a series of characters the will represent the path the ball takes as it falls through the machine.  
The only possible characters here are "L" or "R", depending on whether the ball fell to the left or right 
when it hit the peg, respectively.  For example, in the example shown above, the String would end up 
being, "LLRRLLR".  Note that because there are 7 levels in this machine, there are exactly 7 characters, 
one for each peg that the ball hit. 
In addition to the representation of the path, this class will need to provide for the ability to move the 
ball to the right and left, to get the full path, and even to fall a specified number of levels.  This means it 
will need: 
 A 0‐parameter constructor.  Recall that the purpose of the constructor is to initialize the 
instance variable(s). 
 void moveRight() – This method will mark that the ball went to the right when it hit a peg. 
 void moveLeft() – This method that will mark that the ball went to the left when it hit a 
peg. 
 void fallLevels(int number) – This method simulate and log the ball falling down the 
specified number of levels.  Remember that at each level there is an equal chance of falling 
either right or left (Hint: use Math.random) 
 String getPath() – This method that will return the String representation of the path. 
The Problem Statement ‐ BeanMachine
This class will keep track of the size, or number of levels, of the machine.  It will also need to keep track 
of the number of balls in each of the bins at the bottom using a simple array. 
In addition to these characteristics, this class will also need to provide the ability to simulate dropping a 
ball a specified number of times, and the ability to get a description of the number of balls that end up 
in each bin.  This means it will need: 
 A 1‐parameter constructor that accepts the number of level in the machine. 
 void simulateGame(int number) – This method will simulate dropping a ball at the 
top of the board the specified number of times 
 int getBinResult(String) – This method will return the bin number the ball would 
land in, if it followed the path provided (Hint: Think about this one carefully.  You might want to 
try a few examples with paper and pencil so you can see how this works.  It may take you longer 
than you initially realize) 
 String getBinDescription() – This method returns a String describing the bins by 
including the number of balls in each bin

**********************************************************************************


my code thus far... (after so much revamping and changing LOL)




Current Output....

LLRLLLLRRRLRLLLRRRLLRRRLRLRLRL

Desired output


LLRLL = string x
LRRR = string a ... etc, etc.  


Again, thank you in advance for your help....


 
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that jumps out right away is the logic error on line 22 in the fallLevels method.  This is like saying:

You: I want fall levels to be 3.  (In Java, making this call: fallLevels(3))

Path: Ok, I'll use levels = 6  (in your program, levels = 6)

If you're going to do that on line 22, then why pass in a value at all?
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply... the levels 6 should be removed.... I was using that when I was testing the Path class..

I have to pass a value because the professor said so....  I am not really sure what value to pass here.  I can make it 0 and nothing changes....  that's probably not a good thing... huh?


I will remove and see if that helps anything.... Thanks again.
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, I took "levels" out of the method and passed the number I wanted through the calling of the method on Bean Machine.....

I have it generating 6 digits ..... just not each to its own string...

Thank you....  for your help thus far....
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few questions I would ask:

Q1. Where are we dropping the ball? Are we dropping it anywhere in particular, say in the middle of the machine? Or maybe always on the leftmost slot or rightmost slot? Or should that be random, too?
Q2. How wide is this machine? This is related to Q1. The width of the machine will determine how many bins you'll need to catch the ball when it falls through the last level.
Q3. What if, for some reason or strange coincidence, the ball always falls to the same side? What happens when the ball gets to an edge of the machine? Will it be forced to fall to the other side of the peg, opposite the edge?
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OR could I possibly take this string output and break it into groups of 6 characters and scan those groups for char occurrence?
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ella Johnson wrote:OR could I possibly take this string output and break it into groups of 6 characters and scan those groups for char occurrence?


What would be your goal in doing that?
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the ball will land in however many "R"s are in the path....

the bins are levels +1.  There will always be one more bin than level.....

I will eventually code for this, but first I feel like I need to be able to character search each ball path....  

maybe this is where I am misunderstanding.

I haven't finished coding the assignment... i just can't get passed the "string for every ball"  .....  
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might help if you draw something out so you can visualize the problem better:

 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ella Johnson wrote:i just can't get passed the "string for every ball"  .....  


Well, one problem is that you only ever create one Path object.  That's why you end up with a string that represents the path for all the ball drops that you simulate.  That's like using one ball over and over again.  You want to use multiple Path objects, each one you create and run through the machine will represent one ball drop and the unique path it takes through the machine.
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 I am smiling because, I know now, you know the answer....I do not mind thinking to get the answer... I have just been banging my head for a long,  long time.  I should have come here first... lol

I will think about what you said a little more... see, I was thinking slots and bins were the same thing... but they aren't!!!

also, in the directions he asks for a single string variable....  this is confusing me... i guess...

I will think and be back...

Thank you so very much
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it this way:

1. Create a Path object
2. Run the simulation, using that Path object
3. Ask the Path object to report what happened to it. You get back a string of "L" and "R" in random sequence that indicates how the ball fell.
4. Create another Path object, and go back to step 2
5. Repeat steps 2-4 for as many times as you want to run simulations.
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, so there is only one slot... then it funnels down to the bins... does this change anything ?
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't see your last reply before I sent mine...

This does help, I think... I will go code some more and report back...

Thanks
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ella Johnson wrote:ok, so there is only one slot... then it funnels down to the bins... does this change anything ?


Well, think about what would be the differences between these possible setups:



On Machine 1, would it be possible to get a path of "LLLLLLL"? How?
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or is the peg arrangement more like a triangle?  I guess it makes more sense if it's that way.
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




i am still working..  I just wanted to get this image up ....  
pic252B713.jpg
[Thumbnail for pic252B713.jpg]
image example
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok,  I think I broke eclipse...  while it recovers...

can you elaborate a little more on the path object situation.....

He says class Path will need to have a single string variable that holds a series of characters....

Will you elaborate on how to create multiple strings... I think this is a huge part of my problem and I just can't get it... maybe

explain what you understand the loop in Path fallLevles() to say...  maybe that's my breakdown.

I understand it to say...

run a ball down x levels and print the path for that ball.


then in Bean machine I am saying for every ball run the Falllevels(levels) and give me that path...  ugh... my kids come home in 5 hours.. lol... I am no further than when they went to grandmas SMH



 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok.. here is an updated version of my code.... the output looks great...  I am not sure how to call the output from simulateGame to the getBinResult method...

I can call the whole simulateGame method, but that is not giving me the string result I need...  

can you help point out why that is happening?

Thank you... in advance


[/code]

the // are things I've tried, but I am back up to just trying to get the strings to getBinResult().

Thanks
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ella, your code has gotten colder. It was better before. At least your Path class was better, with the exception of the bad line of code that I pointed out.

You can't guess at this, which is what it seems you're doing. You have to think logically. Also, you need to understand what each and every line of code that you write does. The less you understand what you're telling the computer to do, the more likely it is that you're telling it to do something you didn't intend for it to do.

Consider what you wrote:

This is VERY different from what you are actually trying to tell the computer to do. However, the computer can't read your mind so it will just blindly follow these logically incorrect instructions. You need to understand WHY it's logically incorrect. I already told you why: on line 50, you create ONE Path object. Inside the for-loop on line 55, you call the fallLevels() method of that single object.  What is the significance of the 0 value that you pass with that method call?  What is the significance of calling the fallLevels() method on the same object multiple times?

Unless you understand the answers to these questions, you will just keep fumbling around in the dark.

It might help if you go back to plain English:

Translated to Java, that would look something like this:

I have translated the first two lines of English into the equivalent Java. Can you try to translate the other two English commands to Java commands?
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example that illustrates the concepts you need to understand:

Line 2: each Car object will track how many miles it has been driven using its own mileage instance variable.
Line 4-6: creates a Car, assumes mileage is 0.
Line 8-10: creates a Car, sets its current mileage to the number provided.
Line 12-14: updates the mileage to reflect an additional number of miles driven.
Line 16-18: reports the current mileage recorded

Now, read the code in the main() method and try to predict what the output would be. Then copy/paste this example into a Car.java file, compile, and run it. See if you guessed the output correctly. Understand why the output is what it is. This should help you understand what you need to do with your Path class.
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ella Johnson wrote:
can you elaborate a little more on the path object situation.....

He says class Path will need to have a single string variable that holds a series of characters....


I'm not sure what you need elaboration on. This is the code that you wrote:

This code is very relevant to your question. So, is there any line of code above whose purpose and function you can't fully explain?
 
Ella Johnson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your example code.....

I think one of the fundamental things that get me crossed up... are that we are required to use different classes and packages for our programs.  So anytime I see 'static main void... i think this code only belongs on the testing or driver page.

Another thing... is the way the assignments are worded.....  i start focusing on the wording of his instructions...like a class that keeps track of " his detailed description" and he has taken off for not following to a t... so anything outside of what he states he wants... I feel will be counted off.

THAT SAID... i am really, really new and I have very limited understanding of coding....   I will certainly pour over your example and I do think I can figure it out.

however, my children have arrived and I'll have to revisit tonight after they are in bed.

I tell you this, so you don't think I have lost interest and have stopped trying..... If it's too late tonight, and you can be nice enough to check tomorrow, I would be most grateful..


I can't think you enough, I know its frustrating watching someone fumble with something you can do while sleeping  Thanks again,

 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My frustration is NOT so much in the student as it is in the instructors and the kind of instruction they give. It's appalling to think about the kind of unnecessary struggles, IMO, that students like you have to go through because of the lack of context you have from your course materials.

(Edit: I had left out the NOT)
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing I'd like to point out is the importance of good names. In my opinion, good names help you clarify your ideas and your understanding of the program and how it solves a problem. Bad names mislead and confuse you.  I don't think name Path is a very good choice. To me, this "thing" does not represent a path that is taken through the machine as much as it represents a Ball.  You don't drop a path into the machine, you drop a ball. The path is what the ball creates as it randomly bounces as it falls through the machine. If anything, you would tell a Ball object to fall X number of times and then ask it, "Hey, what path did you take while falling and hitting pegs X number of times?"  In Java, that would be coded as something like this:

What this code implies is that every time the ball falls, it tracks which way it bounced (left or right) and you can tell it to report what it has tracked so far by calling the path() method.

Since your instructions were to name your class "Path", you should probably just go with what you were told. If it were me, I would bring this up as a point of discussion with my instructor.

You may think that this name thing is a trivial matter but when it comes to programming, good names matter.
 
Junilu Lacar
Marshal
Posts: 17009
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ella, you wrote:

First thing you need to do is to align your code properly. Poor formatting can make your code very difficult to read and understand and it can often mislead you into thinking your code does something that it doesn't. Since you indicated that you use Eclipse, you should use Ctrl+Shift+F (format code) often to automatically format and indent your code properly. This is what Eclipse will do for you when you press Ctrl+Shift+F:

This code illustrates how poorly chosen names can make your code unclear, confusing, and misleading.  What does s1 stand for? Can you find another name that explains its purpose more clearly?  What about count? Is that really a count? What is it counting? Look carefully at how you're using it. Does that look like a count to you? On line 5, why are you changing the value of s1? What's the point of allowing that value to be passed in as a parameter if you're just going to use a different value anyway?  

It looks like you were trying to kill two birds with this one method: increase the count of balls in a bin and figure out which bin the ball fell into.  That's a bad idea. A method should focus on doing ONE and ONLY ONE thing.  If you need to do two things, then write two methods. The logic error in this code stems directly from violating this principle.  If the methods are somehow related, then use parameters or instance variables to allow them to share information with each other. Prefer using parameters and local variables instead of instance variables when you do that.

 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic