Create a website that displays an adding quiz (use random numbers between 0 and
ID: 3574331 • Letter: C
Question
Create a website that displays an adding quiz (use random numbers between 0 and 100 - see the image below). The elapsed time will also displayed. When an answer is submitted, your JS program should check whether the given answer is correct or incorrect, and then a suitable pop-up message- whether the answer is wrong or right- should be displayed. The timer should wait until the user click the okay button. If the answer is correct a new quiz should be displayed, and the timer should be initialized to 0. If the answer is wrong, the original quiz should be displayed and the answer box should be reset. The timed should continue from the previous value.Explanation / Answer
< div id='cssmenu'>
< ul>
< li class=''>< a href="${pageContext.request.contextPath}">< span>Home< /span>< /a>< /li>
< li>< a href="${pageContext.request.contextPath}/login">< span>Login< /span>< /a>< /li>
< li>< a href="${pageContext.request.contextPath}/register">< span>Register< /span>< /a>< /li>
< li class='#'>< a href="#">< span>Submit a Question< /span>< /a>< /li>
< li class='#'>< a href="#">< span>Feedback< /span>< /a>< /li>
< li>< a href="#">< span>Contribute< /span>< /a>< /li>
< li>< a href="#">< span>Contact us< /span>< /a>< /li>
< /ul>
< /div>
< c:if test='${not empty sessionScope.user}'>
< div>
Logged as < a href="#" class="button username">${sessionScope.user}< /a>
< /div>
< div>
< a href="${pageContext.request.contextPath}/logout">Logout< /a>
< /div>
< /c:if>
< div>
< table cellpadding="0" cellspacing="50">
< tr>
< td>< a href="takeExam?test=java">< img height="200" width="200" src="${pageContext.request.contextPath}/images/java.png"/>< /a>< /td>
< td>< a href="takeExam?test=javascript">< img height="200" width="200" src="${pageContext.request.contextPath}/images/javascript.png"/>< /a>< /td>
< td>< a href="takeExam?test=sql">< img height="200" width="200" src="${pageContext.request.contextPath}/images/sql-logo.png"/>< /a>< /td>
< td>< a href="takeExam?test=python">< img height="200" width="200" src="${pageContext.request.contextPath}/images/python.jpg"/>< /a>< /td>
< /tr>
< tr>
< td>< a href="takeExam?test=css">< img height="200" width="200" src="${pageContext.request.contextPath}/images/css.jpg"/>< /a>< /td>
< td>< a href="takeExam?test=php">< img height="200" width="200" src="${pageContext.request.contextPath}/images/php-logo.jpg"/>< /a>< /td>
< td>< a href="takeExam?test=linux">< img height="200" width="200" src="${pageContext.request.contextPath}/images/logo-linux.png"/>< /a>< /td>
< td>< a href="takeExam?test=mongodb">< img height="200" width="200" src="${pageContext.request.contextPath}/images/mongodb_logo.png"/>< /a>< /td>
< /tr>
< /table>
< /div>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String email=request.getParameter("email");
String password=request.getParameter("password");
Connection con=DatabaseConnectionFactory.createConnection();
try
{
Statement st=con.createStatement();
String sql = "INSERT INTO users values ('"+username+"','"+password+"','"+email+"')";
System.out.println(sql);
st.executeUpdate(sql);
}catch(SQLException sqe){System.out.println("Error : While Inserting record in database");}
try
{
con.close();
}catch(SQLException se){System.out.println("Error : While Closing Connection");}
request.setAttribute("newUser",username);
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsps/regSuccess.jsp");
dispatcher.forward(request, response);
}
public class DatabaseConnectionFactory {
private static String dbURL="jdbc:mysql://localhost/quiz";
private static String dbUser="root";
private static String dbPassword="";
public static Connection createConnection()
{
Connection con=null;
try{
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
con = DriverManager.getConnection(dbURL,dbUser,dbPassword);
}
catch(SQLException sqe){ System.out.println("Error: While Creating connection to database");sqe.printStackTrace();}
return con;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
Connection con=DatabaseConnectionFactory.createConnection();
ResultSet set=null;
int i=0;
try
{
Statement st=con.createStatement();
String sql = "Select * from users where username='"+username+"' and password='"+password+"' ";
System.out.println(sql);
set=st.executeQuery(sql);
while(set.next())
{
i=1;
}
if(i!=0)
{ HttpSession session=request.getSession();
session.setAttribute("user",username);
RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/jsps/home.jsp");
rd.forward(request, response);
}
else
{ request.setAttribute("errorMessage","Invalid username or password");
RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/jsps/login.jsp");
rd.forward(request, response);
}
}catch(SQLException sqe){System.out.println("Error : While Fetching records from database");}
try
{
con.close();
}catch(SQLException se){System.out.println("Error : While Closing Connection");}
}
@WebServlet(urlPatterns = { "/login", "/register", "/writeExam", "/logout" })
public class MainController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String applicationContextPath = request.getContextPath();
if (request.getRequestURI().equals(applicationContextPath + "/")) {
RequestDispatcher dispatcher = request
.getRequestDispatcher("/WEB-INF/jsps/home.jsp");
dispatcher.forward(request, response);
} else if (request.getRequestURI().equals(
applicationContextPath + "/login")) {
RequestDispatcher dispatcher = request
.getRequestDispatcher("/WEB-INF/jsps/login.jsp");
dispatcher.forward(request, response);
} else if (request.getRequestURI().equals(
applicationContextPath + "/register")) {
RequestDispatcher dispatcher = request
.getRequestDispatcher("/WEB-INF/jsps/register.jsp");
dispatcher.forward(request, response);
} else if (request.getRequestURI().equals(
applicationContextPath + "/writeExam")) {
request.getSession().setAttribute("currentExam", null);
String exam = request.getParameter("test");
request.getSession().setAttribute("exam", exam);
System.out.println(request.getSession().getAttribute("user"));
if (request.getSession().getAttribute("user") == null) {
request.getRequestDispatcher("/login").forward(request,
response);
} else {
RequestDispatcher dispatcher = request
.getRequestDispatcher("/WEB-INF/jsps/quizDetails.jsp");
dispatcher.forward(request, response);
}
} else if (request.getRequestURI().equals(
applicationContextPath + "/logout")) {
request.getSession().invalidate();
RequestDispatcher dispatcher = request
.getRequestDispatcher("/WEB-INF/jsps/home.jsp");
dispatcher.forward(request, response);
}
}
}
< quiz>
< title>MongoDB Quiz (01/09/2015)< /title>
< questions>
< question>
< quizquestion>MongoDB is a < /quizquestion>
< answer>Relational Database< /answer>
< answer>Object Relational Database< /answer>
< answer>Graph Database< /answer>
< answer>Document Database< /answer>
< correct>3< /correct>
< /question>
< question>
< quizquestion>What is the name of MongoDB server ?< /quizquestion>
< answer>mongoserver< /answer>
< answer>mongod< /answer>
< answer>mongodb< /answer>
< answer>mongo< /answer>
< correct>1< /correct>
< /question>
< question>
< quizquestion>What is the name of MongoDB client ?< /quizquestion>
< answer>mongo< /answer>
< answer>mongod< /answer>
< answer>mongodb< /answer>
< answer>mongo-client< /answer>
< correct>0< /correct>
< /question>
< /questions>
< /quiz>
public class QuizQuestion {
int questionNumber;
String question;
String questionOptions[];
int correctOptionIndex;
public String getQuestion()
{
return question;
}
public int getQuestionNumber()
{
return questionNumber;
}
public void setQuestionNumber(int i)
{
questionNumber=i;
}
public int getCorrectOptionIndex()
{
return correctOptionIndex;
}
public String[] getQuestionOptions()
{
return questionOptions;
}
public void setQuestion(String s)
{
question=s;
}
public void setCorrectOptionIndex(int i)
{
correctOptionIndex=i;
}
public void setQuestionOptions(String[]s)
{
questionOptions=s;
}
}
public class Exam {
Document dom;
public int currentQuestion=0;
public Map selections=new LinkedHashMap();
public ArrayList questionList = new ArrayList(10);
public Exam(String test) throws SAXException,ParserConfigurationException,IOException, URISyntaxException{
dom=CreateDOM.getDOM(test);
}
// code
}
public int calculateResult(Exam exam){
int totalCorrect=0;
Map<Integer,Integer> userSelectionsMap=exam.selections;
List userSelectionsList=new ArrayList(10);
for (Map.Entry<Integer, Integer> entry :userSelectionsMap.entrySet()) {
userSelectionsList.add(entry.getValue());
}
List questionList=exam.questionList;
List correctAnswersList=new ArrayList(10);
for(QuizQuestion question: questionList){
correctAnswersList.add(question.getCorrectOptionIndex());
}
for(int i=0;i<selections.size();i++){
System.out.println(userSelectionsList.get(i)+" --- "+correctAnswersList.get(i));
if((userSelectionsList.get(i)-1)==correctAnswersList.get(i)){
totalCorrect++;
}
}
System.out.println("You Got "+totalCorrect+" Correct");
return totalCorrect;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.