-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerScienceQuiz.java
More file actions
218 lines (205 loc) · 8.22 KB
/
Copy pathComputerScienceQuiz.java
File metadata and controls
218 lines (205 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Author: Gautham Radheepan
This is what the Computer Science quiz runs on by accessing the questions and displaying them
*/
package com.example.simple_quiz;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class ComputerScienceQuiz extends AppCompatActivity {
private String[] questions,aAnswers,bAnswers,cAnswers,dAnswers,correct;
Button abtn,bbtn,cbtn,dbtn,menu;
TextView edttext,edtquestion,edttexta,edttextb,edttextc,edttextd,finalCSCount;
public ComputerScienceQuizQuestions q = new ComputerScienceQuizQuestions();//gets the questions
private int randomArray[];
private int random,counter,correctCounter,randomArrayCounter;
String st;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//sets the button and the text view
setContentView(R.layout.activity_computer_science_quiz);
abtn = findViewById(R.id.csabtn);
bbtn = findViewById(R.id.csbbtn);
cbtn = findViewById(R.id.cscbtn);
dbtn = findViewById(R.id.csdbtn);
menu = findViewById(R.id.csmenubtn);
menu.setVisibility(View.INVISIBLE);//sets this button as temporarily invisible
edttext = findViewById(R.id.cstextview);
edtquestion = findViewById(R.id.csquestionview);
edttexta = findViewById(R.id.csatextview);
edttextb = findViewById(R.id.csbtextview);
edttextc = findViewById(R.id.csctextview);
edttextd = findViewById(R.id.csdtextview);
finalCSCount = findViewById(R.id.csfinalcount);
finalCSCount.setVisibility(View.INVISIBLE);//sets this button as temporarily invisible
//sets the questions and answers
questions=q.getQuestions();
aAnswers=q.getaAnswers();
bAnswers=q.getbAnswers();
cAnswers=q.getcAnswers();
dAnswers=q.getdAnswers();
correct=q.getCorrect();
randomArray = new int[10];
randomArrayCounter=0;
correctCounter=0;
counter=0;
initialSetQuestions();
//checks to see if a button is clicked
abtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
st=(String) abtn.getText();
checkQuestion();
}
});
bbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
st=(String) bbtn.getText();
checkQuestion();
}
});
cbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
st=(String) cbtn.getText();
checkQuestion();
}
});
dbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
st=(String) dbtn.getText();
checkQuestion();
}
});
}
private void initialSetQuestions(){ //the inital set questions
random = new Random().nextInt(20);
edtquestion.setText(("Question " + 1));
edttext.setText(questions[random]);
edttexta.setText(aAnswers[random]);
edttextb.setText(bAnswers[random]);
edttextc.setText(cAnswers[random]);
edttextd.setText(dAnswers[random]);
random++;
randomArray[randomArrayCounter]=random;
random--;
}
private void checkQuestion(){//after a button is clicked it checks to see if the answer is right and picks a new question to display
if (st.equals(correct[random])){
correctCounter++;
displayCorrect();//displays correct screen
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
setNextQuestion();
}
});
}
else{
displayWrong();//display wrong screen with the correct answer
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
setNextQuestion();
}
});
}
}
private void setNextQuestion(){//sets the questions after the button has been clicked
counter++;
if (counter==10){//checks to see if 10 questions have been answered
setVisibility();
finalCSCount.setText("You have got " + getCorrect() + " questions right");
menu.setText("Back to Menu");
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Menu();
}
});
}
else {
resetVisibility();
random = new Random().nextInt(20);
setRandomArray();
edtquestion.setText(("Question " + (counter + 1)));
edttext.setText(questions[random]);
edttexta.setText(aAnswers[random]);
edttextb.setText(bAnswers[random]);
edttextc.setText(cAnswers[random]);
edttextd.setText(dAnswers[random]);
}
}
private int getCorrect(){
return correctCounter;
}
private void Menu(){//returns to the main menu
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
private void setVisibility(){//sets the visibility to show incorrect and correct questions as well as the main menu option
abtn.setVisibility(View.INVISIBLE);
bbtn.setVisibility(View.INVISIBLE);
cbtn.setVisibility(View.INVISIBLE);
dbtn.setVisibility(View.INVISIBLE);
edttexta.setVisibility(View.INVISIBLE);
edttextb.setVisibility(View.INVISIBLE);
edttextc.setVisibility(View.INVISIBLE);
edttextd.setVisibility(View.INVISIBLE);
edtquestion.setVisibility(View.INVISIBLE);
edttext.setVisibility(View.INVISIBLE);
menu.setVisibility(View.VISIBLE);
finalCSCount.setVisibility(View.VISIBLE);
}
private void resetVisibility(){//sets the visibility to see the questions
abtn.setVisibility(View.VISIBLE);
bbtn.setVisibility(View.VISIBLE);
cbtn.setVisibility(View.VISIBLE);
dbtn.setVisibility(View.VISIBLE);
edttexta.setVisibility(View.VISIBLE);
edttextb.setVisibility(View.VISIBLE);
edttextc.setVisibility(View.VISIBLE);
edttextd.setVisibility(View.VISIBLE);
edtquestion.setVisibility(View.VISIBLE);
edttext.setVisibility(View.VISIBLE);
menu.setVisibility(View.INVISIBLE);
finalCSCount.setVisibility(View.INVISIBLE);
}
private void setRandomArray(){//makes sure that duplicate questions do not appear
random++;
while (true){
if(randomArray[randomArrayCounter]==random) {
random = new Random().nextInt(20);
random++;
randomArrayCounter = 0;
while (randomArray[randomArrayCounter]==random){
if (randomArray[randomArrayCounter]==random){
random = new Random().nextInt(20);
random++;
randomArrayCounter = 0;
}
else{
break;
}
}
}
if (randomArray[randomArrayCounter]==0){
randomArray[randomArrayCounter]=random;
break;
}
randomArrayCounter++;
}
random--;
randomArrayCounter=0;
}
private void displayCorrect(){//if the answer is correct display screen
setVisibility();
finalCSCount.setText("Thats Correct!");
menu.setText("Next Question");
}
private void displayWrong(){//if the answer is wrong display screen
setVisibility();
finalCSCount.setText("Thats incorrect. The correct answer was " + correct[random]);
menu.setText("Next Question");
}
}