Tuesday, September 20, 2011

Calculating the percent correct, and a dialog box




Ok, yesterday we started implementing this logic into our iPhone app from the Android version:


if (appState.quizSequenceIndex < appState.quizSequence.size()) {

incrementQuestion();

QuestionActivity.this.setupDisplay();

} else { // finished

// Do this to keep highlight in reversed position from showing up
makeListTranparent(this.getListView());

int pct = CalcTotals.calcPctCorrect((AppState) this
.getApplicationContext());

if (pct < 100) {

text = "You correctly answered " + appState.correctAnswers
+ " out of " + appState.quizSequence.size()
+ " for a total score of " + pct + "%";

Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG)
.show();

} else {

// pass control to next level activity
Intent intent = new Intent(QuestionActivity.this,
NextLevelActivity.class);

startActivity(intent);
}



We want to focus on the piece right now:

int pct = CalcTotals.calcPctCorrect((AppState) this
.getApplicationContext());

if (pct < 100) {

text = "You correctly answered " + appState.correctAnswers
+ " out of " + appState.quizSequence.size()
+ " for a total score of " + pct + "%";

Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG)
.show();




So, first we have to figure out if we are correctly tracking percent. Also, we want to show the message. It's possible we're tracking it already, since the template for this program was another quiz application.

Actually, we can probably just use the existing logic to calculate the % correct. We have both the number of questions (shuffled array length) - well, do have number of questions answered correctly?

Well, it's on the old and no-longer used answer controller - we now keep it on the same display. But here's the logic:


if (selection == [appState slotOfCorrectAnswer]) {

resultView.text = @"Correct!";
appState.correctAnswers++;
[appState saveState];
}


So, we can definitely keep the increment to correct answers. We wont' save app state because if they come out and back in, they can just restart the quiz. We can drop the "correct" setting text, as this is now handled by a green highlight. So, all we have to do is put this wherever we're judging where the correct answer is.


Easy enough:


if(result) {
NSLog (@"correct");
appState.correctAnswers++;
}


Ok, now to the display of the message. We already did one, where was it? The settings contoller:

// stringWithFormat autoreleases
NSString *myMessage = [NSString stringWithFormat:@"End Quiz Number must be less then or equal to %d",[appState.questionDict count] + 1];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"InvalidEnd Quiz Number"
message:myMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];


Ok, it's in the "advance_question" method of the question controller. We will need to do something like this:



else
{

float pct = (float) appState.correctAnswers / (float) [appState.shuffledQuestionDictKeyList count];

if (pct < 100.0f){

// stringWithFormat autoreleases
NSString *myMessage = [NSString stringWithFormat:@"You answered %d out %d correctly",appState.correctAnswers, [appState.questionDict count]];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"To advance, all questions in the level must be answered correctly"
message:myMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}



Let's see if we can get this message to pop up.

Well, ok, but the numbers aren't correct:




Obviously, we need to zero out correct answers when starting the quiz. Now, why is shuffled array keys so large? Shouldn't it be only 3?

Ok, this is better:

NSString *myMessage = [NSString stringWithFormat:@"You answered %d out %d correctly",appState.correctAnswers, [appState.shuffledQuestionDictKeyList count]];

Now, we just need to zero out the count. We'll put this in the StartController logic that opens the question contoller:

appState.correctAnswers = 0;

Ok, looks good:


No comments:

Post a Comment