CSE1/CSE4IOO Semester 1, 2019
Assignment – Part 2
Assessment: This Part 2 of the assignment is worth 15% of the final mark for this
subject.
Due Date: Wednesday 29 May 2019, at 10.00 am
Delays caused by computer downtime cannot be accepted as a valid reason for a late
submission without penalty. Students must plan their work to allow for both scheduled
and unscheduled downtime. Penalties are applied to late assignments (accepted up to 5
days after the due date only). See the university policy for details.
Individual Assignment: This is an individual assignment. You are not permitted to
work as a group when writing this assignment.
Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a manner
that gives the impression that the work is your own. The Department of Computer
Science and Information Technology treats academic misconduct seriously. When it is
detected, penalties are strictly imposed. Refer to the unit guide for further information
and strategies you can use to avoid a charge of academic misconduct. All submissions will
be electronically checked for plagiarism.
Objectives: The general aims of this assignment are:
To analyze a problem in an object-oriented manner, and then design and implement
an object-oriented solution that conforms to given specifications
To practise using inheritance in Java
To practise file input and output in Java
To make implementations more robust through mechanisms such as exception handling.
Submission Details: Instructions on how to submit electronic copies of your source code
files from your latcs8 account and a marking scheme overview are given at the end. If
you have not been able to complete a program that compiles and executes containing all
functionality, then you should submit a program that compiles and executes with as much
functionality as you have completed. (You may comment out code that does not compile.)
Deployment Platform: While you are free to develop the code for this assignment on
any operating system, your solution must run on the latcs8 system. We should be able to
compile your classes with the simple command javac *.java, and execute your programs
with a simple command, e.g. java QuizMenu.
For students at Sydney and Dandenong campuses: Please follow your lecturer’s
instructions regarding assignment submission and deployment platform.
1
Background
As described in the handout for Part 1, the overall aim of the assignment is to develop a
menu-driven 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 answers.
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 other information you may need.
Task 1
Modify the classes QuizQuestion and QuizAnswer and their subclasses to use ArrayList
instead of array whenever you need to maintain a collection.
Test your classes with the provided BasicTester program.
Task 2
(For Tasks 2, 3 and 4, you must also use ArrayList whenever you need to maintain
collections.)
This task does not required you to do any exception handling. (That will done in Tasks
3 and 4).
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 screen
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. qq.txt) and reads the
questions from this file. The file name (to be entered) includes the file extension. The file
format is exactly as described in Part 1.
2
Option A. The way the program administers the quiz questions and obtains the answers
is exactly 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, options D and W are still available to display and save the
results of the questions have 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
A try statement must have a catch block.
True or false?
Type true or false in lower case: true
Do you want to continue? (y/n): y
Question 2
FileNotFoundException is a/an _____ exception. Fill in the blank
Type in your answer: checked
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:
3
Option D. The way the program displays the answer is illustrated by the example below:
Question 1
TrueFalseAnswer[quizQuestion: TrueFalseQuestion[question: A try statement must have
a catch block., correctAnswer: false]
, userAnswer: true, result: I]
Question 2
WordAnswer[quizQuestion: WordQuestion[question: FileNotFoundException is a/an _____
exception. Fill in the blank., correctAnswer: checked, caseSensitive: false]
, userAnswer: checked, result: C]
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. The program asks for the name of the file to save the result to (e.g. result.txt).
The information to be written and the format are exactly as for displaying the result on
the screen.
Hints:
For each menu option (except “quit”), you should have a method to carry out the
required operation.
Test your program with both qq.txt and qqe.txt (which has a format error).
4
Task 3
The menu-driven program of Task 2 can crash. For Task 3, you are required to enhance
the program of Task 2 to prevent it from crashing.
More specifically, we want to achieve the following objective:
When the program carries out the operation required for a menu option, if some
exceptional condition arises, the program will display a message to inform the
user of the occurrence of the exception, terminate the operation and return to
the main menu.
In this way, the program does not crash, and all the work up to that point
(before the failed operation) is preserved and further operations 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
We now seek to enhance the program of Task 3 to remove some of its shortcomings.
Examples of shortcomings of program QuizMenuRobust
It does not make sense to administer a quiz when it has not been read. So ideally, if
the user tries to administer a quiz before it is read, the program should display an error
message, and terminate the operation and return to the main menu.
This is not the behavior of program of Task 3. How it behaves depend on whether the list
of question instances has been initialized to an empty list or not.
– If the list of answer objects has not been initialized, i.e. it is a null object, a message
about null pointer exception is displayed before returning to the main menu.
– If the list has been initialized to an empty ArrayList, the program returns immediately
to the main menu because an empty list of questions has been administered.
As another example, if you read the file qqe.txt, an exception occurs, and the program
returns to the main menu. But a number of questions has been read, and the list has a
number of question instances. You can see this by choosing option A next.
This behavior is generally not regarded as being satisfactory. Normally, if an operation
like this fails, we would like to restore things to their states prior to the failed operation.
(This is what rollback operation in database is about.)
The state of the menu-driven program
As mentioned before, if the user tries to administer a quiz before it is read, the program
should display an error message, and terminate the operation and return to the main
menu.
In order to make that sort of behavior possible, one solution is to regard the program as
what is known as a state machine. By that, we mean we regard the program an entity
that can go through a number of states and the state that it is in can affect its behavior.
5
As a solution adequate for our purpose, we can regard the program as an entity that can
be in one of three states represented by the three constants declared below:
private static final int QUIZ_NOT_READ = 1;
// A quiz has not been read or has not been read successfully.
// When the program is in this state, there is not available
// a list of quiz objects to be administered.
private static final int QUIZ_AVAILABLE = 2;
// A quiz has been read and is available but it has not been
// administered. When the program is in this state, there is not
// a list of answer objects to be displayed or saved.
private static final int QUIZ_TAKEN = 3;
// A quiz has been administered, and the answers are available
// for displaying or saving to file
// More information related to these states is given below
Then we should have a variable – let us call it state – to keep track of the state that the
program is in.
Initially, the state should be QUIZ_NOT_READ. When a quiz is successfully read in, the state
should be QUIZ_AVAILABLE. When a quiz is successfully administered, completely or with
early exit, the state should be QUIZ_TAKEN.
Enhancement features
Make a copy of QuizMenuRobust, change the name to QuizMenuEnhanced, and modify it
to implement the enhancement features described below:
For option R (read questions)
E1 If the operation completes successfully, the state must be set to QUIZ_AVAILABLE
E2 If the operation fails (and throws an exception),
The list of question objects must be set to null
The state must be set to QUIZ_NOT_READ
E3 If the user chooses option R when the state is QUIZ_AVAILABLE or QUIZ_TAKEN,
Ask the user if they really want to read a new quiz, and then proceed accordingly.
If the user confirms that they want to proceed with reading in a new quiz, then
we prepare a “clean sheet” first with the following actions
– Set the state to QUIZ_NOT_READ
– Set the lists of question and answer objects to null
before proceed to reading the questions.
NOTE: If the subsequent reading is completed successfully, the state must be set to
QUIZ_AVAILABE. If the subsequent reading fails, the state must be QUIZ_NOT_READ.
See E1 and E2 above.
6
For options A and F (administer the quiz)
E4 If the operation completes successfully, the state must be set to QUIZ_TAKEN.
E5 If the operation fails,
The list of answer objects must be set to null
the state must be QUIZ_AVAILABE
E6 If the user tries option A or F when the state is QUIZ_NOT_READ, display an error
message, terminate the operation and return to the main menu.
E7 If the user tries to administer a quiz when the state is QUIZ_TAKEN,
Ask the user if they really want to take the test again, and then proceed accordingly.
If the user confirms that they want to proceed with this option, then
– The list of answer instances must be set to null
– The state must be set to QUIZ_AVAILABLE
NOTE: If the subsequent administering operation is completed successfully, the
state must be set to QUIZ_TAKEN. If the operation fails, the state must be
QUIZ_AVAILABLE. See E4 and E5.
For options D and W (display and save answers)
E8 If the user tries option D or W when the state is not QUIZ_TAKEN, display an error
message, terminate the operation and return to the main menu.
Task 5. (For CSE4IOO students only)
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("%3d\t%,d\n", 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).
7
Now, 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("%3d\t%,d\n", 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
(The total mark for Part 2 will be 100 for CSE1IOO students and 110 for CSE4IOO
students. The percentage of contribution to the final mark will be the same.)
Electronic Submission of the Source Code
Put in a directory whose name starts with your student id, followed by
‘‘_IOO_Assignment_Part2’’:
QuizQuestion.java
TrueFalseQuestion.java
WordQuestion.java
ChoiceQuestion.java
QuizAnswer.jva
TrueFalseAnswer.java
WordAnswer.java
ChoiceAnswer.java
BasicTester.java
QuizMenu.java
QuizMenuRobust.java
QuizMenuEnhanced.java
qq.txt
qqe.txt
BigIntegerTowerMoves.java (for CSE4IOO only)
8
Submit the whole directory, by using the submit command with the directory name as
the argument, e.g.
submit IOO 20191234_IOO_Assignment_Part2
After submitting the directory, you can run the following command that lists the files
of the directory you have submitted:
verify
You can submit as many times as you like before the assignment deadline; the previously
submitted copy will be replaced by the latest one.
Marking Scheme Overview
Implementation and Execution for Tasks 1-4: 100 marks
Do all parts of the programs execute correctly?
Are the classes implemented according to the given requirements?
You must make every effort to submit an assignment that compiles and runs. You
should comment out the parts that do not compile or run. A program that does not
compile or run, and especially if it contains serious errors, may receive 0 marks.
The breakdown of the 100 marks is as follows:
Task 1: 10 marks
Task 2: 50 marks
Task 3: 16 marks
Task 4: 24 marks
Program Design and Coding Style: A possible deduction of up to 10 marks.
A deduction may be applied in the case your assignment seriously fails in the following
aspects: Does the program follow good programming practices? Does the indentation
and code layout follow a good, consistent standard? Are the identifiers meaningful? Are
comments being used effectively?
Task 5 (for CSE4IOO only): 10 marks
Return of Assignments
Department policy requires that assignments are returned within 3 weeks of the submission
date. Students will be notified by email and via the CSE1/CSE4IOO LMS forum when
marking sheets are available for collection.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。