First, write a class called TFQuestion that represents a true/false question. This class should have exactly two instance variables: a String variable that holds the statement of a true/false question, and a boolean variable that holds the correct answer. This class should provide the following public methods:

A constructor that takes the statement of a true/false question and the correct answer as parameters and initializes the instance variables with the parameter values
The toString() method that returns the statement of the true/false question
A method checkAnswer() that takes a boolean answer to the true/false question as its parameter and returns true if that answer is correct, false otherwise

I dont really sure how to write these code for tf question
please help me

What have you done so far?

regards,
sukatoa

public class TFQuestion
{
String tfQuestion;
boolean cAnswer;
public TFQuestion(String tfQtion)
{
tfQuestion = tfQtion;
cAnswer = true;
}
public String toString()
{
return tfQuestion;
}
public boolean checkAnswer()
{

}
this is what i got... i dont know if i am correct for first constructor and i am not sure about tostring and checkanswer method...like what code should i put for checkanswer and tostring and also the first constructor

@ TFQuestion Constructor, you are using boolean variable "true", which is not allowed to be a variable in java. It is a keyword.

if tfQuestion is the holder of statement with respect to the true/false question, then return that string, not the name of the class.

about your checkAnswer, that is not a constructor, since it should return a boolean value, put a boolean after the public.

regards,
sukatoa

@ TFQuestion Constructor, you are using boolean variable "true", which is not allowed to be a variable in java. It is a keyword.

Really? It was my impression you can set a boolean to either "true" or "false".

boolean started = false;

if(x == 4){
   started = true; 
}

public class TFQuestion
{
String tfQuestion;
boolean cAnswer;
public TFQuestion(String tfQtion)
{
tfQuestion = tfQtion;
cAnswer = true;
}
public String toString()
{
return tfQuestion;
}
public boolean checkAnswer()
{

}
this is what i got... i dont know if i am correct for first constructor and i am not sure about tostring and checkanswer method...like what code should i put for checkanswer and tostring and also the first constructor

You are almost there:

According to your requirements, in your constructor, you also need to pass a boolean variable; so, your constructor looks something like this:

public TFQuestion(String tfQtion, boolean tfValue)
{
    tfQuestion = tfQtion;
    cAnswer = tfValue;
}

Finally your checkAnswer() method. Here you may ask your teacher again, what should be the return value... should it be a String (holding a string "True" and/or "False") or a boolean holding true (1) and/or false(0)? If the answer is formar, then you proceed as follow:

public String checkAnswer(boolean answer) {
    if (answer) {
        return "True";
    } else {
        return "False";
    }
}

And if the answer is later, then you need to change is the return type of the method to boolean, as follow (Which I don't see any use for this):

public boolean checkAnswer(boolean answer) {
    return answer;
}

Hope this helps

comjisu33, based on sukatoa's response it looks like your original code was quite different and you edited it after the response. If you make changes to the code, make a new reply with those changes instead of altering the original. After your edits, sukatoa's response seems to make no sense because that code is no longer there. Please leave the integrity of the thread in place so as not to cause confusion.

comjisu33, based on sukatoa's response it looks like your original code was quite different and you edited it after the response. If you make changes to the code, make a new reply with those changes instead of altering the original. After your edits, sukatoa's response seems to make no sense because that code is no longer there. Please leave the integrity of the thread in place so as not to cause confusion.

ok I will do that thx for telling me;;

You are almost there:

According to your requirements, in your constructor, you also need to pass a boolean variable; so, your constructor looks something like this:

public TFQuestion(String tfQtion, boolean tfValue)
{
    tfQuestion = tfQtion;
    cAnswer = tfValue;
}

Finally your checkAnswer() method. Here you may ask your teacher again, what should be the return value... should it be a String (holding a string "True" and/or "False") or a boolean holding true (1) and/or false(0)? If the answer is formar, then you proceed as follow:

public String checkAnswer(boolean answer) {
    if (answer) {
        return "True";
    } else {
        return "False";
    }
}

And if the answer is later, then you need to change is the return type of the method to boolean, as follow (Which I don't see any use for this):

public boolean checkAnswer(boolean answer) {
    return answer;
}

Hope this helps

thank you so much !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.