Java 2 old Papers Solution 2018 || BIM STUDY NOTES

5th Semester

year 2018





 

  1. Create a HTML document that contains header information of a page and include this HTML as a header file in header.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>Orchid International College</h1>
<h3>Gaushala, Kathmandu</h3>
</center>
</body>
</html>

include.jsp
<html>
<body>
<%@include file = “header.html” %>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

</p></body>
</html>


3.  Create a servlet to find out factorial of a positive integer value.

import javax.servlet.http.*;
import java.io.*;
public class Ques3_2018 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) { response.setContentType("text/html");
int num = Integer.parseInt(request.getParameter("number"));
 int fact = 1;
while(num > 0) {
     for(int i = 1; i <= num; i++) {
      fact = fact * i;
   }
 }
try {
PrintWriter pw = response.getWriter();
pw.println("<html><body>");
pw.println("<h1>Factorial of "+num+ " is "+fact+"</h1>");
pw.println("</html></body>");
}catch(Exception ex) {
  System.out.println(ex);
  }
  }
}
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action = "ques2_2018" method = "get">
    <label>Enter a Number:</label>
 <input type = "text" name = "number"/>
  <input type = "submit" value = "Submit"/>
</form>
</body>
</html>
Q.no 4  Write a program to insert 5 records into the table “Book” that contains (id, title, author, price) inside the database “Library”.



import java.sql.*;
public class T2018_4 {
private static Connection con;
private static Statement s;
public static void main(String[] args) throws Exception{
insertRecords();
}
public static void insertRecords() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/Library";
con=DriverManager.getConnection(url,"root","");
if(con!=null){
s=con.createStatement();
String sql="INSERT INTO book (id,title,author.price) VALUES "
+ "(25,'AbcBook','AbcAuthor',2500),"
+ "(47,'MnoBook','MnoAuthor',800),"
+ "(33,'PqrBook','PqrAuthor',625),"
+ "(49,'XyzBook','XyzAuthor',1200),"
+ "(11,'StuBook','StuAuthor',1200) ";
int res=s.executeUpdate(sql);
if(res!=-1){
System.out.println("Successfully inserted in table BOOK");
}
else{
System.out.println("Could not insert into table");
}
}
else{
System.out.println("Error in connectiong to database!");
    }
 }
}

5.Create an applet that reads the parameter supplied from the <param> tag and display the parameter value. Also create a suitable HTML file.





import java.applet.*;
import java.awt.*;
import java.applet.*;
/*
<applet code="T2018_5.class" height="300" width="500">
<param name="name" value="Laxmi" />
<param name="age" value="20" />
</applet>
*/
public class T2018_5 extends Applet
{
String n;
String a;
public void init(){
n=getParameter("name");
a=getParameter("age");
}
public void paint(Graphics g){
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 40);
}
}

Q.no 6   Create a swing GUI that contains a text field and two buttons (ok and clear). When ok button is clicked, “welcome” should be displayed in the text field and clear button should clear the text field content.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ques6_2018 implements ActionListener {
JFrame f1;
JTextField t1;
JButton b1, b2;
Ques6_2018() {
f1 = new JFrame(); f1.setSize(300, 300);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setLayout(new FlowLayout());
t1 = new JTextField(10);
b1 = new JButton("Ok");
b2 = new JButton("Clear");f1.add(t1); f1.add(b1);
f1.add(b2);b1.addActionListener(this);
b2.addActionListener(this); f1.setVisible(true);
if(e.getSource() == b1) {
t1.setText("Welcome");}
else if(e.getSource() == b2)
 {
t1.setText(""); } }}

 

Leave a Reply

Your email address will not be published.