1. a) b) c) d) e) One of the methods I have created allows me to set thequestion
ID: 3614543 • Letter: 1
Question
1. a) b) c) d) e)
One of the methods I have created allows me to set thequestion. It is publicvoid setQuestion(StringquestionText). So if I create an instance, q, all i have to do it typein....
q.setQuestion("This is myquestion.");
And it returns....
1. This is myquestion. a) b) c) d) e)
Next, another method I created is supposed to allow me to set5 possible answers (a through e) and the the text of each of thepossible answers. It is ... publicboolean setAnswer(char whichAnswer,String answerText). But when I try and create 5 possible answers by typingin...
q.setAnswer('a',"FirstChoice."); q.setAnswer('b',"Second Choice."); q.setAnswer('c',"Third Choice."); q.setAnswer('d',"Fourth Choice."); q.setAnswer('e',"Fifth Choice.");
This iswhat returns...
1. This is myquestion. a) b) c) d) e) FifthChoice.
HERE ISMY QUESTION I knowthat each new "q.setAnswer(.....)" Itype overrides the previous one, but I don't know howto fixit.
PLEASE HELP!!!!Here is all of my code for the program....
Explanation / Answer
First: why are you making the field letter_choice static? That is, why are you using the keyword "static" in thevariable declaration? By doing this, you are making the class--andALL instants of that class (that is, ALL objects you create of thisclass) of having only one "letter_choice." This is clearly not whatyou intend, since you want to be able to set letter_choicedifferent times and have the program actually remember thosevalues. So, remove static from the declaration. In fact, take thekeyword "static" out wherever you do not need it. Just say:
char whichAnswer, letter_choice;
(Frankly, the line should only be:
char letter_choice.
whichAnswer should not/does not even need to be declared inthis class since it is only a formal parameter being passed tosetAnswer).
Second: you have only one variable for answer_choice. Thatmeans each time you call setAnswer, the value of answer_choicechanges to the String answerText that you pass as a parameter. Thelast time you call setAnswer is the value that will be saved foranswer_choice. Then, in your toString method, you have if blocksand you keep calling answer_choice to print within those if blocks.If your toString method was working properly (we'll get to that ina minute), then all of your answers would be the same.
This is what is ultimately happening in the toString method:the last time you called your setAnswer method, you reset the valueof the STATIC variable letter_choice to 'e' with the line: q.setAnswer('e',"FifthChoice."); Then, when your toStringmethod executes, the if block can only find letter_choice=e (again,because the static variable has been set to e), and so only thatblock executes.
To remedythe situation. Start here:
Afterremoving the static keyword, go back and tweak your setAnswermethod. Use ifblocks again, but like this: if(which_answer=='a') { answer_a=answerText; } if(which_answer=='b') { answer_b=answerText; }
and soforth. This way you are actually setting the correct String to thecorrect answer variable (answer_a, answer_b, etc).
This waywhen you go to call toString, answer_a has a value already and youdo not need to use the line: answer_a=answerChoice;
In fact,you would need to remove that line and the lines like it in yourtoString method (that is remove answer_b=answerChoice,answer_c=answerChoice, etc. Because, don't you see, you are settingall the answers to the same value?)
In yourtoString method, you also need to remove the tests: if(letter_choice=='a') or if (letter_choice=='b'), etc. Take outthose if/else clauses. The variable letter_choice was onlyused--and is only useful--in your setAnswer method to set thecorrect answer String to the correct answer choice.
Use this asa guideline. I hope this helps! Good luck! q.setAnswer('e',"FifthChoice."); Then, when your toStringmethod executes, the if block can only find letter_choice=e (again,because the static variable has been set to e), and so only thatblock executes.
To remedythe situation. Start here:
Afterremoving the static keyword, go back and tweak your setAnswermethod. Use ifblocks again, but like this: if(which_answer=='a') { answer_a=answerText; } if(which_answer=='b') { answer_b=answerText; }
and soforth. This way you are actually setting the correct String to thecorrect answer variable (answer_a, answer_b, etc).
This waywhen you go to call toString, answer_a has a value already and youdo not need to use the line: answer_a=answerChoice;
In fact,you would need to remove that line and the lines like it in yourtoString method (that is remove answer_b=answerChoice,answer_c=answerChoice, etc. Because, don't you see, you are settingall the answers to the same value?)
In yourtoString method, you also need to remove the tests: if(letter_choice=='a') or if (letter_choice=='b'), etc. Take outthose if/else clauses. The variable letter_choice was onlyused--and is only useful--in your setAnswer method to set thecorrect answer String to the correct answer choice.
Use this asa guideline. I hope this helps! Good luck!
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.