CS 111: Lab 2

Student Learning Objectives

Demonstrate the ability to

Lab 2 Assignment

Sample Session

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

To Receive Credit

Programming Challenge

If you finish early, get yourself checked off first! As a programming challenge (not required but recommended)

  1. Re-save your Driver program as Driver2.java
  2. First, modify your program read from the file and save the file input in a Vector (not an array of objects). Get it working for five questions.
  3. Next, modify your program so that it will read input from the file and store them in the Vector until the end of the file. Add more questions to your input file and test your program.
  4. Once that works, change your definition of the Vector to use type-safe generic data type.

MultipleChoiceQuestion Class

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;
      }
   }

questions.txt

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