Advertisement
SarahT1111

Earthquake quiz app Java

Apr 7th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 KB | None | 0 0
  1. package com.example.android.earthquake;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.text.Editable;
  9. import android.util.Log;
  10. import android.view.Gravity;
  11. import android.view.View;
  12. import android.view.WindowManager;
  13. import android.widget.CheckBox;
  14. import android.widget.EditText;
  15. import android.widget.RadioButton;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import java.text.NumberFormat;
  20.  
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.     private static final String LOG_TAG = MainActivity.class.getSimpleName();
  24.  
  25.     // Question 1
  26.     RadioButton question1_choice4;
  27.     // Question 2
  28.     RadioButton question2_choice1;
  29.     // Question 3
  30.     CheckBox question3_choice1;
  31.     CheckBox question3_choice2;
  32.     CheckBox question3_choice3;
  33.     CheckBox question3_choice4;
  34.     // Question 4
  35.     RadioButton question4_choice1;
  36.     // Question 5
  37.     EditText question5_answer;
  38.     // Question 6
  39.     RadioButton question6_choice2;
  40.  
  41.     @Override
  42.     protected void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         // Hide the keyboard
  45.         this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  46.         setContentView(R.layout.activity_main);
  47.     }
  48.  
  49.     public void submitAnswers(View view) {
  50.         CharSequence resultsDisplay;
  51.         Log.e(LOG_TAG, " " + this.findViewById(R.id.question1_choice4));
  52.         int answer1_score;
  53.         int answer2_score;
  54.         int answer3_score;
  55.         int answer4_score;
  56.         int answer5_score;
  57.         int answer6_score;
  58.         int answer7_score;
  59.         int answer8_score;
  60.         int answer9_score;
  61.         int answer10_score;
  62.         int final_score;
  63.  
  64.         //------------------------------------------------------------------------------------------
  65.         // Question 1 - Correct Answer is #4 (reverse)
  66.         //------------------------------------------------------------------------------------------
  67.         Boolean answer1;
  68.  
  69.         question1_choice4 = (RadioButton) this.findViewById(R.id.question1_choice4);
  70.         answer1 = question1_choice4.isChecked();
  71.         if (answer1) {
  72.             answer1_score = 1;
  73.         } else {
  74.             answer1_score = 0;
  75.         }
  76. //------------------------------------------------------------------------------------------
  77.         // Question 2 - Correct Answer is #1 (Strike-slip)
  78.         //------------------------------------------------------------------------------------------
  79.         Boolean answer2;
  80.  
  81.         question2_choice1 = (RadioButton) this.findViewById(R.id.question2_choice1);
  82.         answer2 = question2_choice1.isChecked();
  83.         if (answer2) {
  84.             answer2_score = 1;
  85.         } else {
  86.             answer2_score = 0;
  87.         }
  88. //------------------------------------------------------------------------------------------
  89.         // Question 3  - Correct Answers are #3 (Strike-slip) and #4 (Left-lateral strike-slip)
  90.         //------------------------------------------------------------------------------------------
  91.         Boolean answer3_choice1;
  92.         Boolean answer3_choice2;
  93.         Boolean answer3_choice3;
  94.         Boolean answer3_choice4;
  95.         question3_choice1 = (CheckBox) this.findViewById(R.id.question3_choice1);
  96.         question3_choice2 = (CheckBox) this.findViewById(R.id.question3_choice2);
  97.         question3_choice3 = (CheckBox) this.findViewById(R.id.question3_choice3);
  98.         question3_choice4 = (CheckBox) this.findViewById(R.id.question3_choice4);
  99.         answer3_choice1 = question3_choice1.isChecked();
  100.         answer3_choice2 = question3_choice2.isChecked();
  101.         answer3_choice3 = question3_choice3.isChecked();
  102.         answer3_choice4 = question3_choice4.isChecked();
  103.         if (!answer3_choice1 && !answer3_choice2 && answer3_choice3 && answer3_choice4) {
  104.             answer3_score = 1;
  105.         } else {
  106.             answer3_score = 0;
  107.         }
  108. //------------------------------------------------------------------------------------------
  109.         // Question 4 - Correct Answer is #1 (Normal)
  110.         //------------------------------------------------------------------------------------------
  111.         Boolean answer4;
  112.  
  113.         question4_choice1 = (RadioButton) this.findViewById(R.id.question4_choice1);
  114.         answer4 = question4_choice1.isChecked();
  115.         if (answer4) {
  116.             answer4_score = 1;
  117.         } else {
  118.             answer4_score = 0;
  119.         }
  120. //------------------------------------------------------------------------------------------
  121.         // Question 5 - Correct Answer is "Seismograph"
  122.         //------------------------------------------------------------------------------------------
  123.         String answer5;
  124.         question5_answer = (EditText) this.findViewById(R.id.question5_answer);
  125.         answer5 = question5_answer.getText().toString().toLowerCase();
  126.         if (answer5.equals("seismograph")) {
  127.             answer5_score = 1;
  128.         } else {
  129.             answer5_score = 0;
  130.         }
  131.  
  132. //------------------------------------------------------------------------------------------
  133.         // Question 6 - Correct Answer is #2 (No)
  134.         //------------------------------------------------------------------------------------------
  135.         Boolean answer6;
  136.  
  137.         question6_choice2 = (RadioButton) this.findViewById(R.id.question6_choice2);
  138.         answer6 = question6_choice2.isChecked();
  139.         if (answer6) {
  140.             answer6_score = 1;
  141.         } else {
  142.             answer6_score = 0;
  143.         }
  144.  
  145. //------------------------------------------------------------------------------------------
  146.         // Final Score
  147.         //------------------------------------------------------------------------------------------
  148.         final_score = answer1_score + answer2_score + answer3_score + answer4_score + answer5_score +
  149.                 answer6_score;
  150.  
  151.         if (final_score == 6) {
  152.             resultsDisplay = "Great Job! You scored 6 out of 6";
  153.         } else {
  154.             resultsDisplay = "You need to study more. You scored " + final_score + " out of 6";
  155.         }
  156.  
  157.         Context context = getApplicationContext();
  158.         int duration = Toast.LENGTH_LONG;
  159.         Toast toast = Toast.makeText(context, resultsDisplay, duration);
  160.         toast.setGravity(Gravity.CENTER, 0, 0);
  161.         toast.show();
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement