This site requires JavaScript, please enable it in your browser!
Greenfoot back
adlafforest
adlafforest wrote ...

2017/3/10

Geography quiz

adlafforest adlafforest

2017/3/10

#
How do I associate/connect a certain picture to each quiz question? I would like to have the map of a specific country show up for each question, and have the player select the correct name, among the four possible answers, for said country. String questionText = "What country is this?"; ArrayList<Answer> answers = new ArrayList<Answer>(); answers.add(new Answer("Lithuania", false)); answers.add(new Answer("Latvia", false)); answers.add(new Answer("Estonia", true)); answers.add(new Answer("Belarus", false));
danpost danpost

2017/3/10

#
Change 'ArrayList<Answer>" to 'ArrayList<Actor>'. Then, have an Actor subclass to display the image and create and add it to the list:
answers.add(new Picture("Estonia.png"));
with the following class:
import greenfoot.*;

public class Picture extends Actor
{
    public Picture(String filename)
    {
        setImage(new GreenfootImage(filename));
    }
}
adlafforest adlafforest

2017/3/13

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Collections; import java.util.regex.Pattern; import java.util.regex.Matcher; /** * The workd in which the questions go. * * @author (your name) * @version (a version number or a date) */ public class QuizWorld extends MyWorld { private List<Question> questions; private int questionNum; private int wrong; private int correct; /** * Questions and answers. * */ public QuizWorld() { super(500, 400, 1); questions = new ArrayList<Question>(); String questionText = "What country is this?"; ArrayList<Actor> answers = new ArrayList<Actor>(); answers.add(new Picture("France.png")); answers.add(new Answer("Belgium", false)); answers.add(new Answer("Switzerland", false)); answers.add(new Answer("France", true)); answers.add(new Answer("Germany", false)); Collections.shuffle(answers); Question question = new Question(questionText, answer); String explanation = "This is France"; question.setExplanation(explanation); questions.add(question); This is the beginning of what I have in my QuizWorld. The bold "answer" is showing up as wrong. I think I need to add an "answer" variable but I'm not sure how to do it. Thank you
adlafforest adlafforest

2017/3/13

#
In addition, the "super" is underlined in red and I don't understand why it is wrong.
danpost danpost

2017/3/13

#
If you are trying to pass the 'answers' ArrayList object to the new Question object, then you need to use 'answers' (with an 's' at the end).
adlafforest adlafforest

2017/3/15

#
super(600, 400, 1); questions = new ArrayList<Question>(); String questionText = "What country is this?"; ArrayList<Actor> answers = new ArrayList<Actor>(); answers.add(new Picture("France.png")); answers.add(new Answer("Belgium", false)); answers.add(new Answer("Switzerland", false)); answers.add(new Answer("France", true)); answers.add(new Answer("Germany", false)); Collections.shuffle(answers); Question question = new Question(questionText, answers); String explanation = "This is France"; question.setExplanation(explanation); questions.add(question); String questionText2 = "What country is this?"; ArrayList<Actor> answers2 = new ArrayList<Actor>(); answers2.add(new Answer("Sweden", false)); answers2.add(new Answer("Finland", true)); answers2.add(new Answer("Denmark", false)); answers2.add(new Answer("Norway", false)); Collections.shuffle(answers2); Question question2 = new Question(questionText2, answers2); String explanation2 = "This is Finland"; question2.setExplanation(explanation2); questions.add(question2); My program is not working and I'm not certain why that is. Do you have any recommendations?
danpost danpost

2017/3/15

#
A couple of suggestions: (1) add the answers to the list and shuffle them before adding the picture to it; (2) you need the picture of Finland added to the second answer set.
You need to login to post a reply.