Guest User

Untitled

a guest
Feb 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package com.example.android.schumiquiz;
  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.CheckBox;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10. import android.widget.RadioButton;
  11. import android.widget.RadioGroup;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15. double score = 0;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. }
  22.  
  23. public void reset(View v) {
  24. score = 0;
  25.  
  26. CheckBox q21 = (CheckBox) findViewById(R.id.q21);
  27. q21.setChecked(false);
  28. CheckBox q22 = (CheckBox) findViewById(R.id.q22);
  29. q22.setChecked(false);
  30. CheckBox q23 = (CheckBox) findViewById(R.id.q23);
  31. q23.setChecked(false);
  32. CheckBox q24 = (CheckBox) findViewById(R.id.q24);
  33. q24.setChecked(false);
  34. CheckBox q31 = (CheckBox) findViewById(R.id.q31);
  35. q31.setChecked(false);
  36. CheckBox q32 = (CheckBox) findViewById(R.id.q32);
  37. q32.setChecked(false);
  38. CheckBox q33 = (CheckBox) findViewById(R.id.q33);
  39. q33.setChecked(false);
  40. CheckBox q34 = (CheckBox) findViewById(R.id.q34);
  41. q34.setChecked(false);
  42. RadioGroup q4 = findViewById(R.id.q4Options);
  43. q4.clearCheck();
  44. EditText nameInput = findViewById(R.id.q5Input);
  45. nameInput.setText(null);
  46. RadioGroup q1 = findViewById(R.id.q1Options);
  47. q1.clearCheck();
  48.  
  49. }
  50.  
  51. public void calculate(View view) {
  52.  
  53. // Check the radio button for the first question
  54.  
  55. RadioButton rb1 = findViewById(R.id.q13);
  56. if (rb1.isChecked()) {
  57. score += 1;
  58. }
  59.  
  60. // Log.v("MainActivity", "Score after 1 question: " + Double.toString(score));
  61.  
  62. // Check checkboxes for the second question
  63.  
  64. CheckBox q21 = findViewById(R.id.q21);
  65. boolean q21Checked = q21.isChecked();
  66. CheckBox q22 = findViewById(R.id.q22);
  67. boolean q22Checked = q22.isChecked();
  68. CheckBox q23 = findViewById(R.id.q23);
  69. boolean q23Checked = q23.isChecked();
  70. CheckBox q24 = findViewById(R.id.q24);
  71. boolean q24Checked = q24.isChecked();
  72.  
  73. // Check answers to second question (multiple choice)
  74. if (!q21Checked && !q22Checked && !q23Checked && !q24Checked) {
  75. //if no answer was selected, it is counted as zero points
  76. } else {
  77. if (q21Checked) {
  78. score += 0.25;
  79. }
  80. if (!q22Checked) {
  81. score += 0.25;
  82. }
  83. if (q23Checked) {
  84. score += 0.25;
  85. }
  86. if (!q24Checked) {
  87. score += 0.25;
  88. }
  89. }
  90.  
  91. // Log.v("MainActivity", "Score after 2 questions: " + Double.toString(score));
  92.  
  93. // Check checkboxes for the third question
  94.  
  95. CheckBox q31 = findViewById(R.id.q31);
  96. boolean q31Checked = q31.isChecked();
  97. CheckBox q32 = findViewById(R.id.q32);
  98. boolean q32Checked = q32.isChecked();
  99. CheckBox q33 = findViewById(R.id.q33);
  100. boolean q33Checked = q33.isChecked();
  101. CheckBox q34 = findViewById(R.id.q34);
  102. boolean q34Checked = q34.isChecked();
  103.  
  104. // Same logic for points calculation as with previous question
  105.  
  106. if (!q31Checked && !q32Checked && !q33Checked && !q34Checked) {
  107. //if no answer was selected, it is counted as zero points
  108. } else {
  109.  
  110. if (!q31Checked) {
  111. score += 0.25;
  112. }
  113. if (!q32Checked) {
  114. score += 0.25;
  115. }
  116. if (q33Checked) {
  117. score += 0.25;
  118. }
  119. if (!q34Checked) {
  120. score += 0.25;
  121. }
  122. }
  123.  
  124. //Log.v("MainActivity", "Score after 3 questions: " + Double.toString(score));
  125.  
  126. // Radio button for the fourth question is checked
  127.  
  128. RadioButton rb4 = findViewById(R.id.q44);
  129. if (rb4.isChecked()) {
  130. score += 1;
  131. }
  132.  
  133. //Log.v("MainActivity", "Score after 4 questions: " + Double.toString(score));
  134.  
  135. // Text input in question five is checked
  136.  
  137. EditText nameInput = findViewById(R.id.q5Input);
  138. String q5Input = nameInput.getText().toString();
  139. if (q5Input.equals("Corinna")) {
  140. score += 1;
  141. }
  142.  
  143. //Log.v("MainActivity", "Score after 5 questions: " + Double.toString(score));
  144.  
  145. // Toast displayed
  146.  
  147. if (score == 5) {
  148. Toast.makeText(this, "Congratulations! All answers are correct!", Toast.LENGTH_SHORT).show();
  149. } else {
  150. Toast.makeText(this, "Your score is " + Double.toString(score) + " points out of 5", Toast.LENGTH_SHORT).show();
  151. }
  152. score = 0;
  153.  
  154. //Log.v("MainActivity", "Score after Toast: " + Double.toString(score));
  155.  
  156. }
  157.  
  158. }
Add Comment
Please, Sign In to add comment