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

  Videorental store operatorrecord clients

Project is to design a program to help a videorental store operatorrecord clients' transactions - Design a primitive database indicating

  Simulate some people catching fish in a lake in java program

In this project, you will simulate some people catching fish in a lake. The purpose of the assignment is to get used to using Arrays as well as getting more experience in having objects interact together.

  Modify the scholarship application

Modify the Scholarship application so that if a user enters a grade point average under 0 or over 4.0, or a negative value for either of the activities, an error message appears.

  Eclipse or netbeans environments

As recommendation, you can try to use Eclipse or NetBeans environments that are used also for other courses as: Introduction in Java programming and Intermediate programming.

  Difference between the two following statements

What exactly is the difference between the two following statements and which is preferred, please provide details: 1) frame.setSize(400, 300); // Set JFrame Size

  Create java program to describe inheritance and polymorphism

Create a Java program based on the geometric shapes example described at the beginning of this lesson using Classes Square, Triangle, Rectangle, and Circ leto help describe inheritance and polymorphism.

  Java program for line item application

This exercise explains you the process of testing and enhancing Line Item application. Open LineItemApp, Validator, Product, LineItem, and ProductDB classes that are in the c:java1.6ch06LineItem directory and review this code.

  Sets a loop to find 10 random generated

Write a java code that 1) uploads a file 2) sets a loop to find 10 random generated words from the file and then search those words by a. IndexSequential Method b. indexBinary Method. then, report how much time both methods took.

  Multiple choice java programming questions

Determine which of the following may be a violation of information hiding if inserted for the comment above?

  Java application prompt user to put in integer from keyboard

Write a java application that performs the following task: prompt user to put in an integer from the keyboard, search for the user input from the array created in step 1.

  Creates an array named odds

Write code that creates an array named odds and stores all odd numbers between -6 and 38 into it using a loop. Make the array's size exactly large enough to store the numbers.

  Robot preparing for competition

Create an event so that the skateboard can be controlled by game player

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