Background

As described in the handout for Part 1, the overall aim of the assignment is to develop a program to administer a quiz test.

In Part 1, you have implemented the classes to represent the quiz questions and answers. You have also written programs to test these classes. In particular, you have written a program to read a number of questions from a text file, administer the questions, obtain the answers, and print out the answer.

Building on the work that you have done for Part 1, in this Part 2, you are required to do the tasks described below.

Besides the information given in the tasks below, please refer to Part 1 of the Assignment for any information you need.

Task 1

Modify the classes QuizQuestion and QuizAnswer and their subclasses to use ArrayList in- stead of array whenever you need to maintain a collection.

Modify the QuizQuestionTester program from Part 1 to test your new classes.

Note: Even if you have used ArrayList for Part 1, you still need to submit all the files listed above.

Task 2

(For Tasks 2 and 3, you must also use ArrayList whenever you need to maintain collections.)

Write a menu program called QuizMenu. The program displays the menu with the options shown below:

========
Options:
========
R: Read question details from a text file
A: Administer the quiz test
F: Administer the quiz test with option to quit
D: Display the answers on the scr
W: Write the answers to a text file
Q: Quit
Please select an option:

Other than the minimum required for the program to compile, no exception handling is required for this task.

Option R. The program asks for the name of the text file (e.g. Quiz1.txt) and read the ques- tions from this file. The file name (to be entered) includes the file extension. The file format is exactly as described in Part 1.

Option A. The way the program administers the quiz questions and obtains the answers is ex- actly as described in Part 1.

Option F. (F stands for Flexible administration of the quiz test). With this option, after answering a quiz question, the user will be asked if he/she wants to continue or not. If the user chooses to quit early, option D is still available to display the results of the questions has been answered. The behavior of the program is illustrated with a sample run below:

========
Options:
========
R: Read question details from a text file
A: Administer the quiz test
F: Administer the quiz test with option to quit
D: Display the answers on the screen
W: Write the answers to a text file
Q: Quit
Please Select an option: f
Question 1
try/catch blocks can be nested.
Type true or false: true
Do you want to continue? (y/n): y

Question 2
ArithmeticException is an _____ exception. Fill in the blank.
Type your answer: unchecked
Do you want to continue? (y/n): n
========
Options:
========
R: Read question details from a text file
A: Administer the quiz test
F: Administer the quiz test with option to quit
D: Display the answers on the screen
W: Write the answers to a text file
Q: Quit
Please Select an option:

Option D. The way the program displays the answer is illustrated by the example below:

QUESTION 1:
A checked exception must be caught or declared.
True or false?
Your answer is: true
It is correct.
QUESTION 2:
What is the keyword used to declare a class to be a subclass of another class?
a) implements
b) extends
c) subclass
d) interface
Your answer is: c
It is incorrect.
The correct answer is: b
Number of questions: 5
Number of answers: 2
Number of correct answers: 1
Number of incorrect answers: 1

As illustrated above, the display of the result of a quiz test ends with four lines of summary.

Option W. Theprogramasksforthenameofthefiletosavetheresultto(e.g. Quiz1Result.txt). The information to be written and the format are exactly as for displaying the result on the screen.

Task 3

Once we start the menu program and take a few options, we do not want the program to crash.

For this task, you are required to enhance the program to achieve this objective. More specifically, the program is made robust in the following sense:

when the program carries out the operation required for an option, if some exceptional condition arises, this operation is aborted but the program itself does not crash. Thus, all the work up to that point is preserved and further operations, if appropriate, can be taken.

Make a copy of the previous menu program and call it QuizMenuRobust.java. Modify it to make it robust as described above.

Task 4

Refer to the Tower of Hanoi problem covered in the lectures. The following program is written to find the number of moves it takes to move n discs (n = 1 to 64).

public class IntTowerMoves
{
public static int moves(int numberOfDiscs)
{
if (numberOfDiscs == 1)
return 1;
else
return 1 + 2 * moves(numberOfDiscs - 1);
}
public static void main(String [] args)
{
for(int n = 1; n <= 64; n++)
System.out.printf("%3dt%,dn", n, moves(n));
}
}

Run it and you will see that when n = 32, the output is -1, because the number of moves is too big for int (overflow).

Let us try to use long for number of moves as in the program below:

public class LongTowerMoves
{
public static long moves(int numberOfDiscs)
{
if (numberOfDiscs == 1)
return 1;
else
return 1 + 2 * moves(numberOfDiscs - 1);
}
public static void main(String [] args)
{
for(int n = 1; n <= 64; n++)
System.out.printf("%3dt%,dn", n, moves(n));
}
}

With this new version, when n = 64, we get an overflow.

Now, your task will be to solve this problem using the BigInteger class.

Look up the BigInteger class and use it to modify one of the programs above so that we can get the number of moves when n = 64. Call your new class BigIntegerTowerMoves

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.