Computer Science, asked by rockingadi3ma3, 1 year ago

Can you please write down a BlueJ program for making an online quiz or a pattern? Please try to provide a little explanation too:)


AionAbhishek: sorry its too big and its taking long time to write it here wait a little

Answers

Answered by AionAbhishek
2
rest part is here 

 ____________________________________________________________

                        qpa[9][3]="int arr[rows][]=new int[rows][cols]";

                        qpa[9][4]="int[][] arr=new int[rows][cols]";

                       

                        //qca stores pairs of question and its correct answer

                        qca=new String[10][2];

                       

                        qca[0][0]="How to run Java program on the command prompt?";

                        qca[0][1]="java JavaProgram";

                        qca[1][0]="What is the use of the println method?";

                        qca[1][1]="It is used to print text on the screen with the line break.";

                        qca[2][0]="How to read a character from the keyboard?";

                        qca[2][1]="char c=(char)System.in.read()";

                        qca[3][0]="Which one is a single-line comment?";

                        qca[3][1]="//...";

 

                        qca[4][0]="How do you declare an integer variable x?";

                        qca[4][1]="int x";

                        qca[5][0]="How do you convert a string of number to a number?";

                        qca[5][1]="int num=Integer.parseInt(str_num)";

                        qca[6][0]="What is the value of x? int x=3>>2";

                        qca[6][1]="0";

                       

                        qca[7][0]="How to do exit a loop?";

                        qca[7][1]="Using break";

                       

                        qca[8][0]="What is the correct way to allocate one-dimensional array?";

                        qca[8][1]="int[] arr=new int[size]";

                        qca[9][0]="What is the correct way to allocate two-dimensional array?";

                        qca[9][1]="int[][] arr=new int[rows][cols]";

                       

                       

                        //create a map object to store pairs of question and selected answer

                        map=new HashMap<Integer, String>();

                       

                        }

        public String getSelection(){

                        String selectedChoice=null;

                        Enumeration<AbstractButton> buttons=bg.getElements(); 

                        while(buttons.hasMoreElements()) 

                        { 

                        JRadioButton temp=(JRadioButton)buttons.nextElement(); 

                        if(temp.isSelected()) 

                                    { 

                                                selectedChoice=temp.getText();

                                    } 

                         }  

                        return(selectedChoice);

            }

 

        public void readqa(int qid){

                        lblmess.setText("  "+qpa[qid][0]);

                        choice1.setText(qpa[qid][1]);

                        choice2.setText(qpa[qid][2]);

                        choice3.setText(qpa[qid][3]);

                        choice4.setText(qpa[qid][4]);

                        choice1.setSelected(true);

            }

       public void reset(){

                        qaid=0;

                        map.clear();

                        readqa(qaid);

                        btnext.setText("Next");

                        }

       public int calCorrectAnswer(){

                        int qnum=10;

                        int count=0;

                        for(int qid=0;qid<qnum;qid++)

                                    if(qca[qid][1].equals(map.get(qid))) count++;

                        return count;

            }

        public class Report extends JFrame{

                        Report(){

                                    setTitle("Answers");

                                    setSize(850,550);

                                    setBackground(Color.WHITE);

                                    addWindowListener(new WindowAdapter(){

                                                            public void windowClosing(WindowEvent e){

                                                                        dispose();

                                                                        reset();

                                                            }

                                                });

                                    Draw d=new Draw();                                  

                                    add(d);

                                    setVisible(true);

                                    }

                       

                                            class Draw extends Canvas{

                                    public void paint(Graphics g){

                                                int qnum=10;

                                                int x=10;

                                                int y=20;

                                                for(int i=0;i<qnum;i++){

                                                            //print the 1st column

                                                            g.setFont(new Font("Arial",Font.BOLD,12));                                         

                                                            g.drawString(i+1+". "+qca[i][0], x,y);

                                                            y+=30;           

                                                            g.setFont(new Font("Arial",Font.PLAIN,12));                             

                                                            g.drawString("      Correct answer:"+qca[i][1], x,y);

                                                            y+=30;

                                                            g.drawString("      Your answer:"+map.get(i), x,y);

                                                            y+=30;

                                                            //print the 2nd column

                                                            if(y>400)

                                                            {y=20;

                                                              x=450;

                                                            }

                                                           

                                                }

                                                //Show number of correct answers

                                                int numc=calCorrectAnswer();

                                                g.setColor(Color.BLUE);

                                                g.setFont(new Font("Arial",Font.BOLD,14));

                                                g.drawString("Number of correct answers:"+numc,300,500);

                       

                                               

                                    }

                        }

                                   

            }

       

}

public class QuizProgram {

public static void main(String args[]){

                         new Quiz();

  

            }

}

____________________________________________________________


Attachments:

AionAbhishek: first see the these 4 pics then see the answer from the above
Answered by Anonymous
1
i think it will help..........

<%@page language="java" import="java.sql.*" %>

