forked from e2xperimental/Programming-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriverExam.java
More file actions
118 lines (93 loc) · 3.12 KB
/
Copy pathDriverExam.java
File metadata and controls
118 lines (93 loc) · 3.12 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
/*
# 08
11/25/2010
Driver's License Exam
*/
import java.util.Scanner; // Needed for the Scanner class
public class DriverExam
{
public static void main(String[] args)
{
// Create an array init to 0
char[] answerKey = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'},
studentAns = new char[answerKey.length];
double passing = 65.0;
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
String input; // To hold a line of input
char answer; // To hold a single character;
// Ask the user to enter a student's answers.
for (int i = 0; i < answerKey.length; i++)
{
System.out.print("What is the answer to question #" + (i + 1) + "? ");
input = keyboard.nextLine();
studentAns[i] = input.charAt(0);
}
// Demonstrate the totalCorrect() function.
System.out.println("Total correct: " + totalCorrect(studentAns, answerKey) );
// Demonstrate the totalncorrect() function.
System.out.println("Total incorrect: " + totalIncorrect(studentAns, answerKey) );
// Demonstrate the questionsMissed() function.
int[] missedQuestions = questionMissed(studentAns, answerKey);
printMissedQuest(missedQuestions);
// Demonstrate the passed() function.
if (passed(studentAns, answerKey, passing) )
System.out.print("You passed!");
else
System.out.print("You failed. Better luck next time.");
System.out.println(" Passing is " + passing + "%.");
}
// Returns true if the student passed the exam or false if the student failed.
private static boolean passed(char[] exam, char[] answers, double passing)
{
double percentageCorrect = (totalCorrect(exam, answers) / answers.length * 100);
return (percentageCorrect >= passing);
}
// Returns the total number of correctly answered questions
private static int totalCorrect(char[] exam, char[] answers)
{
int numCorrect = 0;
for (int i = 0; i < exam.length; i++)
if (exam[i] == answers[i])
numCorrect++;
return numCorrect;
}
// Returns the total number of incorrectly answered questions
private static int totalIncorrect(char[] exam, char[] answers)
{
int numIncorrect = 0;
for (int i = 0; i < exam.length; i++)
if (exam[i] != answers[i])
numIncorrect++;
return numIncorrect;
}
// An int array containing the question numbers of the questions that the student missed.
private static int[] questionMissed(char[] exam, char[] answers)
{
int[] missedQuestions = new int[exam.length];
for (int i = 0; i < exam.length; i++)
if (exam[i] != answers[i])
missedQuestions[i] = (i+1);
return missedQuestions;
}
// Prints the values in a char array.
private static void printArray(char[] exam)
{
for (int row = 0; row < exam.length; row++)
{
System.out.print(exam[row] + " ");
}
System.out.println();
}
// Prints the values in an int array that are non-zero.
private static void printMissedQuest(int[] missedQ)
{
System.out.print("Questions missed: ");
for (int row = 0; row < missedQ.length; row++)
{
if (missedQ[row] != 0)
System.out.print(missedQ[row] + ", ");
}
System.out.println();
}
}