• Post Reply Bookmark Topic Watch Topic
  • New Topic

Self-taught Java Beginner having trouble with Bowling project  RSS feed

 
John Welch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been teaching myself Java to learn a new skill, and trying to write my own bowling project. It has three classes: a driver, an input, and a game functions. So far, it counts frames, and tallies the normal score well enough. However, it doesn't count bowls, and doesn't apply my strike or spare modifiers. Any help would be appreciated. I'll post my code, and then output from a run using a variety of numbers.







OUTPUT
Please input the score of bowl 1: bowl 1=
10
Frame=1
Bowl=0
Score=10
Please input the score of bowl 2: bowl 2=
9
Frame=1
Bowl=0
Score=19
Please input the score of bowl 1: bowl 1=
8
Frame=2
Bowl=0
Score=27
Please input the score of bowl 2: bowl 2=
7
Frame=2
Bowl=0
Score=34
Please input the score of bowl 1: bowl 1=
6
Frame=3
Bowl=0
Score=40
Please input the score of bowl 2: bowl 2=
5
Frame=3
Bowl=0
Score=45
Please input the score of bowl 1: bowl 1=
4
Frame=4
Bowl=0
Score=49
Please input the score of bowl 2: bowl 2=
3
Frame=4
Bowl=0
Score=52
Please input the score of bowl 1: bowl 1=
2
Frame=5
Bowl=0
Score=54
Please input the score of bowl 2: bowl 2=
1
Frame=5
Bowl=0
Score=55
Please input the score of bowl 1: bowl 1=
0
Frame=6
Bowl=0
Score=55
Please input the score of bowl 2: bowl 2=
10
Frame=6
Bowl=0
Score=65
Please input the score of bowl 1: bowl 1=
9
Frame=7
Bowl=0
Score=74
Please input the score of bowl 2: bowl 2=
8
Frame=7
Bowl=0
Score=82
Please input the score of bowl 1: bowl 1=
7
Frame=8
Bowl=0
Score=89
Please input the score of bowl 2: bowl 2=
6
Frame=8
Bowl=0
Score=95
Please input the score of bowl 1: bowl 1=
5
Frame=9
Bowl=0
Score=100
Please input the score of bowl 2: bowl 2=
4
Frame=9
Bowl=0
Score=104
Please input the score of bowl 1: bowl 1=
3
Frame=10
Bowl=0
Score=107
Please input the score of bowl 2: bowl 2=
2
Frame=10
Bowl=0
Score=109

 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, it doesn't count bowls, and doesn't apply my strike or spare modifiers. Any help would be appreciated.

Sometimes a flowchart will help you solidify what you need to do and divide it into smaller blocks. In general try to write code as compactly as possible, reusing what you can. The less, the better.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in this article by Uncle Bob: http://www.objectmentor.com/resources/articles/xpepisode.htm

Robert "Uncle Bob" Martin is an author, speaker, and well-known industry figure whose books and articles have a lot of good stuff that help developers hone their skills.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, welcome to Java Ranch!

Good for you for teaching yourself Java. Eventually, you may want to get a book though, or even join the Cattle Drive here, just to keep yourself from falling into bad habits and misconceptions. I haven't run your code here, but it looks like the bowling counter, which is a member variable, is only assigned a value when it is used as a loop counter. That's not a great idea anyway, but let's ignore that for now.

The Display() method (which should be lowercase display()) prints out the value of the member variable, so why is it always printing 0? The problem is you re-declare bowl in a couple of the loops, leading to a common problem called "shielding". That is, you are assigning a value to a local variable called bowl instead of to the member variable bowl as you intended. It's a common problem for beginners. Believe me, it's much easier to fall into this trap in C or C++, but as you found out, it's not impossible even with Java. So, your loops should not be "for (int bowl = 1; ...)", but "for (bowl = 1)". Some of your loops are correct in the listing, so it possible those aren't being run at all. You'll have to investigate that further.

Some other points:

Your loops generally are set so the body will always run exactly twice, and then you have an if-else structure in the body to do one thing the first time through the loop, and a different thing the second time. If you think about it then, do you really need the loop at all?

