Challenge Three Solution

Hello Everyone,

For the third challenge I introduced an array mCheatCount which keeps track throughout the course of the program whether any cheating has taken place or not. Here are the snippets from the program where I made the changes :

private int[] mCheatCount = new int[]{0,0,0,0,0,0}; /declaring the new array/

@Override /making the value of mCheatCount[mCurrentIndex] equal to 1 if
cheating took place for a particular question
/
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode != Activity.RESULT_OK) {
return;
}
if(requestCode == REQUEST_CODE_CHEAT){
mCheatCount[mCurrentIndex] = 1;
if(data == null)
return;
mIsCheater = CheatActivity.wasAnswerShown(data);
}

private void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

    int messageResId = 0;
    if(mCheatCount[mCurrentIndex] == 1){           /*checking whether the user tried to cheat 
                                                                            on this particular question*/
        mIsCheater = true;
    }
    if(mIsCheater){
        messageResId = R.string.judgement_toast;
    }
    else {
        if (userPressedTrue == answerIsTrue) {
            messageResId = R.string.correct_toast;
        } else {
            messageResId = R.string.incorrect_toast;
        }
    }
    Toast.makeText(this,messageResId,Toast.LENGTH_LONG).show();

}

The value of mCheatCount[] would remain same for the question even if the user presses NEXT and returns to the question. This is working fine for me. Do let me know if anyone else has come UP with a different solution. Because I went through the discussion and I couldn’t find a different solution for this question.

THANKS!

1 Like

I did a little different. In the class TrueFalse.java I added one more field, it is a boolean field called private boolean mCheater. It is working, but when I rotate the screen, it is not showing the correct result, even though the array mQuestionBank is not restarted in onCreate().

Your array idea helped out (I should use local variable for stoarge,) I was trying to use the same variable being returned once the answer is shown mIsCheater, but it kept resetting. So I decided to add another field to the Question.class called mCheater and make a setter to flag it once the cheatActivity returned data. Then I have a method called buttonMagic() that disables the buttons based on the (boolean) flags. Here are my code snippets.

public class Question implements Serializable {
private int mTextResId;
private boolean mAnswerTrue;
/**
*adding a check to see if answered already or cheat happened, then use the setter and getter
*/
private boolean mAlreayAnswered;//CHALLENGE
private boolean mCheater;//CHALLENGE

public Question(int textResId, boolean answerTrue) {
    mTextResId = textResId;
    mAnswerTrue = answerTrue;
    mAlreayAnswered = false; //ADDED FOR CHALLENGE
    mCheater = false; //ADDED FOR CHALLENGE
}//CONSTRUCTOR

next add the flag catcher!
/*************************************************************************
* THIS METHOD IS TO CHECK THE RETURN INTENT DATA*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != Activity.RESULT_OK) {
return;
}
if(requestCode == REQUEST_CODE_CHEAT) {
if(data == null) {
return;
}
mIsCheater = CheatActivity.wasAnswerShown(data);
mQuestionBank[mCurrentIndex].setIsCheaterAnswered(true);//CHALLENGE
}
}//METHOD

Finally do the work.
/**********************************************************************
* THIS IS THE ENABLE DISABLE OF THE BUTTONS
* *******************/
public void buttonMagic() {
//CHALLENGE
boolean cheaterStorage = mQuestionBank[mCurrentIndex].isCheaterAnswered();
boolean checkIfAnswered = mQuestionBank[mCurrentIndex].isAlreayAnswered();
if (checkIfAnswered) {
mTrueButton.setEnabled(false);
mFalseButton.setEnabled(false);
} else {
mTrueButton.setEnabled(true);
mFalseButton.setEnabled(true);
}
if(cheaterStorage) {
mCheatButton.setEnabled(false);
} else {
mCheatButton.setEnabled(true);
}
}//METHOD

Hey all, I got confused on this question , the idea of creating an array and recording cheated questions dint strike , so I disabled the Next button after user reaches the last question, so that user cant repeat the ques.
Will this account as valid solution ?

I did the same thing. After the last question, the next button is disabled. And while displaying the score, the cheated answer is not counted. In other words, if he answered 2 questions and cheated on 1, his score would be 2 / 3.