Reference no: EM13162402
In this exercise you will use inheritance to read, store, and print questions for a test. First, write an abstract class
TestQuestion that contains the following:
a) A protected String variable that holds the test question.
b) An abstract method protected abstract void readQuestion() to read the question.
Recall that the protected modifer provides public visibility for a variable or method only within the package.
Now define two subclasses of TestQuestion: Essay and MultChoice. Essay will need an instance variable to store the
number of blank lines needed after the question (answering space). MultChoice will not need this variable, but it will need an array of Strings to hold the choices along with the main question. Assume that the input is provided from the standard input as follows, with each item on its own line:
a) type of question (character, m=multiple choice, e=essay)
b) number of blank lines for essay, number of answer choices for multiple choice (integer)
c) choice 1 (multiple choice only)
d) choice 2 (multiple choice only) ...
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:
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?
You will need to write readQuestion methods for the MultChoice and Essay classes that read information in this format. (Presumably the character that identifies what kind of question it is will be read by a driver. Read p148-149 to refresh your memory of how to process input from a file.) You will also need to write toString methods for the MultChoice and Essay classes that return nicely formatted versions of the questions (e.g., the choices should be lined up, labeled a), b), etc, and indented in MultChoice).
Now define a class WriteTest that creates an array of TestQuestion objects. It should read the questions from the
standard input as follows in the format above, first reading an integer that indicates how many questions are coming. It should create a MultChoice object for each multiple choice question and an Essay object for each essay question and store each object in the array. (Since it's an array of TestQuestion and both Essay and MultChoice are subclasses of TestQuestion, objects of both types can be stored in the array.) 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 (shown on the following page) to test your program.
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
I have this code so far:
import java.util.Scanner;
public abstract class TestQuestion
{
protected String Question;
/**
* Constructor for objects of class MultChoice
*/
public TestQuestion(String question)
{
Question = question;
}
protected abstract void readQuestion(Scanner myFile);
}
import java.util.Scanner;
import java.io.*;
public class Essay extends TestQuestion
{
private int empties;
private String file;
/**
* Constructor for objects of class MultChoice
*/
public Essay(String Question, int noAns)
{
super(Question);
empties = noAns;
}
protected void readQuestion(Scanner File)
{
Scanner scan = File;
int empties = scan.nextInt();
Question = scan.nextLine();
}
}
import java.io.*;
import java.util.Scanner;
public class MultipleChoice extends TestQuestion
{
private int ansChoices;
String[] answer;
/**
* Constructor for objects of class MultChoice
*/
public MultipleChoice(String descision)
{
super(descision);
}
public void readQuestion(Scanner scan)
{
int ansChoices = scan.nextInt();
answer = new String[ansChoices];
Question = scan.nextLine();
for (int x = 0; x<ansChoices; x++)
{
answer[x] = scan.nextLine();
}
}
public String toString()
{
System.out.println(Question);
for (int x=0; x<ansChoices; x++)
{
System.out.println ((x+1) + "\t" + answer[x]);
}
return (null);
}
}
import java.util.Scanner;
import java.io.*;
public class WriteTest
{
public static void main (String[]args) throws IOException
{
Scanner scan = new Scanner (new FileInputStream("testbank.txt"));
int number = Integer.parseInt(scan.nextLine());
TestQuestion questionList[] = new TestQuestion[number];
for (int currentQuestion = 0; currentQuestion<number; currentQuestion++)
{
String type = scan.nextLine();
type = scan.nextLine();
if (type.equals("e"))
{
questionList[currentQuestion] = new Essay(null);
questionList[currentQuestion].readQuestion(scan);
questionList[currentQuestion].toString();
}
else if (type.equals("m"))
{
questionList[currentQuestion] = new MultipleChoice(null);
questionList[currentQuestion].readQuestion(scan);
questionList[currentQuestion].toString();
}
}
}