• 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

Beginner Yahtzee Progam

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

I am relatively new to Java and while searching through the other posts i found a topic on a Yahtzee program. I decided to give it a go....but would love to hear input on what ive done wrong or improvements that could be made?

This is my first attempt, it is only for user input at the command line as i dont have much experience creating GUI's. Also lack of completed comments due to not much time as off 2 work in a min but figured u would get wots going on neway

First Class - Yahtzee



Second Class - Dice



Third and last class - Score



The program seems to work fine, only put a few bits of error checking etc in atm. sure it could be crashed by a user somewhere lol!

Thank you and i am looking forward to hearing back.

John

[ January 25, 2006: Message edited by: John Bartlett ]
[ January 25, 2006: Message edited by: John Bartlett ]
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

im not quite sure what your question is. There's quite alot of code, and
I havn't read it in detail, but overall it looks ok. You've got a class
for each area of responsibility:

- Yahtzee (main)
- Dice
- Score


What's the purpose of the program? school project, or are you just interested in learning Java?

/Svend Rost
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Looks like a good start. There are always ways to improve on code, and do that with my own code from time to time. But the best way is to do like you've done and ask for constructive but honest critique.

Generally it looks like the organization is good. Consider that the Yahtzee class itself could be designed more like an object, i.e. with more instance side and fewer statics (fields and methods). It's fine as is, however. The main method could be decomposed a bit more to make things easier to follow.

Minor nits: one of the strings should read "you have chosen too many dice" (instead of "to"). I'd also prefer the class name "Die" instead of "Dice".

But of course the GUI is where it's at. Tackling a pretty user interface is a good amount of work. However, I think that could be your next best step. What would it take so that you could build a Swing GUI, whereby both the Swing GUI and a text interface could both use the same domain classes (Yahtzee, Dice, Score)? I think it'd be a pretty educational exercise, and would demonstrate an important concept of design: you shouldn't mix presentation (user interface code, such as System.out) with underlying logic.

If you go that route, start with a simple GUI. For example, start by just showing text strings in a window for the die roll. Then incrementally add the things that will make it nicer: display images, improve layout, add animation (much later), etc.

I think it's a great start, and better than a lot of the code that I see new Java programmers come up with.

Best regards,
Jeff
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you jeff for ur help and honest suggestions.

I will hopefully attempt the GUI interface in the next couple of days, will give my book a read over and see what i can do.

To answer Svend Rost question my question was just asking for constructive criticism on my coding, as it is easy to code things but harder to get someones (more experienced programmers) opinion on whether it is actually any good and suggestions/improvements.

The reason i am doing this program, it is a bit of both...i am currently in my second year at uni on a Computer Science course, and have really enjoyed learning java over the time there (although the first years course was very noddy). I would like to continue Java programming into a career if possible so i am trying to increase my understanding by coding and reading as much as possible.

How long have you both been programming for?

Although i was doing some noddy stuff at uni, i have only really started to teach myself Java for the last 6 months and i was wondering what kind of level you are expected to be at when going for a Java Programming job?

Also whether that level changes depending on if your on a placement year or actually beginning full time work?

Thank you,

John

[ January 25, 2006: Message edited by: John Bartlett ]
[ January 25, 2006: Message edited by: John Bartlett ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overall, your code looks good. You use consistent formatting and coding conventions. This is a real plus in my book. Which coding conventions to use are arbitrary, imo. Picking them and using them consistently is crucial in making code readable. I have a few comments that might help you improve your code:

1) There are several places where loops would be helpful. For example, the createDice() and rollDice() methods. Whenever you need to access all the elemnts in an array or similar structure with a numerical index, you should probably use a for loop. There are also several return statements that use arrays to calculate the return value. Can you find a way to use a for loop instead of manually listing each element in the array?
2) When you find yourself using variable names like var1, var2, var3, etc., you should consider using an array. For example, in your sortNumbers() method, you have

If you use an array here insetead of 6 separate variables, the body of the for loop could be a single line of code.

3) The id field in the Dice class seems unnecessary to me. It doesn't seem to be used in any meaningful way. The index that where a Dice object is stored is part of the ArrayList which is external to the Dice itself.

I hope these comments are helpful. If you decide to create a GUI for this, good luck with that!

Layne
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Bartlett:
How long have you both been programming for?



Way too long. But I still enjoy it more than any other aspect of my job(s).

i have only really started to teach myself Java for the last 6 months and i was wondering what kind of level you are expected to be at when going for a Java Programming job?

That all depends on what people are looking for. Your best bet is to not just learn about programming Java, but also to learn about software design at the same time. Read up on object-oriented programming concepts, design concepts, testing (maybe look at test-driven development), design patterns. At the same time, start looking at a few enterprise level concepts (a lot of what's in J2EE; for example, persistence and distributed computing). Perhaps take a look at some of the jobs that interest you and take a look at some of the technology skills they seek. But remember that your best bet in the long run is to bang on the design side of things. Learn how to build solid, high-quality code.

Most importantly, always seek to learn more about your career. The ones who are willing to continually challenge themselves are the ones I look to hire. They're also the ones who rise to the top in any organization.

When I interview entry level candidates, I look for aptitude and attitude.

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

Originally posted by John Bartlett:

To answer Svend Rost question my question was just asking for constructive criticism on my coding, as it is easy to code things but harder to get someones (more experienced programmers) opinion on whether it is actually any good and suggestions/improvements.


Are you aware of "design patterns" ? (Gang of four, PLOP books, Larman, Fowler)
If you were making a "game framework" you could improve the design, making
the software more resuable - but it's most likely not worth the trouble if
your only making a yahtzee program.

Originally posted by John Bartlett:

How long have you both been programming for?


I've been coding java for since 2000. Before that I was "into" delphi
and VB (yes yes.. I know)

Originally posted by John Bartlett:

...i was wondering what kind of level you are expected to be at when going for a Java Programming job?


I think "it depends". I got my first development job after around 2-3 years
at uni. Worked there part-time while studying. I'd say, that a student
with 2-3 years of CS "on his back" should be able to work as a programmer.
If your smart, you can also get a job earlier - it's all about problem
solving .. you'll learn the "syntax" along the way.

As a side note: Learn several languages. Technologies changes, but the
basic concepts dont. A few languages you could "check out": C/C++, ML or Haskell(functionel
programming), Prolog (Logic programming), Python

I know that it depends from country to country - and even from town to town
but it's my opinion, that you'll gain alot by having a programming job
besides ones study, since you get to try the things out you learn at uni.
Just remember to set your priorities (i.e. in exam time, uni is the nr 1
priority).

/Svend Rost
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to everyone for your help, it has been very much appreciated.

If i get any more probs i will be sure to post bk here.

Regards,

John
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone,

I have made a start on the GUI side of this program and tbh i am a bit stumped on where to start.

1) hud i implement a GUI using the Yahtzee class created above

2) Shud i create a seperate class e.g YahtzeeGUI and then use instances of the other class'?

Thank you,

John
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd try to build YahtzeeGUI and work up your existing classes (Yahtzee, Dice, ...) so that YahtzeeGUI can use them. One potential first step is to build YahtzeeGUI so that it just uses System.out/System.in statements. Then see what it takes to incrementally cut over to a GUI implementation of YahtzeeGUI.

-Jeff
 
reply
    Bookmark Topic Watch Topic
  • New Topic