Ask Java Expert


Home >> Java

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

{

answer[x] = scan.nextLine();

}

}

public String toString()

{

System.out.println(Question);

for (int x=0; 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

{

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();

}

}

}

 

 

Java, Programming

  • Category:- Java
  • Reference No.:- M9452525
  • Price:- $55

Priced at Now at $55, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As