• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return Items in random order from an array of enum elements.  RSS feed

 
Francisco Gallardo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
I am a Java Beginner, but I am really interested in mastering Java language. I have the next question: How can I make a method that returns items in random order from an array of “enum” elements contained in an Enumeration?
The only hint I have is:


I really appreciate your help.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is not an Enumeration. An Enumeration is a legacy class which has been superseded by iterator. I don't know where you got that code from but I don't think it helps you at all.
You cannot make a method which returns items. Not in normal order, nor in random order, because a method only ever returns one thing. You can make a method which creates a data structure containing all the elements of an enum and you can shuffle that data structure and return a reference to that.
 
Francisco Gallardo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

This is the Enumeration I am talking about:


And this is the MMRow class:



How can I make a Random Row of these enumeration items everytime the MasterMind game starts?
Hint: I have to put some code in the "FIXME" parts.

There is also a Test class:


 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, that is not an Enumeration but an enumerated type. I am pleased to see you have used documentation comments, but your code style needs a bit of tidying. Never try to squash a method into one line. Each statement merits a line by itself and the brace also merits its own line.
Your create(String) method is very brittle. you should not return null; if you are given an incorrect argument you should be throwing IllegalArgumentExceptions left right and centre. All ifs, whiles, etc. should be followed by braces to delineate their body, even if it is only one line.

You have got the code tags in the wrong places. I shall try to correct that.

How do you think you are going to create a row of differently coloured pegs? But not on screen. Turn your computer off and write it with a pencil and paper and (most important) eraser.
Who wrote those methods? Can you change them?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!