Guest User

Untitled

a guest
Nov 19th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. int question = 1;
  3. int score = 0;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. }
  9. // Displays the result of the quiz
  10. public void displayResult(View view) {
  11. // Checks if it is a first submission
  12. if (question == 1) {
  13. score = 0;
  14. score = calculateResult();
  15. question = question + 1;
  16. // Presents the obtained score in a toast
  17. String finalResult = ("Your result: " + score);
  18. Toast.makeText(this, finalResult, Toast.LENGTH_LONG).show();
  19. // Prints correct answers
  20. TextView answers = (TextView) findViewById(R.id.answers);
  21. String answersMessage = "Correct answers:"+"\n"+"Question 1: B."+"\n"+"Question 2: D."+"\n"+"Question 3: transcription."+"\n"+"Question 4: D."+"\n"+"Question 5: A, D. The first drug produced using recombinant DNA technology was insulin. You're encouraged to read more on knockout mice."+"\n"+"Question 6: A." ;
  22. answers.setText(answersMessage);
  23. }else { // If the submit button is clicked more than once
  24. String toastMessage = "You can only submit once! Your scored: "+score;
  25. Toast.makeText(this, toastMessage, Toast.LENGTH_SHORT).show();
  26. }
  27. }
  28. // Calculates result (score)
  29. private int calculateResult() {
  30. // Finds the views with correct answers and checks if they are marked
  31. int score = 0;
  32. boolean checked1 = ((RadioButton) findViewById(R.id.b1)).isChecked();
  33. boolean checked2 = ((RadioButton) findViewById(R.id.d2)).isChecked();
  34. boolean checked4 = ((RadioButton) findViewById(R.id.d4)).isChecked();
  35. boolean checked6 = ((RadioButton) findViewById(R.id.a6)).isChecked();
  36. boolean checked5a = ((CheckBox) findViewById(R.id.a5)).isChecked();
  37. boolean checked5b = ((CheckBox) findViewById(R.id.b5)).isChecked();
  38. boolean checked5c = ((CheckBox) findViewById(R.id.c5)).isChecked();
  39. boolean checked5d = ((CheckBox) findViewById(R.id.d5)).isChecked();
  40. if (checked1) {
  41. // The correct answer: B.
  42. score = score + 1;
  43. }
  44. if (checked2) {
  45. // The correct answer: D.
  46. score = score + 1;
  47. }
  48. if (checked4) {
  49. // The correct answer: D.
  50. score = score + 1;
  51. }
  52. if (checked6) {
  53. // The correct answer: A.
  54. score = score + 1;
  55. }
  56. if (checked5a && checked5d && !checked5b && !checked5c) { // The correct answers: A, D.
  57. score = score + 1;
  58. }
  59. // Open question - allowed answers: transcription/Transcription --> TODO: periods? other characters in response?
  60. EditText transcription = findViewById(R.id.transcription_q);
  61. String answerTranscription = transcription.getText().toString().toLowerCase();
  62. if (answerTranscription.equals("transcription")) {
  63. score = score + 1;
  64. }
  65. return score;
  66. }
  67. }
Add Comment
Please, Sign In to add comment