patternjavaMinor
Driver license program which grades an individual's responses
Viewed 0 times
gradesindividualprogramlicenseresponsesdriverwhich
Problem
Are there any ways I can make my program more efficient? My program runs, but are there any things that are unneeded that I included?
The local Driver's License Office has asked you to write a program
that grades the written portion of the license exam. The exam has 20
multiple choice questions. Here are the correct answers:
A student must correctly answer 15 questions of the 20 questions to
pass the exam.
Write a class named DriverExam that holds the correct answers to the exam in an array field. The class should have an array field to
hold the student's answers. The class should have the following
methods:
passed: The method returns true if the student passed the exam, false
otherwise totalCorrect: returns the total number of correctly answered
questions totalIncorrect: returns the total number of incorrectly
answered questions questionsMissed: an int array containing the
question numbers of the question that the student missed
Demonstrate the class in a test program that asks the user to enter a
student's answers, and then display the results returned from the
DriverExam class's methods.
Input validation: only accept the letters A, B, C, or D as answers
```
public class DriverExam
{
//An array containing a student's answers
private String[] correctAnswers =
{"B", "D", "A", "A", "C", "A",
"B", "A", "C", "D",
"B", "C", "D", "A",
"D", "C", "C", "B", "D", "A"};
//Store the user's answers
private String[] userAnswers;
int[] missed = new int[correctAnswers.length];
//Process the user's answers
public DriverExam (String[] Answers)
{
The local Driver's License Office has asked you to write a program
that grades the written portion of the license exam. The exam has 20
multiple choice questions. Here are the correct answers:
1. B 2. D 3. A 4. A
5. C 6. A 7. B 8. A
9. C 10. D 11.B 12. C
13. D 14. A 15. D 16. C
17. C 18. B 19. D 20. AA student must correctly answer 15 questions of the 20 questions to
pass the exam.
Write a class named DriverExam that holds the correct answers to the exam in an array field. The class should have an array field to
hold the student's answers. The class should have the following
methods:
passed: The method returns true if the student passed the exam, false
otherwise totalCorrect: returns the total number of correctly answered
questions totalIncorrect: returns the total number of incorrectly
answered questions questionsMissed: an int array containing the
question numbers of the question that the student missed
Demonstrate the class in a test program that asks the user to enter a
student's answers, and then display the results returned from the
DriverExam class's methods.
Input validation: only accept the letters A, B, C, or D as answers
```
public class DriverExam
{
//An array containing a student's answers
private String[] correctAnswers =
{"B", "D", "A", "A", "C", "A",
"B", "A", "C", "D",
"B", "C", "D", "A",
"D", "C", "C", "B", "D", "A"};
//Store the user's answers
private String[] userAnswers;
int[] missed = new int[correctAnswers.length];
//Process the user's answers
public DriverExam (String[] Answers)
{
Solution
I would like to propose a number of refactorings:
I don't quite understand why the missed array is an
I don't quite understand why the missed array is an
int array which stores it's own indices if the answers are incorrect. A more logical choice would be a boolean array. We should also only calculate it only once, so we might as well do it in the constructer (which also only runs once per answer). The same applies to total number of correct/incorrect answers. The rest of my commentary is in the code.// this should be static since it can be shared between all instances of the test
private static String[] correctAnswers =
{"B", "D", "A", "A", "C", "A",
"B", "A", "C", "D",
"B", "C", "D", "A",
"D", "C", "C", "B", "D", "A"};
// lets leave initializing to the constructor, and
// let's store the values so we only calculate them once
// Also, make sure they are private to restrict access
private boolean[] missed;
private int correct;
private int incorrect;
private String[] userAnswers;
//Process the user's answers
public DriverExam (String[] answers)
{
missed = new boolean[answers.length];
userAnswers = new String[answers.length];
correct = 0;
incorrect = 0;
for (int i = 0; i 15
public boolean passed()
{
return correct >= 15; // don't use if/else when you are using a boolean expression
}
/*
* Let's use the values we calculated to make the methods
* very simple and easy to read. In addition, we only calculate things once
* which makes our code more efficient
*/
public int totalCorrect()
{
return correct;
}
public int totalIncorrect()
{
return incorrect;
}
public boolean[] questionsMissed()
{
return missed;
}Code Snippets
// this should be static since it can be shared between all instances of the test
private static String[] correctAnswers =
{"B", "D", "A", "A", "C", "A",
"B", "A", "C", "D",
"B", "C", "D", "A",
"D", "C", "C", "B", "D", "A"};
// lets leave initializing to the constructor, and
// let's store the values so we only calculate them once
// Also, make sure they are private to restrict access
private boolean[] missed;
private int correct;
private int incorrect;
private String[] userAnswers;
//Process the user's answers
public DriverExam (String[] answers)
{
missed = new boolean[answers.length];
userAnswers = new String[answers.length];
correct = 0;
incorrect = 0;
for (int i = 0; i < answers.length; i++)
{
userAnswers[i] = answers[i];
missed[i] = userAnswers[i].equalsIgnoreCase(correctAnswers[i])
if (!missed[i]) {
correct++;
} else {
incorrect++;
}
}
}
//Returns a boolean value if correct answers > 15
public boolean passed()
{
return correct >= 15; // don't use if/else when you are using a boolean expression
}
/*
* Let's use the values we calculated to make the methods
* very simple and easy to read. In addition, we only calculate things once
* which makes our code more efficient
*/
public int totalCorrect()
{
return correct;
}
public int totalIncorrect()
{
return incorrect;
}
public boolean[] questionsMissed()
{
return missed;
}Context
StackExchange Code Review Q#85280, answer score: 6
Revisions (0)
No revisions yet.