It's generally a bad idea to compare expressions to boolean values (true/false). For example, don't write "if (isSpare() == true)", just "If (isSpare())".

I'm not positive about this one, but it seems you expect the "else" section of if-else to handle thrown exceptions. That's not the case. You need a catch block for that. In fact, saying "if (true) ... else if (false) ... else ..." makes no sense. (See lines 91-111 of your GameRules listing.) Often the compiler will tell you this, but you've fooled it in this case. It should just be, "if (true) ... else ..."
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is very long and tough to read. you should fix indentation, delete redundant and obviously correct code, and just post 10 lines or so of what you feel might be incorrect.

From what I could gather from skimming through is that you seem to gathering data and scoring with the same processes. I'm not sure why you're using several arrays of length 30 or "score modifier" type methods. With just one multi-dimensional array you can capture a user's input in the sense of how many pins were knocked down on which rolls. Scoring the game based on that data should be a completely separate process. It seems like that would be a moderately difficult algorithm, so you may be slightly out of your depth as a beginner.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually think it would be really interesting to see what a total newbie would do if put in a situation similar to what is described in the article by Uncle Bob. I have done workshops on TDD at work and the conversations have never gone the same way. Somebody always has a different idea or goes off on a different tangent. I just try to go with the flow instead of trying to force it back to the "script" of the article. Anyway, I hope you read through that article, John, and see how experienced developers work and think.

Here's that link again (hint, hint): http://www.objectmentor.com/resources/articles/xpepisode.htm
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Interesting comment you have there on line 37. On line 36, you asked for a new Error object to be thrown, so were you surprised that it actually happened? Also, you should rarely, if ever, need to explicitly throw an Error() in your code like this. That's the system's job. You could throw an Exception -- and the choice of doing so or not can lead to a lot of questions and discussion -- but not an Error. They are not the same things.
 
John Welch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, guys. Thanks for all of the feedback. I'll be sure to read up on that article, Junilu. It'll take me a while to process all of this information, and do something useful with it. The errors were more a reminder of where my failure conditions were. I should probably change that. Greg, I really appreciate the direction. Funnily enough, I started working with C++ first before I decided that I liked Java better. I am using the oracle "tracts", and have read everything up to file I/O, but a book would probably be welcome. I'll make a better post when I've had a chance to process.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And John, if that article piques your interest in pair programming and test-driven development, it's not unheard of for me to do something like what RCM and RSK did. Check out the Testing forum sometime. Just in case you were wondering...

http://www.coderanch.com/t/641831/Testing/book-Practical-Unit-Testing-JUnit

 
John Welch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ALRIGHT! I got it to work! Once the shielding issue was out of the way, I managed to tweak a few things in my modifier methods. I understand now the comment about loops. There was no real reason to have them, and in fact they were what was breaking my code. Pulled the frame for loop, replaced with with frame++, and then it worked fine. Thanks for all the help guys.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Welch wrote:ALRIGHT! I got it to work!

Well done!

I hope you don't mind a bit of constructive criticism though (and don't worry; it's a common one for beginners...and me when I'm not thinking straight ):
Your program is very procedural.

And by that I mean that it relies heavily on getting the code right, because the elements of the problem are almost all solved with pieces of code, rather than with objects (or classes).

If I was looking at a "bowling" problem, I suspect I'd come up with classes like: Player, Frame, Score, and possibly even Pin, to encapsulate "things" that my program needs to know about. And that's before you even start on things like "drivers".
A couple of others might be Strike and Spare; although I'm not sure that they actually qualify as classes (you get a feel for that kind of stuff after a while; but not if you don't do it ).

How would I implement them? No idea, but they're words (specifically nouns) that crop up a lot when we talk about bowling; and that's usually a good indication that they might be needed, so I'd definitely list them down as candidates.

I guess what I'm trying to say is: Don't be afraid to create classes. Or interfaces.

They're the essence of Java, and I can't think of too many moderate-size problems I've solved that didn't involve at least a dozen of them - and I've written several programs (or applications) that involved more than a hundred.

At the risk of sounding pretentious: They're your building blocks to understanding the problem, rather than just coding it.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!