atultiwari

Helper.java

Jul 16th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. //---- Helper.java ---- //
  2. package com.atultiwari.medicalquiz;
  3.  
  4. /**
  5.  * @author robert.hinds
  6.  *
  7.  */
  8. public class Helper {
  9.  
  10.     /**
  11.      * This method selects a end game response based on the players score
  12.      * and current difficulty level
  13.      *
  14.      * @param numCorrect - num correct answers
  15.      * @param numRounds - number of questions
  16.      * @param diff - the difficulty level
  17.      * @return String comment
  18.      */
  19.     public static String getResultComment(int numCorrect, int numRounds, int diff)
  20.     {
  21.         String comm="";
  22.         int percentage = calculatePercentage(numCorrect, numRounds);
  23.         switch (diff)
  24.         {
  25.         case Constants.EXTREME :
  26.             if (percentage > 90){
  27.                 comm = "You downloaded the Intersect!";
  28.             }else if (percentage >= 80){
  29.                 comm="Nicely Done!";
  30.             }else if (percentage >= 60){
  31.                 comm="Not too bad..";
  32.             }else if (percentage >= 40){
  33.                 comm="Well, don't give up..";
  34.             }else{
  35.                 comm="Next time, stay in the car..";
  36.             }
  37.             break;
  38.            
  39.         default:
  40.             if (percentage > 90){
  41.                 comm = "Awesome!";
  42.             }else if (percentage >= 80){
  43.                 comm="Nicely Done!";
  44.             }else if (percentage >= 60){
  45.                 comm="Not too bad..";
  46.             }else if (percentage >= 40){
  47.                 comm="Well, don't give up..";
  48.             }else{
  49.                 comm="Next time, stay in the car..";
  50.             }
  51.         }
  52.        
  53.         return comm;
  54.     }
  55.    
  56.    
  57.     /**
  58.      * Method to return an image to use for the end of game screen
  59.      *
  60.      * @param numCorrect - number of correct answers
  61.      * @param numRounds - number of rounds
  62.      * @param diff - difficulty level
  63.      * @return int Image ID
  64.      */
  65.     public static int getResultImage(int numCorrect, int numRounds, int diff){
  66.         //calculate percentage
  67.         int percentage = calculatePercentage(numCorrect, numRounds);
  68.        
  69.         //work out which image
  70.         if (percentage > 90){
  71.             if (diff == Constants.EXTREME){
  72.                 return R.drawable.hard_winner;
  73.             }else{
  74.                 return R.drawable.easy_winner;
  75.             }
  76.         }else if (percentage >= 80){
  77.             return R.drawable.prettygood;
  78.         }else if (percentage >= 60){
  79.             return R.drawable.notbad;
  80.         }else if (percentage >= 40){
  81.             return R.drawable.tryagain;
  82.         }else{
  83.             return R.drawable.loser;
  84.         }
  85.     }
  86.    
  87.  
  88.     /**
  89.      * Calculate the percentage result based on the number correct and number of questions
  90.      *
  91.      * @param numCorrect - number of questions right
  92.      * @param numRounds - total number of questions
  93.      * @return int percentage correct
  94.      */
  95.     private static int calculatePercentage(int numCorrect, int numRounds) {
  96.         double frac = (double)numCorrect/(double)numRounds;
  97.         int percentage = (int) (frac*100);
  98.         return percentage;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment