-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAminoAcidQuiz.java
89 lines (64 loc) · 2.77 KB
/
AminoAcidQuiz.java
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
//this code is an amino acid quiz
//The quiz ends after 30 seconds or when there is a single incorrect answer.
//The program displays the full name of an amino acid (like “alanine” ) and asks the user to type in the one character code (like a)
//The quiz ignores case in the answer.
//The program displays the amino acids in random order, can repeat amino acids and can show more than 20 if the user gets that many correct.
//The total score is the number of correct answers
package jCode;
import java.util.Random;
public class AminoAcidQuiz {
public static void main(String[] args) {
//sets up array of amino acid 1 letter code
String[] SHORT_NAMES =
{ "A","R", "N", "D", "C", "Q", "E",
"G", "H", "I", "L", "K", "M", "F",
"P", "S", "T", "W", "Y", "V" };
//sets up array of amino acid full name
String[] FULL_NAMES =
{
"alanine","arginine", "asparagine",
"aspartic acid", "cysteine",
"glutamine", "glutamic acid",
"glycine" ,"histidine","isoleucine",
"leucine", "lysine", "methionine",
"phenylalanine", "proline",
"serine","threonine","tryptophan",
"tyrosine", "valine"};
//sets up start time variable
long start = System.currentTimeMillis();
//sets the stop time as 30 seconds
long stop = start + 30000;
//sets up empty count variable to add score to
int count = 0;
// says to execute the code while the time is less than or equal to the stop variable of 30 seconds
while (System.currentTimeMillis() <= stop) {
Random random = new Random();
//gets random index from the full names array
int q = random.nextInt(FULL_NAMES.length);
System.out.println(FULL_NAMES[q]);
//takes user input and assigns to a variable
String aString = System.console().readLine().toUpperCase();
//if the users input correlates to the correct amino acid then run the code
if(aString.equals(SHORT_NAMES[q])) {
//add 1 to the score when correct
count = count + 1;
System.out.println("Number correct: " + count);
//calculates the time elapsed
long current = System.currentTimeMillis();
long totalTime = (current - start)/1000;
System.out.println("Time elapsed: " + totalTime + " seconds");
}
//if the user inputs 'quit', the code will end
else if (aString.equals("QUIT")) {
System.out.println("You have selected to quit. Quiz is now over");
return;
}
//if the users input does not correlate to the correct amino acid then run the code
else {
long current = System.currentTimeMillis();
long totalTime = (current - start)/1000;
System.out.println("That was incorrect. Total correct: " + count + " Time elapsed: " + totalTime + " seconds");
//end the loop
return;
}
}