Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. package com.mamaril.justin.dronelawquiz;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.RadioGroup;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. import org.w3c.dom.Text;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16. private int score = 0;
  17. private int tries = 0;
  18.  
  19. // 1. Declare your views as class variables
  20.  
  21. Button submitAnswersButton;
  22. Button resetQuizButton;
  23.  
  24. RadioGroup question1;
  25. RadioGroup question2;
  26. RadioGroup question3;
  27. RadioGroup question4;
  28. RadioGroup question5;
  29. RadioGroup question6;
  30. RadioGroup question7;
  31. RadioGroup question8;
  32.  
  33.  
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39.  
  40. // 2. Initialize your views onCreate, after inflating the Layout
  41.  
  42. submitAnswersButton = (Button) findViewById(R.id.submit_answers_button);
  43. resetQuizButton = (Button) findViewById(R.id.reset_quiz_button);
  44.  
  45. question1 = (RadioGroup) findViewById(R.id.question_1);
  46. question2 = (RadioGroup) findViewById(R.id.question_2);
  47. question3 = (RadioGroup) findViewById(R.id.question_3);
  48. question4 = (RadioGroup) findViewById(R.id.question_4);
  49. question5 = (RadioGroup) findViewById(R.id.question_5);
  50. question6 = (RadioGroup) findViewById(R.id.question_6);
  51. question7 = (RadioGroup) findViewById(R.id.question_7);
  52. question8 = (RadioGroup) findViewById(R.id.question_8);
  53.  
  54. // 3. Run your setOnClickListener methods without those pesky null pointer warnings
  55. submitAnswersButton.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View view) {
  58. calculateScore();
  59. Toast.makeText(MainActivity.this, "Score: " + score + "/8. Hit 'Reset Quiz' to try again!", Toast.LENGTH_LONG).show();
  60. }
  61. });
  62.  
  63. resetQuizButton.setOnClickListener(new View.OnClickListener() {
  64. @Override
  65. public void onClick(View view) {
  66. resetQuiz();
  67. }
  68. });
  69.  
  70. }
  71.  
  72. private void calculateScore() {
  73. if (tries == 0) {
  74.  
  75. // 4. Run your getCheckedRadioButtonId methods without those pesky null pointer warnings
  76. int one = question1.getCheckedRadioButtonId();
  77. int two = question2.getCheckedRadioButtonId();
  78. int three = question3.getCheckedRadioButtonId();
  79. int four = question4.getCheckedRadioButtonId();
  80. int five = question5.getCheckedRadioButtonId();
  81. int six = question6.getCheckedRadioButtonId();
  82. int seven = question7.getCheckedRadioButtonId();
  83. int eight = question8.getCheckedRadioButtonId();
  84.  
  85. if (one == 1)
  86. score += 1;
  87. if (two == 5)
  88. score += 1;
  89. if (three == 7)
  90. score += 1;
  91. if (four == 12)
  92. score += 1;
  93. if (five == 14)
  94. score += 1;
  95. if (six == 16)
  96. score += 1;
  97. if (seven == 19)
  98. score += 1;
  99. if (eight == 24)
  100. score += 1;
  101.  
  102. tries = 1;
  103. }
  104. }
  105.  
  106. private void resetQuiz() {
  107.  
  108. score = 0;
  109. tries = 0;
  110. // 5. Run your clearCheck methods without those pesky null pointer warnings
  111. question1.clearCheck();
  112. question2.clearCheck();
  113. question3.clearCheck();
  114. question4.clearCheck();
  115. question5.clearCheck();
  116. question6.clearCheck();
  117. question7.clearCheck();
  118. question8.clearCheck();
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement