Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
97 views

Java Lab Manual: Program 1

The document contains code for 6 Java programs covering topics like inheritance, exceptions, interfaces, applets, database connectivity using JDBC, and servlets. Program 1 demonstrates constructors and method overloading. Program 2a shows inheritance where class B inherits from class A. Program 2b uses try, catch, and finally blocks to demonstrate exception handling. Program 3 implements interfaces for shapes. Program 4 creates moving applets. Program 5 inserts data into a database. Program 6 contains code for basic servlets.

Uploaded by

Naseer Syed
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

Java Lab Manual: Program 1

The document contains code for 6 Java programs covering topics like inheritance, exceptions, interfaces, applets, database connectivity using JDBC, and servlets. Program 1 demonstrates constructors and method overloading. Program 2a shows inheritance where class B inherits from class A. Program 2b uses try, catch, and finally blocks to demonstrate exception handling. Program 3 implements interfaces for shapes. Program 4 creates moving applets. Program 5 inserts data into a database. Program 6 contains code for basic servlets.

Uploaded by

Naseer Syed
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

JAVA LAB MANUAL

PROGRAM 1
class mainclass
{
public static void main (String[] args)
{
new myclass();
myclass t= new myclass(10);
t.info();
t.info("overload method");
}
}

class myclass
{
int height;
myclass()
{
System.out.println("empty constructor");
height=0;
}
myclass(int i)
{
System.out.println("parameterized constructor");
height=i;
}
void info()
{
System.out.println(" ");
}
void info(String s)
{
System.out.println(s);
}
}

PROGRAM 2a
class A
{
int i,j;
void showij()
{
System.out.println("i and j=" +i+ " " +j);
}
}
class B extends A
{
int k;
void showk()
{
System.out.println("k:"+k);
}
void sum()
{
System.out.println("i+j+k=" +(i+j+k));
}
}

class SimpleInheritance
{
public static void main(String args[])
{
A superOb=new A();
B subOb=new B();
superOb.i=10;
superOb.j=20;
System.out.println("contents of superob:");
superOb.showij();
System.out.println();
subOb.i=7;
subOb.j=8;
subOb.k=9;
System.out.println("contents of subob:");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("sum of i,j,&k in subOb:");
subOb.sum();
}
}

PROGRAM 2b
class FinallyDemo{
static void procA() {
try {
System.out.println("insidde proc A");
throw new RuntimeException("demo");
}
finally {
System.out.println("procA's finally");
}
}
static void procB() {
try{
System.out.println("inside procB");
return;
}
finally {
System.out.println("procB's finally");
}
}
static void procC() {
try{
System.out.println("inside procC");
return;
}
finally {
System.out.println("procC's finally");
}
}

public static void main(String args[])


{
try{
procA();
}
catch(Exception e){
System.out.println("exception caught");
}
procB();
procC();
}
}

PROGRAM 3a
interface Area
{
final static float pi=3.145F;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return (x*y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{
return (pi*x*x);
}
}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Circle cir = new Circle();
Area area;
area = rect;
System.out.println("Area of rectangle=" +area.compute(10,20));
area =cir;
System.out.println("area of circle =" +area.compute(10,10));
}
}

PROGRAM 3b
class clicker implements Runnable {
int click=0;
Thread t;
private volatile boolean running=true;
public clicker(int p)
{
t=new Thread(this);
t.setPriority(p);
}
public void run() {
while(running)
{
click++;
}
}
public void stop()
{
running=false;
}
public void start()
{
t.start();
}
}
class HiloPri {
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
clicker hi=new clicker(Thread.NORM_PRIORITY+2);
clicker llo=new clicker(Thread.NORM_PRIORITY-2);
llo.start();
hi.start();
try {
Thread.sleep(10000);
}
catch(InterruptedException e) {
System.out.print("main thread interrupted");
}
llo.stop();
hi.stop();
try{
hi.t.join();
llo.t.join();
}

catch(InterruptedException e)
{
System.out.println("interrupted exception largest");
}
System.out.println("low priority thread" +llo.click);
System.out.println("high priority thread" +hi.click);
}
}

PROGRAM 4a
/*<applet code=Applet Banner width=300 height=200>
<applet>*/

import java.awt.*;
import java.applet.*;

public class Applet Banner extends Applet implements Runnable


{
String str;
int x,y;

public void init()


{
str="welcome to NMIT";
x=300;
newThread(this).Start();
}

public void run()


{
try
{
while(true)
{
x=x-10;
if(x<0)
{
x=300;
}
repaint();
system.out.println(x);
Thread.sleep(1000);
}
catch(Exception e){}
}

public void paint(Graphics g)


{
for(int i=0;i<10;i++)
g.drawString(str,x,100);
}
}
}

PROGRAM 4b
import java.awt.*;
import java.applet.*;

/*<applet code="ParamDemo" width=300 height=80>


<paramname=FontName value=Courier>
<paramname=FontSize value=14>
<paramname=Leading value=2>
<paramname=AccountEnable value=True>
</applet>
*/

public class ParamDemo extends Applet{

String fontName;
int fontSize;
float leading;
boolean active;
public void Start()
{
String param;

fontName=getParameter("fontName");
if(fontName==null)
fontName="not found";
param=getParameter("fontSize");
try
{
if(param!=null)
fontSize=Integer.parseInt(param);
else
fontSize=0;
}
catch(NumberFormatException e)
{
fontSize=-1;
}

param=getParameter("leading");
try
{
if(param!=null)
leading=Float.valueOf(param).floatValue();
else
leading=0;
}
catch(NumberFormatException e)
{
leading=-1;
}

param=getParameter("accountEnable");
if(param!=null)
active=Boolean.valueOf(param).booleanValue();
}

public void paint(Graphics g)


{
g.drawString("Fontname="+fontName,0,10);
g.drawString("Fontsize="+fontSize,0,26);
g.drawString("leading="+leading,0,42);
g.drawString("Account Active:"+active,0,58);
}
}
PROGRAM 5
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class StudentDb extends JFrame implements ActionListener
{
JTextField studname,studreg,studcourse;
JLabel name,reg,course;
JFrame frame;
JButton save,exit;
Container contentPane;
StudentDb()
{
contentPane=getContentPane();
contentPane.setLayout(new FlowLayout());
frame=new JFrame("Student form");
name=new JLabel("Name");
reg=new JLabel("Register");
course=new JLabel("Course");
save=new JButton("Save");
exit=new JButton("Exit");
studname=new JTextField(15);
studreg=new JTextField(15);
studcourse=new JTextField(15);
frame.setSize(280,230);
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
Panel p5=new Panel();
p1.add(name);
p1.add(studname);
contentPane.add(p1);
p2.add(reg);
p2.add(studreg);
contentPane.add(p2);
p3.add(course);
p3.add(studcourse);
contentPane.add(p3);
contentPane.add(p5);
p4.add(save);
p4.add(exit);
contentPane.add(p4);
frame.getContentPane().add(contentPane,"Center");
save.addActionListener(this);
exit.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)


{
String str=ae.getActionCommand();
if(str.equals("save"));
{
String name=studname.getText();
String course=studcourse.getText();
String reg=studreg.getText();
System.out.println("save");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:shruti","sa","abc");
Statement st=con.createStatement();
st.executeUpdate("insert into student12 values('"+name+"','"+course+"','"+reg+"')");
System.out.println("row inserted successfully");

}
catch(SQLException e)
{
}
catch(Exception e)
{
}
}
if(str.equals("Exit"));
System.exit(0);
}

public static void main(String args[])


{
StudentDb stu=new StudentDb();
stu.frame.setVisible(true);
stu.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

PROGRAM 6 (program 8 according to the manual)


 SERVLET 1
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class Myservlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws
IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<body bgcolor='skyblue'>");
out.println("<form action='./apple' method='get'>");
out.println("Username:<input type='text' name='username'/><br></br>");
out.println("Password:<input type='password' name='password'/><br></br>");
out.println("<input type='submit' name='Login'/>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}

 SERVLET 2
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class Myservlet1 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name=req.getParameter("username");
String password=req.getParameter("password");
out.println("<html>");
out.println("<body bgcolor='skyblue'>");
out.println("The user name is:\t"+name);
out.println("<br />The password is is:\t"+password);
out.println("</body>");
out.println("</html>");
}
}

 WEB-XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>Myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/temp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>pqr</servlet-name>
<servlet-class>Myservlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pqr</servlet-name>
<url-pattern>/apple</url-pattern>
</servlet-mapping>
</web-app>

PROGRAM 7(program 11 according to the manual)


 HTML
<html>
<body bgcolor='skyblue'>

<form action='../god1.do'>
<input type='text' name='username'></input>
<input type='submit' value='login'></input>
</form>

<form action='../god2.do'>
<input type='text' name='name'></input>
<input type='submit' value='go'></input>
</form>

</body>
</html>

 JSP 1
<html>
<body bgcolor='skyblue'>
<%String strname=request.getParameter("username");%>
<strname%> you are in skyblue..
</body>
</html>

 JSP 2
<html>
<body bgcolor='lime'>
<%String strname=request.getParameter("name");%>
<%=strname%> U are in the Nitte College
</body>
</html>

 JSP3
<html>
<body bgcolor='skyblue'>
ur in nitte college..
</body>
</html>

 WEB-XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"> -->

<web-app>
<servlet>
<servlet-name>god</servlet-name>
<servlet-class>Lab11</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>god</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

 SERVLET
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Lab11 extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
System.out.println("!!!!!!!!!!!!!");
res.setContentType("text/html");
PrintWriter out= res.getWriter();
String s=req.getServletPath();
System.out.println("s is"+s);
if(s.equals("/god1.do"))
{
String strname=req.getParameter("username");
RequestDispatcher rd=req.
getRequestDispatcher("./JSP/11a.jsp");
rd.forward(req,res);
}
else if(s.equals("/god2.do"))
{
String strname1=req.getParameter("name");
RequestDispatcher rd=req.
getRequestDispatcher("./JSP/11b.jsp");
rd.forward(req,res);
}
}
}

PROGRAM 9a(program 13a according to the manual)


 JSP
<html>
<head>
<title>odd &even no</title>
</head>
<body>
<%! int i=0; %>
<p><u>Even&Odd no</u></p>
<table border="5" bgcolor='skyblue'>
<tr>
<th> <u>EVEN</u></th>
<th><u>Odd</u></th>
</tr>
<% for(i=1;i<=20;i++){ %>
<tr>
<td>
<% if(i%2==0){%>
</td>
</tr>
<tr>
<td>
<%=i%>
</td>
<% }else{%>

<td>
<%=i%>
</td>
</tr>
<%} %>
<%}%>

</table>
</body>
</html>

PROGRAM 9b(program 13b according to the manual)


 HTML
<html>
<head>
<title>13b program</title>
</head>
<body bgcolor='skyblue'>
<form action="../JSP/13b.jsp">
Username:<input type='text' name='username'/><br /><br />
Password:<input type='password' name='password'/><br /><br />
<input type='submit' name='Submit'/>
</form>
</body>
</html>

 JSP
<html>
<body>
<% String strname=request.getParameter("username");
String strpassword=request.getParameter("password");

if(strname.equals("Mahesh")&&strpassword.equals("lalana")){%>
<%=strname%>
***Welcome Mahesh Welcome***
<%} else {%>
<%=strname%>
is nota member of RB'sfamily
<% }%>
</body>
</html>

You might also like