<%
if(request.getParameter("submit")!=null)
{
Connection con = null;
String url = "jdbc:mysql://192.168.10.211:3306/";
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName ="amar";
String pass="amar123";

try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,userName,pass);

try{
Statement st = con.createStatement();
String quest = request.getParameter("quest").toString();
String QA = request.getParameter("QA").toString();
String QB = request.getParameter("QB").toString();
String QC = request.getParameter("QC").toString();
String QD = request.getParameter("QD").toString();
String correctAns = request.getParameter("correctAns").toString();
out.println("quest : " + quest);

String qry = "insert into question_deatil(quest,QA,QB,QC,QD,correctAns) values('"+quest+"','"+QA+"','"+QB+"','"+QC+"','"+QD+"','"+correctAns+"')";

out.println("qry : " + qry);

int val = st.executeUpdate(qry);

con.close();
if(val>0)
{
response.sendRedirect("quizeApplication.jsp");
}

}

catch(SQLException ex){
System.out.println("SQL satatment not found");
}
}
catch(Exception e){
e.printStackTrace();
}
}

%>

<html>
<title>Quize application in jsp</title>
<head>

<script>

function validateForm(theForm){

if(theForm.quest.value==""){
//Please enter username
alert("Please enter Question.");
theForm.quest.focus();
return false;
}

return true;

</script>
</head>

<body>
<br>
<br/>
<center>

<table border="1" width="450px" bgcolor="pink" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<form method="POST" action="" onsubmit="return validateForm(this);">

<h2 align="center"><font color="red">Quize Application in JSP</font></h2>
<table border="0" width="400px" cellspacing="2" cellpadding="4">
<tr>
<td width="50%"><b>Enter Question:</b></td>
<td width="50%"><input type="text" name="quest" size="40"/> </td>
</tr>
<tr>
<td width="50%"><b>Enter Answer(A.):</b></td>
<td width="50%"><input type="text" name="QA" size="40"/> </td>
</tr>
<tr>
<td width="50%"><b>Enter Answer(B.):</b></td>
<td width="50%"><input type="text" name="QB" size="40"/> </td>
</tr>

<tr>
<td width="50%"><b>Enter Answer(C.):</b></td>
<td width="50%"><input type="text" name="QC" size="40"/> </td>
</tr>

<tr>
<td width="50%"><b>Enter Answer(D.):</b></td>
<td width="50%"><input type="text" name="QD" size="40"/> </td>
</tr>

<tr>
<td width="50%"><b>Correct Answer:</b></td>
<td width="50%"><input type="text" name="correctAns" size="10"/> </td>
</tr> 

</table>
<center>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</center> 
</form>
</td>
</tr>
</table>
</center>
</body>
</html>


Step 2 : Create "quizeApplication.jsp" for Process the Data and retrieve the data from database and show the all data on browser.

Here is the code:

<%@ page import="java.sql.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% 
String ans=" ";
if(request.getParameter("correctAns")!=null)
{
ans=request.getParameter("correctAns").toString();
}
Connection conn = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.10.211:3306/";
String db = "amar";
String user = "amar";
String pass = "amar123";
Statement st = null;
ResultSet qrst;
ResultSet rs = null;

String id=request.getParameter("id");
System.out.println("id:"+id);

int i=1;

String s,g;

int count=0;

try {

Class.forName(driver);
conn = DriverManager.getConnection(url+db,user,pass);

st = conn.createStatement();
rs = st.executeQuery("select * from question_deatil");
while(rs.next()) {
%>
<br>
<br/>
<center>

<table border="1" width="500px" bgcolor="pink" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">

<form name="form1">

<h2 align="center"><font color="red">Online Quize Application</font></h2>

<b>Select Correct Answer</b>
<table border="0" width="500px" cellspacing="2" cellpadding="4">
<tr>

<td width="50%"> Question:</td>
<input type="hidden" name="correctAns" value="<%=rs.getString(7)%>" />
<tr>
<td><%= rs.getString("quest") %></td></tr>
<tr>
<td>

1: <input type="radio" name="a" value= "QA" /></td>
<td><%= rs.getString("QA") %></td></tr> 
<tr>
<td>
2: <input type="radio" name="a" value="QB" /></td>
<td><%= rs.getString("QB") %></td></tr>

<tr>
<td>
3: <input type="radio" name="a" value="QC" /></td>
<td><%= rs.getString("QC") %> </td></tr>

<tr>
<td>
4: <input type="radio" name="a" value="QD" /> </td>
<td> <%= rs.getString("QD") %> </td></tr>

<tr>
<td>
<center>
<input type="submit" value="Submit" name="submit"></center></td></tr>
</table>

</form>
</td>
</tr>
</table>
</center>
</body>
<% g=request.getParameter("a");
%>
<% 
if(g.equals(ans)){

count++;
out.println("Correct");

}
else
out.println("false");
%>

<%
}}

catch (Exception ex) {
ex.printStackTrace();

%>

<%
} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}
out.println("Score="+count);
%>
</html>
Similar questions