Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

arraylist shuffle  RSS feed

 
Kristers Drozds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
So i got some issues about arraylist shuffle.
I'm making a "Programming test".
There is around 100 questions, so i wanted to make so it shuffles all array.
I did that, but how can i make it so answers array knows - what question is about to display after shuffle so i can display correct answers for that question?

 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Difficult to determine if you chose right approach in general, for solving that problem.
Could you please specify how that test question sounds?

[edit] I am sorry, I misread your post. I thought you're solving test, it appears you're creating one.

Still, question remains similarly, do you have whole idea what your program is supposed to be doing? Could you please explain a bit more about it.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, have a look at this tutorial (<-- link). Read carefully, there is a data structure which might would fit better in your case. Think of which implementation that could be.
 
Kristers Drozds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch.

Difficult to determine if you chose right approach in general, for solving that problem.
Could you please specify how that test question sounds?

[edit] I am sorry, I misread your post. I thought you're solving test, it appears you're creating one.

Still, question remains similarly, do you have whole idea what your program is supposed to be doing? Could you please explain a bit more about it.

Hey, thanks for the fast response.

I'm creating a "Test".
There will be around 100 Questions in a Array. Each Question will hold 4 answers(4 radio buttons).
1. Question 1
Answer 1 Answer 2 Answer 3 Answer4
The Question array will be shuffled. And the first 0-9 index i will display them as questions after shuffle, so when you open the program again, it's not the same questions over, over again.

Don't know still if i'm gonna put all 10 questions in 1 Jframe or on button click.

I hope you did get my idea.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristers Drozds wrote:. . . Don't know still if i'm gonna put all 10 questions in 1 Jframe or on button click. . . .
Until you have worked out what you want to do, you cannot even think of things like JFrames. You may need to turn your computer off and write down in simple words on paper what you are planning to do. That applies especially to GUIs. If th program doesn't work at the command line, don't try to put a GUI on it.
Don't abbreviate Question to Quest.
Why are you using arrays rather than Lists? Why have you used the + operator so often in lines 13‑15?
 
Kristers Drozds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i got program working on console, first semester i made it without gui. But on 2 semester i want to make it with gui and more complete.

I have 2 different arrays:
*questions array
*answers array
The Question is, how can i put answers to the correct question after array shuffle?

In my old program i used this(no shuffle array):


The first semester program, i can upload or send it to you as well.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is really simple: get some objects and beat them into submission. Create a Question class which encapsulates both question and answer. When you sort/search/randomise the questions, all the answers will follow.
 
Kristers Drozds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The answer is really simple: get some objects and beat them into submission. Create a Question class which encapsulates both question and answer. When you sort/search/randomise the questions, all the answers will follow.

Last question :


These are answers for each Question, and can i somehow split them on 4 different radio buttons?
Cause answers[0] will display :
(1) centralizētā vadība (2) sacensību princips (3) ar marķiera palīdzību (4) token passing
and can i split them so i can put each of them on different radio button?
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to use them separately, then keep them separated instead of mashing everything together in a single string like that.

That's like mixing flour, eggs, water, and sugar in a bowl and then saying "How do I separate some of the sugar so I can sprinkle it on top of the pancake later?"
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initially I was thinking about to suggest you to ease your life with HashMap instead of parallel arrays, but Campbell Ritchie is right, that would be a changing of one bad way doing this with another bad way.

So you most likely need to consider of moving to Objectville. Have you learned about writting classes yet?

I can't remember in which book I've seen such problem, I think it was in one of: either "Head First Java" or Cay Hortsmann "Java for Everyone, Late Objects". If I'm not mistaken, there are good suggestions and hints how to implement it in object oriented way.

If you're thinking to move along with this app further, you could try to look up for those books in your local library.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The term Objectville appears in Head First Java.
 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In OO you would create a Question class. Your main class would contain a List<Question>. The Question class would contain a List<String> for the answers and an int of which answer is correct You main class would shuffle the Question list ... Collections.shuffle( questionList ) ... (if I remember correctly).

You could add another layer of abstraction by having an Answer class. An Answer class would have a String for the question and a boolean to flag the correct answer. In this way you could also shuffle the answers.
 
Kristers Drozds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Initially I was thinking about to suggest you to ease your life with HashMap instead of parallel arrays, but Campbell Ritchie is right, that would be a changing of one bad way doing this with another bad way.

So you most likely need to consider of moving to Objectville. Have you learned about writting classes yet?

I can't remember in which book I've seen such problem, I think it was in one of: either "Head First Java" or Cay Hortsmann "Java for Everyone, Late Objects". If I'm not mistaken, there are good suggestions and hints how to implement it in object oriented way.

If you're thinking to move along with this app further, you could try to look up for those books in your local library.


Yes, i kinda know a basic about Object Oriented Programming and how to write class.
I just didn't get the idea... so i should make new class + object for questions and new for answers as well? Or i need to mash them in 1 class?
Dont get it how to make an answers follow the question to be honest.


main:
 
Kristers Drozds
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:In OO you would create a Question class. Your main class would contain a List<Question>. The Question class would contain a List<String> for the answers and an int of which answer is correct You main class would shuffle the Question list ... Collections.shuffle( questionList ) ... (if I remember correctly).

You could add another layer of abstraction by having an Answer class. An Answer class would have a String for the question and a boolean to flag the correct answer. In this way you could also shuffle the answers.


Thanks didnt see this post earlier, gonna try it .
 
Carey Brown
Bartender
Posts: 3027
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristers Drozds wrote:
Carey Brown wrote:In OO you would create a Question class. Your main class would contain a List<Question>. The Question class would contain a List<String> for the answers and an int of which answer is correct You main class would shuffle the Question list ... Collections.shuffle( questionList ) ... (if I remember correctly).

You could add another layer of abstraction by having an Answer class. An Answer class would have a String for the question and a boolean to flag the correct answer. In this way you could also shuffle the answers.


Thanks didnt see this post earlier, gonna try it .

P.S. If you go the Answer class route, then the Question class would have a List<Answer> instead of a List<String>.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!