slip no 4
que 1
import java.awt.;
import java.awt.event.;
class Slip8_1 extends Frame implements Runnable
{
Thread t;
Label l1;
int f;
Slip8_1()
{
t=new Thread(this);
t.start();
setLayout(null);
l1=new Label("Hello JAVA");
l1.setBounds(100,100,100,40);
add(l1);
setSize(300,300);
setVisible(true);
f=0;
}
public void run()
{
try
{
if(f==0)
{
t.sleep(200);
l1.setText("");
f=1;
}
if(f==1)
{
t.sleep(200);
l1.setText("Hello Java");
f=0;
}
}
catch(Exception e)
{
System.out.println(e);
}
run();
}
public static void main(String a[])
{
new Slip8_1();
}
}
Q2)
Write a Java program to store city names
and their STD codes using an appropriate
collection and perform following operations:
i. Add a new city and its code (No duplicates)
ii. Remove a city from the collection iii.
Search for a city name and display the code
import java.awt.;
import java.awt.event.;
import javax.swing.;
import java.util.;
class Slip16_2 extends JFrame implements ActionListener
{
JTextField t1,t2,t3;
JButton b1,b2,b3;
JTextArea t;
JPanel p1,p2;
Hashtable ts;
Slip16_2()
{
ts=new Hashtable();
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
b1=new JButton("Add");
b2=new JButton("Search");
b3=new JButton("Remove");
t=new JTextArea(20,20);
p1=new JPanel();
p1.add(t);
p2= new JPanel();
p2.setLayout(new GridLayout(2,3));
p2.add(t1);
p2.add(t2);
p2.add(b1);
p2.add(t3);
p2.add(b2);
p2.add(b3);
add(p1);
add(p2);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(b1==e.getSource())
{
String name = t1.getText();
int code = Integer.parseInt(t2.getText());
ts.put(name,code);
Enumeration k=ts.keys();
Enumeration v=ts.elements();
String msg="";
while(k.hasMoreElements())
{
msg=msg+k.nextElement()+" = "+v.nextElement()+"\n";
}
t.setText(msg);
t1.setText("");
t2.setText("");
}
else if(b2==e.getSource())
{
String name = t3.getText();
if(ts.containsKey(name))
{
t.setText(ts.get(name).toString());
}
else
JOptionPane.showMessageDialog(null,"City not
found ...");
}
else if(b3==e.getSource())
{
String name = t3.getText();
if(ts.containsKey(name))
{
ts.remove(name);
JOptionPane.showMessageDialog(null,"City
Deleted ...");
}
else
JOptionPane.showMessageDialog(null,"City not
found ...");
}
}
public static void main(String a[])
{
new Slip16_2();
}
}
Slip Nos-5
- Write a Java Program to create the hash
table that will maintain the mobile number
and student name. Display the details of
student using Enumeration interface
import java.io.;
import java.util.;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty hashtable
Hashtable<String, String> ht
= new Hashtable<String,
String>();
// Inserting key-value pairs into hash
table
// using put() method
ht.put("Name", "Rohan");
ht.put("Mpbile_Nos", "8446049402");
// Now creating an Enumeration
object
// to store keys
Enumeration<String> e = ht.keys();
// Condition holds true till there is
// single key remaining
while (e.hasMoreElements()) {
// Getting key
String key = e.nextElement();
System.out.println(key + ":" +
ht.get(key));
}
}
}
Q2) Create a JSP page for an online multiple choice
test. The questions are randomly selected from a
database and displayed on the screen. The choices
are displayed using radio buttons. When the user
clicks on next, the next question is displayed. When
the user clicks on submit, display the total score on
the screen
Exam.jsp
<%@page import="java.sql.,java.util."%>
<%
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(
"jdbc:postgresql:ty1","postgres","");
Set s = new TreeSet();
while(true){
int n = (int)(Math.random()*11+1);
s.add(n);
if(s.size()==5) break;
}
PreparedStatement ps = con.prepareStatement("select * from
questions where qid=?");
%>
<form method='post' action='accept_ans.jsp'>
<table width='70%' align='center'>
<%
int i=0;
Vector v = new Vector(s);
session.setAttribute("qids",v);
int qid = Integer.parseInt(v.get(i).toString());
ps.setInt(1,qid);
ResultSet rs = ps.executeQuery();
rs.next();
%>
<tr>
<td><b>Question:<%=i+1%></b></td>
</tr>
<tr>
<td><pre><b><%=rs.getString(2)%></pre></b></td>
</tr>
<tr>
<td>
<b>
<input type='radio' name='op' value=1><
%=rs.getString(3)%><br>
<input type='radio' name='op' value=2><
%=rs.getString(4)%><br>
<input type='radio' name='op' value=3><
%=rs.getString(5)%><br>
<input type='radio' name='op' value=4><
%=rs.getString(6)%><br><br>
</b>
</td>
</tr>
<tr>
<td align='center'>
<input type='submit' value='Next' name='ok'>
<input type='submit' value='Submit' name='ok'>
</td>
</tr>
</table>
<input type='hidden' name='qno' value=<%=qid%>>
<input type='hidden' name='qid' value=<%=i+1%>>
</form>
</body>
Acceptans.jsp
<%@page import="java.sql.*,java.util.*"%>
<%
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(
"jdbc:postgresql:ty1","postgres","");
Vector answers = (Vector)session.getAttribute("answers");
if(answers==null)
answers = new Vector();
int qno = Integer.parseInt(request.getParameter("qno"));
int ans = Integer.parseInt(request.getParameter("op"));
int i = Integer.parseInt(request.getParameter("qid"));
answers.add(qno+" "+ans);
session.setAttribute("answers",answers);
String ok = request.getParameter("ok");
if(ok.equals("Submit") || i==5){
response.sendRedirect("result.jsp");
return;
}
PreparedStatement ps = con.prepareStatement("select * from
questions where qid=?");
%>
<form method='post' action='accept_ans.jsp'>
<table width='70%' align='center'>
<%
Vector v = (Vector)session.getAttribute("qids");
int qid = Integer.parseInt(v.get(i).toString());
ps.setInt(1,qid);
ResultSet rs = ps.executeQuery();
rs.next();
%>
<tr>
<td><b>Question:<%=i+1%></b></td>
</tr>
<tr>
<td><pre><b><%=rs.getString(2)%></pre></b></td>
</tr>
<tr>
<td>
<b>
<input type='radio' name='op' value=1><%=rs.getString(3)%><br>
<input type='radio' name='op' value=2><%=rs.getString(4)%><br>
<input type='radio' name='op' value=3><%=rs.getString(5)%><br>
<input type='radio' name='op' value=4><
%=rs.getString(6)%><br><br>
</b>
</td>
</tr>
<tr>
<td align='center'>
<input type='submit' value='Next' name='ok'>
<input type='submit' value='Submit' name='ok'>
</td>
</tr>
</table>
<input type='hidden' name='qno' value=<%=qid%>>
<input type='hidden' name='qid' value=<%=i+1%>>
</form>
</body>
Result.jsp
<%@page import="java.sql.*,java.util.*,java.text.*"%>
<%
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(
"jdbc:postgresql:ty1","postgres","");
Vector v = (Vector)session.getAttribute("answers");
if(v==null){
%>
<h1>No questions answered</h1>
<%
return;
}
PreparedStatement ps = con.prepareStatement("select ans from
questions where qid=?");
int tot=0;
for(int i=0;i<v.size();i++){
String str = v.get(i).toString();
int j = str.indexOf(' ');
int qno = Integer.parseInt(str.substring(0,j));
int gans = Integer.parseInt(str.substring(j+1));
ps.setInt(1,qno);
ResultSet rs = ps.executeQuery();
rs.next();
int cans = rs.getInt(1);
if(gans==cans) tot++;
}
session.removeAttribute("qids");
session.removeAttribute("answers");
session.removeAttribute("qid");
%>
<h3>Score:<%=tot%></h1>
<center><a href='exam.jsp'>Restart</a></center>
</body>
SQL File
create table questions(qid serial primary key, question text,
option1 text, option2 text, option3 text, option4 text, ans int);
insert into questions
(question,option1,option2,option3,option4,ans)
values
('Who is prime minister of India?','Rahul Gandhi','Narendra
Modi','Sonia Gandhi','Manmohan Singh',2),
('Who is finance minister of India','Rahul Gandhi','P
Chidambaram','Manmohan Singh','Arun Jately',4),
('What is square root of 16?','2','4','1','256',4),
('Who is chief minister of Maharashtra','Uddhav
Tharakey','Devendra Fadanavis','Raj Thakarey','Sharad Pawar',2),
('What is full for of LIFO?','Last In First Out','Late In First
Out','Long In First Out','Large In First Out',1),
('Which is capital of
India','Delhi','Maharashtra','Kolkata','Goa',1), ('What is
currency of India','Dollar','Rupee','Pound','Yen',2),
('Who Invented C?','Kim Thompson','Bill Joy','Dennis
Ritche','Balaguru Swamy',3),
('Where was Java invented?','Microsoft','Oracle','Sun
Microsystem','Intel',3),
('What is cube root of 8?','2','3','4','5',1),('What is full form
of FIFO','Fast In Fast Out','First in First Out','Fast In First
Out','First In Fast Out',2);