Lab 1. Beans,
EL, and JSTL
CS320,
Winter 2012
Due: Monday, February 6, 10:30PM
Please upload your files for Part II to CSNS.
The files should include all the source code, documentation (optional),
and an
HTML
file lab1.html
which
contains a link to your application deployed on the CS3
server. Note that
file
uploading will be disabled at 10:30PM, and no submission will be
accepted after that.
[Part I] (10pt) Please complete
Part I on CSNS.
[Part II] (30pt) For
this exercise you are going to implement
an online test application using JSP. The application reads a
number of
multiple-choice questions from a text file, displays the questions one
at a time, and collects the answers from a user. Once the user
completes all
the questions, the application compares the user's solution to the
standard solution and displays a score.
A sample file for a test can be downloaded here.
The format of the file is as follows:
- A test file contains a
number of test entries.
The exact
number of entries is unknown, but we do know that the entries are
separated from one another by an empty line.
- A test
entry consists of exactly
five lines: the
first line is the question, the next three lines are three possible
answers, and the last line is a number 1, or 2, or 3, which
indicates which answer is the correct answer.
A copy of the sample file
DrivingTest.txt has been placed on CS3
under the folder /tmp.
When you deploy your application on CS3 you may use the path /tmp/DrivingTest.txt to access
the file.
1. Implement a TestEntry
class which consists of at
least
the following methods:
- public TestEntry( String
question, String choiceA, String
choiceB, String choiceC, int correctAnswer )
- public String getQuestion()
- public String getChoiceA(),
public String getChoiceB(),
public
String getChoiceC()
- public void setAnswer( int
answer ) - set the answer to the
question by a user
- public boolean
isAnswerCorrect() - returns true
if the
user's answer matches the correct answer, or false
otherwise.
2. Implement a class TestBean
which consists of at
least the
following:
- A constructor which reads
the test entries from a file,
and store them in an ArrayList of TestEntry objects
- A read/write property currentTestEntryIndex
of type int.
This property specifies which test entry is current selected.
- A read-only property currentTestEntry
of type TestEntry.
This property is the currently selected entry.
- A write-only property answer
of type int. This property can be used to set the user's answer to the
currently selected test entry.
- A read-only property lastEntry
of type
boolean. The value
of this property is true
if the currently selected
entry is the
last entry in the test, or false
otherwise.
- A read-only property score
of type int.
The score is
calculated
as (100 * number_of_correct_answers / number_of_questions) rounded to
an integer.
3. Create a JSP page Test.jsp
that uses a session scope TestBean. Test.jsp
displays the test entries one at a time, and displays the score when
the user completes the test, as shown in the sample display below.
Note that you may not
use
scripting elements in the JSP.
Hints:
- You may use
Expression Language to access the properties of a TestEntry object. For
example,
suppose the id of your TestBean is tb,
you can
access the
question
part of the current test entry as ${tb.currentTestEntry.question}.
- You may want to use a hidden
form field for the index of
the
"next" entry.
- You may use the value of the
lastEntry property to decide
whether to display NEXT or FINISH.
[Post-Lab Activities]
- Note that although
the online test does not
provide a PREVIOUS button, a user can always use the Back button on a
browser to go back to a previous test entry, and there's no way to
prevent the user from doing that because browsers will cache the
previously viewed pages. Can your application handle this case
correctly? and if
not, how can you fix it?
- Generalize your application
so it can deal with any number
of
choices per test entry.
- Further generalize your
application so it can handle
multiple
correct answers per test entry.
- What if we want to show all
the questions in one page? and
in
that case, how do you collect the answers?