Use inheritance to read, store, and print questions for test

Assignment Help JAVA Programming
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();

}

}

}

 

 

Reference no: EM13162402

Questions Cloud

What mass in grams of solid forms : 57.0 mL of 3.50 M sodium hydroxide is combined with 35.0 mL of 1.80 M magnesium chloride. What mass in grams of solid forms?
Prepare a memorandum of association under uae companies : A brief introduction of your company and reason for choosing this form of business organization and prepare a Memorandum of Association under UAE commercial companies law 1984 for your company
What is the molar mass : An experiment shows that a 115 mL gas sample has a mass of 0.170 g at a pressure of 711 mm Hg and a temperature of 33 Celcius. What is the molar mass?
Calculate activation energy for the reaction : The Reaction 2NOCl(g)---->2NO+CL2(g) has rate-constant values for the reaction of NOCl of 9.3x10^-6/s at 350K and 6.9x10^-4/s at 400K. Calculate activation energy for the reaction. What is the rate constant at 435K?
Use inheritance to read, store, and print questions for test : use inheritance to read, store, and print questions for a test. First, write an abstract class
What is the molarity of the ethanol in this solution : A solution of ethanol (C2H5OH) in water is prepared by dissolving 63.0 mL of ethanol (density = 0.79 g/cm3) in enough water to make 255.0 mL of solution. What is the molarity of the ethanol in this solution?
Write a net ionic equation for the precipitation reaction : Write a net ionic equation for the precipitation reaction, if any, that occurs when aqueous solutions of the following ionic compounds are mixed. Cu(Ch3COO)2 and Na3PO4.
Write the formula of the reactants : Write the formula of the reactants and products-including the symbols for the state- (s), (l), (g), (aq)- then balance the equation.
Implement the boolean function : implement the following Boolean function F, together with the don't care conditions d, USE NO MORE THAN TWO NOT GATES:

Reviews

Write a Review

JAVA Programming Questions & Answers

  Rock-paper-scissors :- java problem

Design decision marks are based on how you implemented our programs/classes.

  The williamsburg women''s club

The Williamsburg Women's Club offers scholarships to local high school students who meet any of several criteria. Write an application that prompts the user for a student's numeric high school grade point average (for example, 3.2), the student's num..

  Write java program which simulates flipping of coin

Write a Java program which simulates flipping of coin 1000 times and prints total number of heads and tails. You should create a class.

  Write java program to print price of table-furniture company

Write down the Java program for furniture company. Ask user to select P for Pine, O for Oak or M for Mahogany. Illustrate the price of table produced with chosen wood.

  Write a method called negative sum

Write a method called negativeSum that accepts a Scanner reading input from a file containing a series of integers, and print a message to the console indicating whether the sum starting from the first number is ever negative

  Java applet to find how much federal tax we need to pay

Write a java applet to determine how much federal tax we need to pay assuming the tax rate is 12%.  Ask the taxable income.

  Java program use breadth-first search closest broadcast

Write the java program which will use breadth-first search (which you implement as part of your program) to determine the closest broadcast vertex for each vertex in graph.

  Uml exercise

UML Exercise: Automated Teller Machine (ATM),   1. To allow authorized card holders to make transactions,   Brief Summary of Requirements:

  Shows the users name and program name

Java program, the program has a page that shows the users name and program name. a second jpanel that shows 4 buttons (circle square rectangle and oval) the problem i am having is that my program is not dropping where i click and the shapes are not s..

  Develop parent lock method asks for four digit password

Develop the parent lock method (turns lock on/off) which asks for 4 digit password. Navigating to any channel which is in range 99-110 must ask for password when parent lock is on.

  Modify each sorting algorithm

Modify each sorting algorithm so that it keeps track of the number of comparisons it performs and the number of exchanges (swaps) it performs during a single sorting operation. Keep track of these numbers using two long integer counters

  Write a class array that encapsulates an array

Write a class Array that encapsulates an array and provides bounds-checked access. The private instance variables should be int index and inarray[10]. The public members should be a default constructor and methods (signatures shown below) to provide ..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd