GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Problem w/Quiz Card Builder in Head First Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem w/Quiz Card Builder in Head First" Watch "Problem w/Quiz Card Builder in Head First" New topic
Author

Problem w/Quiz Card Builder in Head First

Steve Stanicki
Greenhorn

Joined: Sep 24, 2005
Posts: 23
Hello All,
I am working thru Head First Java, and I got stuck on the Quiz Card app on page 451.

Specifically the QuizCard class is confusing me. I think I understand that it represents each Q & A as a card object, but I'm not sure how to write/use it.

Does it 'get' the questions and answers, and 'set' them for output to the text file? Are we supposed to concat them together for each line in the file or use it to parse out each Q & A?

I'm just not getting it at this point.

Very confusing to me.

I got this far in creating the class:

import java.util.*;
import java.io.*;

public class QuizCard{
public String question;
public String answer;

public QuizCard(q,a){

}

public void getQuestion(){

question = q;
return q;
}

public void getAnswer(){

answer = a;
return a;
}

Could someone help me with some direction please?

Thanks for your help (again),

Steve
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
A few things may help.

The code on 450-451 is stand alone. If you just copy it line by line it will work. Just FYI, in your code, you have a couple of issues.

First of all: your constructor. You have it taking q and a but you have not told the system what type q and a are. They are Strings. So you would want to say:



This, can then be used to set the local (if there are any) vars by saying,



For using getters and setters, you might do something like this (you would not necessarily use the constructor)



Then in the object where you set someVar, you may do the following:



I know that some of this can be really confusing and I am likely not helping. I�ll try again if this does not make sense. From your question, I don�t know precisely what you are trying to do. Are you modifying the code or do you have questions about how sections of it work? If the latter, please post the sections that you are confused about.

Best,
Tom
[ January 19, 2006: Message edited by: Tom Sullivan ]
Tom Sullivan
Ranch Hand

Joined: Dec 20, 2005
Posts: 72
Sorry that the formating did not keep. I'll use the IDE next time.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Tom Sullivan:
...This, can then be used to set the local (if there are any) vars by saying,


[ January 19, 2006: Message edited by: Tom Sullivan ]

Be careful with terminology here. This does NOT set local variables. When you declare a variable inside the class like "public String question;", it is called a member or instance variable. In this case, the code in your constructor will look like


I have a few other comments, but I think I will post another reply since they are directed to the OP.

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Steve Stanicki:
Hello All,
I am working thru Head First Java, and I got stuck on the Quiz Card app on page 451.

Specifically the QuizCard class is confusing me. I think I understand that it represents each Q & A as a card object, but I'm not sure how to write/use it.

Does it 'get' the questions and answers, and 'set' them for output to the text file? Are we supposed to concat them together for each line in the file or use it to parse out each Q & A?
...

Object oriented programming can be a bit confusing when you first encounter it. I hope I can help clarify a few things here. I think the QuizCard class is supposed to simply represent the question and answer for a "flash card". No more, no less. Some other class is going to be responsible for creating QuizCard objects with matching questions and answers. This means that the QuizCard class is NOT responsible for file handling or anything more complicated. It simply stores related information (e.g. a question and an answer) in memory.

I hope that clarifies what the QuizCard represents (at least the way I see it). I also have a few comments on your code:

1) You currently have

It is typically considered a bad practice to declare instance variables like this as "public". This is because it allows anyone to change the variables. In this case, some other class could potentially change the answer so that it not the right one for the question. So instead, you should declare variables as "private" like this:

This forces other classes to only call the methods that the class defines to set the question and answer. Sometimes you will provide a setter that control how the variables' values are changed. You typically also provide getter methods that allow other classes to access the variables' values. It looks like you have tried to write these.

2) As Tom pointed out, you need to include the type in the parameter list of a constructor or method. (The parameter list is the part in parentheses after the method's name.) I would go one step further and also suggest that you should use longer names than just "q" and "a" for these variables.

3) Getter methods (like your getQuestion() and getAnswer() methods) are typically used to just return the value of an instance variable. Can you figure out a way to do this with a single line?

4) If you have tried to compile this, you have probably seen multiple compiler errors. Most of these probably say "undefined identifier" or something similar. Tom's suggestions will fix a couple of these. In your getQuestion() and getAnswer() methods, you use variables named q and a that have not been declared where these methods can access them. (In programming jargon, we say that a variable is not been declared "in the scope of each method".) See my comments in #3 above to determine if these variables are even necessary.

I hope I have answered your questions here. I've probably even answered questions you didn't have or already knew the answers to. Good luck with finishing this program. Please come back if you have any further questions.

Regards,

Layne
Steve Stanicki
Greenhorn

Joined: Sep 24, 2005
Posts: 23
Thanks so much. I am on my way once more.

Steve
Steve Stanicki
Greenhorn

Joined: Sep 24, 2005
Posts: 23
Here is what I came up with and it seems to work.

import java.util.*;
import java.io.*;

public class QuizCard{
private String question;
private String answer;

public QuizCard(String q, String a){
answer = a;
question = q;
}

public String getAnswer(){
return answer;
}

public String getQuestion(){
return question;
}
}

The program works, and let's me generate flash cards, but it gives me some run time warnings about NullPointerException.

Thanks,
Steve
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
Hi, where exactly does one put the questions and answers for the cards?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
Don't understand your question.
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
I am referring to the final version. On the quizcard there has to be text. One question, followed by an answer after clicking the button. Now, what I would like to know is where to put the question.

Question(Frage): Which color do you like?
Answer(Antwort): Green.

this is my entire code

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem w/Quiz Card Builder in Head First