Reputation: 117
I'm doing an assignment and I am confused as to what I have been asked to do. The specification is:
So they want me to create a constructor which takes two paramters, one for the question and one for the answer. And then they want me to create a getAnswer and getQuestion method which contains the answer and question for eah flashcard. But if the flashcard contains two strings then woudn't that mean that one flashcard has both the answer and question within it? Which would mean that I coudn't have a get answer and get question method as they cant be seperated?
import java.util.ArrayList;
public class FlashCard {
ArrayList<FlashCard> cardlist = new ArrayList<FlashCard>();
FlashCard(String question, String answer)
{
}
public ArrayList<FlashCard> getQuestion() {
cardlist.add(new FlashCard("1Q - By what initials was Franklin Roosevelt better known?", "1A - FDR"));
cardlist.add(new FlashCard("2Q - Which number president was Franklin Roosevelt?", "2A - 32"));
cardlist.add(new FlashCard("3Q - Which state was Franklin Roosevelt3 born in?", "3A - New York"));
cardlist.add(new FlashCard("4Q - In which year did Roosevelt become Governor of New York?", "4A - 1929"));
cardlist.add(new FlashCard("5Q - What was the name of Franklin Roosevelt's wife?", "5A - Elenor"));
cardlist.add(new FlashCard("6Q - How many children did Franklin Roosevelt have?", "6A - 6"));
cardlist.add(new FlashCard("7Q - From which university did Franklin Roosevelt graduate with an A.B in history?", "7A - Harvard"));
cardlist.add(new FlashCard("8Q - What was the first name of Franklin Roosevelt's 5th cousin, who was also President?", "8A - Theodore"));
cardlist.add(new FlashCard("9Q - Which disease is believed to be the causes of Franklin Roosevelt's paralysis?", "9A - Polio"));
cardlist.add(new FlashCard("10Q - At what age did Franklin Roosevelt die?", "10A - 63"));
}
public ArrayList<FlashCard> getAnswer() {
cardlist.add(new FlashCard("1Q - By what initials was Franklin Roosevelt better known?", "1A - FDR"));
cardlist.add(new FlashCard("2Q - Which number president was Franklin Roosevelt?", "2A - 32"));
cardlist.add(new FlashCard("3Q - Which state was Franklin Roosevelt3 born in?", "3A - New York"));
cardlist.add(new FlashCard("4Q - In which year did Roosevelt become Governor of New York?", "4A - 1929"));
cardlist.add(new FlashCard("5Q - What was the name of Franklin Roosevelt's wife?", "5A - Elenor"));
cardlist.add(new FlashCard("6Q - How many children did Franklin Roosevelt have?", "6A - 6"));
cardlist.add(new FlashCard("7Q - From which university did Franklin Roosevelt graduate with an A.B in history?", "7A - Harvard"));
cardlist.add(new FlashCard("8Q - What was the first name of Franklin Roosevelt's 5th cousin, who was also President?", "8A - Theodore"));
cardlist.add(new FlashCard("9Q - Which disease is believed to be the causes of Franklin Roosevelt's paralysis?", "9A - Polio"));
cardlist.add(new FlashCard("10Q - At what age did Franklin Roosevelt die?", "10A - 63"));
}
}
They also need to return ArrayList<FlashCard>
so I'm really confused as to how I would structure this so that the getQuestions()
method returns flashcard questions the same for the answers. Any help would be very much appreciated, thanks.
Upvotes: 1
Views: 138
Reputation: 936
It sounds funny, so i have taken your requirements and have simulated an mini flash card game, maybe it will be easier to understand your requirements on a example.
public class PlaygroundMain {
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
PlaygroundMain gameDemo = new PlaygroundMain();
System.out.println("Shuffling the cards");
List<FlashCard> shuffledDeck = gameDemo.getQuestions();
Collections.shuffle(shuffledDeck);
System.out.println("Cards Shuffled!");
int score = 0;
for (FlashCard card : shuffledDeck) {
System.out.println(card.getQuestion());
final String answer = sc.nextLine();
if (answer.trim().equals(card.getAnswer())) {
System.out.println("You have answered right!, giving you a point");
score++;
} else {
System.out.println("Sorry, you have answered wrong, the correct answer is: "+card.getAnswer());
}
if (shuffledDeck.indexOf(card) != shuffledDeck.size()-1) {
System.out.println("Preparing next question...");
}
}
System.out.println("Your score is: "+score);
}
public List<FlashCard> getQuestions() {
FlashCard c1 = new FlashCard("Q1: What is my name?", "Alfred");
FlashCard c2 = new FlashCard("Q2: What is my age?", "26");
return Arrays.asList(c1, c2);
}
class FlashCard {
private final String question;
private final String answer;
public FlashCard(String question, String answer) {
this.question = question;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public String getAnswer() {
return answer;
}
}
}
Upvotes: 1
Reputation: 3691
That means a FlashCard
has a single question and a single answer.
Both constructor parameters should be stored in your FlashCard
object. That's its attributes.
The getQuestion
and getAnswer
methods are getters, they simply return the corresponding attribute, here the FlashCard unique question/answer.
But if the flashcard contains two strings then wouldn't that mean that one flashcard has both the answer and question within it?
Yes it does, that the purpose of the card. How could you get the answer if it's not bound to the question?
Which would mean that I coudn't have a get answer and get question method as they cant be seperated?
Well, the answer and the question can't be separated as they are both contained in a FlashCard
object. But you can call getQuestion
without calling getAnswer
(and the other way too).
The code you put in your getQuestion
method should not be in this class, same for the declaration of the cardList
. The list contains FlashCard
but a FlashCard
is not supposed to contains a list of cards. That code is right to create multiple flash cards. You can put it somewhere else, like in your main
method or in another class that handle the card list:
public static void main(String [] args)
{
ArrayList<FlashCard> cardlist = new ArrayList<FlashCard>();
cardlist.add(new FlashCard("1Q - By what initials was Franklin Roosevelt better known?", "1A - FDR"));
cardlist.add(new FlashCard("2Q - Which number president was Franklin Roosevelt?", "2A - 32"));
cardlist.add(new FlashCard("3Q - Which state was Franklin Roosevelt3 born in?", "3A - New York"));
System.out.println("first question: "+cardList.get(0).getQuestion()+ " - answer: "+cardList.get(0).getAnswer());
}
Upvotes: 1
Reputation: 1676
Lets try to clear it:
But if the flashcard contains two strings then woudn't that mean that one flashcard has both the answer and question within it?
Yes. The class has two members. Question and answer. They are bind together in the same moment when the constructor is called. The could and should be final. Like in private final String question;
Which would mean that I coudn't have a get answer and get question method as they cant be seperated?
No. The class has two methods, which can be called any time. They give the same result any time. Like in public String getQuestion(){ return question; }
The only thing what is wrong with your code is that you create the list of the FlashCards twice. Don´t do that. Move the code into a single point. Maybe put in in a separate class which contains the list of FlashCards "Deck". So when you create a 'Deck' it contains a list of all FlashCards.
Upvotes: 0