Demonstrate the ability to
u:\Lab2\ with two Java code files:
MultipleChoiceQuestion.java and Driver.java.
BufferedReader object to read input from a file
for loops to sequentially process the
objects in an array. u:\Lab2\.
Driver should contain the main method
MultipleChoiceQuestion (code
given below) is used to hold one multiple choice question along with its
correct answer. You do not need to modify this class.
Driver class to
Follow the sample session shown below:
Enter the name of the questions file: questions.txt
1. The field used to determine the number of elements in an array is called
A. size
B. length
C. last
D. address
Correct answer is B
2. Each element of an array is accessed by a number known as a(n)
A. subscript
B. size declarator
C. address
D. specifier
Correct answer is A
3. The first subscript in an array is always
A. 1
B. 0
C. -1
D. 1 less than the number of elements
Correct answer is B
4. The last subscript in an array is always
A. 100
B. 0
C. -1
D. 1 less than the number of elements
Correct answer is D
5. Array bounds checking happens
A. when the program is compiled
B. when the program is saved
C. when the program runs
D. when the program is loaded into memory
Correct answer is C
If you finish early, get yourself checked off first! As a programming challenge (not required but recommended)
Driver2.java
Vector (not an array of objects). Get it working for
five questions.
Copy and paste the MultipleChoiceQuestion class code into your
file
public class MultipleChoiceQuestion
{
// Instance variables
private String question;
private String choiceA;
private String choiceB;
private String choiceC;
private String choiceD;
private char correctAnswer;
// Constructor: no args
public MultipleChoiceQuestion()
{
question = "";
choiceA = "";
choiceB = "";
choiceC = "";
choiceD = "";
correctAnswer = 'A';
}
// Constructor
public MultipleChoiceQuestion(String q, String a, String b, String c,
String d, char answer)
{
question = q;
choiceA = a;
choiceB = b;
choiceC = c;
choiceD = d;
correctAnswer = answer;
}
// Returns the question and four choices
public String getQuestion()
{
return (question + "\n\t" + choiceA + "\n\t" + choiceB + "\n\t" +
choiceC + "\n\t" + choiceD + "\n");
}
// Returns the correct answer
public char getAnswer()
{
return correctAnswer;
}
}
Create a text file in your project folder called questions.txt
and copy and paste these test questions into it for testing purposes.
Note: Each question is represented as six lines of input
The field used to determine the number of elements in an array is called A. size B. length C. last D. address B Each element of an array is accessed by a number known as a(n) A. subscript B. size declarator C. address D. specifier A The first subscript in an array is always A. 1 B. 0 C. -1 D. 1 less than the number of elements B The last subscript in an array is always A. 100 B. 0 C. -1 D. 1 less than the number of elements D Array bounds checking happens A. when the program is compiled B. when the program is saved C. when the program runs D. when the program is loaded into memory C