1

Good day, i will try and explain myself here. i am trying to create like a quiz system where an image is mapped to an answer and other wrong answers are provided. An example is like this:

<Image> <answer A> <answer B> <answer c>

I was thinking of using a hash map for the image and correct answer, and maybe an array or an arrayList for the other wrong answers. But i don't know if hash maps works well this way.

what would be the best solution to go about this or any other ideas?

by the way, i would be inputing the values myself and the Image is stored in a resource so its not being downloaded. Thank you

2
  • 2
    It's an object oriented language. Make a class for it. Commented Jan 9, 2011 at 21:32
  • yes you are very correct.. i need to switch it to a mobile platform later thats why i was thinking of the collections and i also need to do some sorting. Commented Jan 10, 2011 at 0:08

4 Answers 4

4

Why not just define an object with an image member, and the correct/incorrect answers ?

public class ImageAnswer {
  private final Image image;
  private final String answer;
  private final String[] wrongAnswers;

  // constructor and methods follow.
}

(note sure about the name - depending on its behaviour you can rename appropriately)

That way you can define everything together (atomically) using a constructor, and embed behaviour particular to that within the object.

Remember - objects should do this for you, not provide fields for you to do things with.

Sign up to request clarification or add additional context in comments.

Comments

1

What you want to do is create an object you can place all of that data in and keep it in one place.

So for example:

class Question {
    Image image;
    String correctAnswer;
    String[] wrongAnswers;
}

Then you can just play around with Question objects instead of trying to keep everything synchronized across multiple collections.

Comments

0

How about following:

class Answer{
String description;
Boolean correct; // true if correct and false otherwise
}

and a HashMap:

HashMap<Image,ArrayList<Answer>> quiz;

2 Comments

yeah.. using the hash map is what i thought of.. but then, won't have to do something similar for the other wrong answers
You will have information of correct or not correct in the Answer object itself, correct = true; indicates a correct answer and correct = false indicates wrong answer.
0

I would do it:

public class Question 
{
  private Image image;
  private String[] options;
  private int rightAnswerIndex;

  //the rest of the code; you could create a constructor that sets all the member vars
  ...

}

That way it would be much simpler to iterate through and display the options for the user. If you put the right answer separate from the other options you would always have it displayed as the first or last option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.