Question
Modify the given code to do the following: This is a driving test application, the code only lets users view each question and the correct
Modify the given code to do the following:
This is a driving test application, the code only lets users view each question and the correct answer. It should be modified to let the user take the exam and get a score.
When the web application is accessed by a user for the first time, it displays the first question in the test as shown below:
Clicking on Start Over should let the user re-take the test.
HINT: store the user's answers in an array in session scope; use the varStatus attribute of
This is the code for the .jsp file and the .java servlet file. The DrivingTest.txt file goes inside the WEB-INF folder in the eclipse project.
The Browser:
package DrivingExam;
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class DrivingTestBrowser */ @WebServlet("/DrivingTestBrowser") public class DrivingTestBrowser extends HttpServlet { private static final long serialVersionUID = 1L; ArrayList
/* * Init, read the file DrivingTest kept in Web-INF */ public void init() throws ServletException { super.init(); System.getProperty("user.dir"); ServletContext context = getServletContext(); String fullPath = context.getRealPath("/WEB-INF/DrivingTest.txt"); // read the file and populate the list of questions try { BufferedReader reader = new BufferedReader(new FileReader(fullPath)); String line = reader.readLine(); while ((line != null) && (!line.isEmpty())) { String desc = line; String ansA = reader.readLine(); String ansB = reader.readLine(); String ansC = reader.readLine(); int correct = Integer.parseInt(reader.readLine());
Question q = new Question(desc, ansA, ansB, ansC, correct); listOfQuestions.add(q); reader.readLine(); // flush the empty line
line = reader.readLine(); } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
} public DrivingTestBrowser() { super(); // TODO Auto-generated constructor stub }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int questionNo = 0; // get the question no if came in query parameters try { questionNo = Integer.parseInt(request.getParameter("id")); } catch (NumberFormatException e) { } // for last question, show the first one if(questionNo >= listOfQuestions.size()) questionNo = questionNo % listOfQuestions.size(); Question q = listOfQuestions.get(questionNo); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); // set tokens to be provided to JSP request.setAttribute("desc", q.getDesc()); request.setAttribute("answerA", q.getAnsA()); request.setAttribute("answerB", q.getAnsB()); request.setAttribute("answerC", q.getAnsC()); request.setAttribute("correctOption", String.valueOf(q.getCorrect())); // setting link for the next question request.setAttribute("link", "?id="+ String.valueOf(questionNo+1)); // forward the request request.getRequestDispatcher("/question.jsp").forward(request, response); } }
JSP
The Question Class for setter and getter:
package DrivingExam;
public class Question { String desc; String ansA; String ansB; String ansC; int correct;
public Question(String desc, String ansA, String ansB, String ansC, int correct) { this.desc = desc; this.ansA = ansA; this.ansB = ansB; this.ansC = ansC; this.correct = correct; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getAnsA() { return ansA; } public void setAnsA(String ansA) { this.ansA = ansA; } public String getAnsB() { return ansB; } public void setAnsB(String ansB) { this.ansB = ansB; } public String getAnsC() { return ansC; } public void setAnsC(String ansC) { this.ansC = ansC; } public int getCorrect() { return correct; } public void setCorrect(int correct) { this.correct = correct; } }
The Text File
You may drive off of the paved roadway to pass another vehicle: If the shoulder is wide enough to accommodate your vehicle If the vehicle ahead of you is turning left. Under no circumstances 3
You are approaching a railroad crossing with no warning devices and are unable to see 400 feet down the tracks in one direction. The speed limit is: 15 mph 20 mph 25 mph 1
When parking your vehicle parallel to the curb on a level street. Your front wheels must be turned toward the street. Your wheels must be within 18 inches of the curb. One of your rear wheels must touch the curb. 2
When you are merging onto the freeway, you should be driving: At or near the same speed as the traffic on the freeway. 5 to 10 MPH slower than the traffic on the freeway. The posted speed limit for traffic on the freeway. 1
When driving in fog, you should use your: Fog lights only. High beams. Low beams. 3
A white painted curb means: Loading zone for freight or passengers. Loading zone for passengers or mail only. Loading zone for freight only. 2
A school bus ahead of you in your lane is stopped with red lights flashing. You should: Stop, then proceed when you think all of the children have exited the bus. Slow to 25 MPH and pass cautiously. Stop as long as the red lights are flashing. 3
California's "Basic Speed Law" says: You should never drive faster than posted speed limits. You should never drive faster than is safe for current conditions. The maximum speed limit in California is 70 mph on certain freeways. 2
You just sold your vehicle. You must notify the DMV within ___ days. 5 10 15 1
To avoid last minute moves, you should be looking down the road to where your vehicle will be in about ______________. 5 to 10 seconds 10 to 15 seconds 15 to 20 seconds 2
You may drive off of the paved roadway to pass another vehicle: 1. If the shoulder is wide enough to accommodate your vehicle O 2. If the vehicle ahead of you is turning left. O 3. Under no circumstances O Next The user may select an answer and click the Next button to move to the next question. Ifthe user simply click on Next without selecting an answer, the application should send the user back to the current question. On the last question the application should display a Finish button instead of Next, e.g To avoid last minute moves, you should be looking down the road to where your vehicle will be in about 1. 5 to 10 seconds O 2. 10 to 15 seconds O 3. 15 to 20 seconds O Finish Clicking on the Finish button should take the user to a summary page like the following: Question Correct Answer Your Answer 1 3 3 3 2 1 (incorrect) 6 2 3 (ncorrect) Your score: 8/10 Start Over You may drive off of the paved roadway to pass another vehicle: 1. If the shoulder is wide enough to accommodate your vehicle O 2. If the vehicle ahead of you is turning left. O 3. Under no circumstances O Next The user may select an answer and click the Next button to move to the next question. Ifthe user simply click on Next without selecting an answer, the application should send the user back to the current question. On the last question the application should display a Finish button instead of Next, e.g To avoid last minute moves, you should be looking down the road to where your vehicle will be in about 1. 5 to 10 seconds O 2. 10 to 15 seconds O 3. 15 to 20 seconds O Finish Clicking on the Finish button should take the user to a summary page like the following: Question Correct Answer Your Answer 1 3 3 3 2 1 (incorrect) 6 2 3 (ncorrect) Your score: 8/10 Start OverStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started