Download Lab 7c: Test Questions - Campbell County Schools

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab 7c: Test Questions
Lab 7c: Test Questions
In this exercise you will use inheritance to read, store, and print questions for a test.
1.
Abstract class TestQuestion7c
First, write an abstract class TestQuestion7c that contains the following:
a. A protected String variable that holds the test question.
b. An abstract method protected abstract void readQuestion( Scanner scan ) to read the
question.
2.
Subclasses Essay7c and MultiChoice7c
a. Now define two subclasses of TestQuestion7c, Essay7c and MultiChoice7c.
b. Essay7c will need an instance variable to store the number of blank lines needed after the question (answering
space).
c. MultiChoice7c will not need this variable, but it will need an array of Strings (use an ArrayList) to hold the
choices.
3.
Code Essay7c and MultiChoice7c methods
a.
toString() - You will also need to write toString() methods for the MultiChoice7cand Essay7c
classes that return nicely formatted versions of the questions [e.g., the choices should be lined up, labeled a), b),
etc, and indented in MultiChoice7c].
b. readQuestion() - You will need to write readQuestion() methods for the MultiChoice7cand
Essay7c classes that read information in this format below.
The following section describes the format for which the data will enter the program: File data format
Essay7c
i. If question is “essay”
1. (integer) Number of blank lines after essay
2. (String) Essay question
MultiChoice7c
ii. If question is “multiple choice”
1. (integer) number of possible answers for multiple choice
2. (String) choice 1 (multiple choice only)
3. (String) choice 2 (multiple choice only)
4. ... continue until # of answers is reached ..
The very first item of input, before any questions, is an integer indicating how many questions will be entered. So the following input:

represents three questions,

an essay question requiring 5 blank lines,

a multiple choice question with 4 choices,

and another essay question requiring 10 blank lines.
Sample Input
3
e
5
Why does the constructor of a derived class have to call the constructor
of its parent class?
m
4
Which of the following is not a legal identifier in Java?
guess2
2ndGuess
_guess2_
Guess
e
10
What does the “final” modifier do?
Page 1 of 3
Inheritance
Lab 7c: Test Questions
4.
Modify RunTest7c
Add the functionality that creates an array of TestQuestion7c objects. It should:
a. Read # of questions - Read the questions from the standard input as follows in the format above, first reading an
integer that indicates how many questions are coming.
b.
c.
Create correct question and read - It should create a MultiChoice7c object for each multiple choice question
and an Essay7c object for each essay question and store each object in the array. (Since it's an array of
TestQuestion7c and both Essay7c and MultiChoice7c are subclasses of TestQuestion7c , objects
of both types can be stored in the array.)
Print out Test - When all of the data has been read, it should use a loop to print the questions, numbered, in order.
Use the data in testbank.dat to test your program
Notify Mrs. DeGroff upon completion. She will grade your lab from your computer.




TestQuestion7c.java
Essay7c.java
MultiChoice7c.java
RunTest7c.java
RunTest7c.java
// ****************************************************************
// RunTest7c.java [Lab 7c]
//
Student Name: <name>
//
// Purpose: Reads a flat file and builds a test composed of Essay
// and Multiple choice questions. Once it has built the test, it
// will print the output.
//
// ****************************************************************
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RunTest7c
{
public static void main( String[] args )
{
// -----------------------------------------------------// Initialize variables
Scanner scan = null;
try
{
// -----------------------------------------------------// Open File
scan = new Scanner( new File( "testbank.dat" ) );
// -----------------------------------------------------// TODO: 4a) Read # of questions
//
//
//
//
//
//
//
-----------------------------------------------------TODO: 4b) Create new question:
i) Determine the type of question
ii) Create that type of question object
iii) Pass "scan" to the question so it can read in the
values
iv) Add the question to the test list
// -----------------------------------------------------// TODO: 4c) Loop through questions and print out test
}
catch ( FileNotFoundException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
Page 2 of 3
Inheritance
Lab 7c: Test Questions
}
finally
{
if ( scan != null )
{
scan.close();
}
}
}
}
testbank.dat
5
e
5
Why does the constructor of a subclass class have to call the constructor of its parent class?
m
4
Which of the following is not a legal identifier in Java?
guess2
2ndGuess
_guess2_
Guess
e
5
What does the “final” modifier do?
e
3
Java does not support multiple inheritance. This means that a class cannot do what?
m
3
A JPanel has an addMouseListener method because JPanel is a subclass of
JComponent
JApplet
Object
Sample Output
Why does the constructor of a subclass class have to call the constructor of its parent class?
Which of the following is not a legal identifier in Java?
1) guess2
2) 2ndGuess
3) _guess2_
4) Guess
What does the “final” modifier do?
Java does not support multiple inheritance.
This means that a class cannot do what?
A JPanel has an addMouseListener method because JPanel is a subclass of
1) JComponent
2) JApplet
3) Object
Page 3 of 3
Inheritance