[Go to site: main page, start]

0% found this document useful (0 votes)
10 views14 pages

Java Lab Manual: Programming Exercises

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Java Lab Manual: Programming Exercises

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

JAVA LAB MANUAL

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

class myclass
{
int height;
myclass()
{
[Link]("empty constructor");
height=0;
}
myclass(int i)
{
[Link]("parameterized constructor");
height=i;
}
void info()
{
[Link](" ");
}
void info(String s)
{
[Link](s);
}
}

PROGRAM 2a
class A
{
int i,j;
void showij()
{
[Link]("i and j=" +i+ " " +j);
}
}
class B extends A
{
int k;
void showk()
{
[Link]("k:"+k);
}
void sum()
{
[Link]("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;
[Link]("contents of superob:");
[Link]();
[Link]();
subOb.i=7;
subOb.j=8;
subOb.k=9;
[Link]("contents of subob:");
[Link]();
[Link]();
[Link]();
[Link]("sum of i,j,&k in subOb:");
[Link]();
}
}

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

public static void main(String args[])


{
try{
procA();
}
catch(Exception e){
[Link]("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;
[Link]("Area of rectangle=" +[Link](10,20));
area =cir;
[Link]("area of circle =" +[Link](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);
[Link](p);
}
public void run() {
while(running)
{
click++;
}
}
public void stop()
{
running=false;
}
public void start()
{
[Link]();
}
}
class HiloPri {
public static void main(String args[])
{
[Link]().setPriority(Thread.MAX_PRIORITY);
clicker hi=new clicker(Thread.NORM_PRIORITY+2);
clicker llo=new clicker(Thread.NORM_PRIORITY-2);
[Link]();
[Link]();
try {
[Link](10000);
}
catch(InterruptedException e) {
[Link]("main thread interrupted");
}
[Link]();
[Link]();
try{
[Link]();
[Link]();
}

catch(InterruptedException e)
{
[Link]("interrupted exception largest");
}
[Link]("low priority thread" +[Link]);
[Link]("high priority thread" +[Link]);
}
}

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

import [Link].*;
import [Link].*;

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();
[Link](x);
[Link](1000);
}
catch(Exception e){}
}

public void paint(Graphics g)


{
for(int i=0;i<10;i++)
[Link](str,x,100);
}
}
}

PROGRAM 4b
import [Link].*;
import [Link].*;

/*<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=[Link](param);
else
fontSize=0;
}
catch(NumberFormatException e)
{
fontSize=-1;
}

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

param=getParameter("accountEnable");
if(param!=null)
active=[Link](param).booleanValue();
}

public void paint(Graphics g)


{
[Link]("Fontname="+fontName,0,10);
[Link]("Fontsize="+fontSize,0,26);
[Link]("leading="+leading,0,42);
[Link]("Account Active:"+active,0,58);
}
}
PROGRAM 5
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class StudentDb extends JFrame implements ActionListener
{
JTextField studname,studreg,studcourse;
JLabel name,reg,course;
JFrame frame;
JButton save,exit;
Container contentPane;
StudentDb()
{
contentPane=getContentPane();
[Link](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);
[Link](280,230);
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
Panel p5=new Panel();
[Link](name);
[Link](studname);
[Link](p1);
[Link](reg);
[Link](studreg);
[Link](p2);
[Link](course);
[Link](studcourse);
[Link](p3);
[Link](p5);
[Link](save);
[Link](exit);
[Link](p4);
[Link]().add(contentPane,"Center");
[Link](this);
[Link](this);
}

public void actionPerformed(ActionEvent ae)


{
String str=[Link]();
if([Link]("save"));
{
String name=[Link]();
String course=[Link]();
String reg=[Link]();
[Link]("save");
try
{
[Link]("[Link]");
Connection con=[Link]("jdbc:odbc:shruti","sa","abc");
Statement st=[Link]();
[Link]("insert into student12 values('"+name+"','"+course+"','"+reg+"')");
[Link]("row inserted successfully");

}
catch(SQLException e)
{
}
catch(Exception e)
{
}
}
if([Link]("Exit"));
[Link](0);
}

public static void main(String args[])


{
StudentDb stu=new StudentDb();
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

PROGRAM 6 (program 8 according to the manual)


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

 SERVLET 2
import [Link].*;
import [Link].*;
import [Link].*;
public class Myservlet1 extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
IOException,ServletException
{
[Link]("text/html");
PrintWriter out=[Link]();
String name=[Link]("username");
String password=[Link]("password");
[Link]("<html>");
[Link]("<body bgcolor='skyblue'>");
[Link]("The user name is:\t"+name);
[Link]("<br />The password is is:\t"+password);
[Link]("</body>");
[Link]("</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='../[Link]'>
<input type='text' name='username'></input>
<input type='submit' value='login'></input>
</form>

<form action='../[Link]'>
<input type='text' name='name'></input>
<input type='submit' value='go'></input>
</form>

</body>
</html>

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

 JSP 2
<html>
<body bgcolor='lime'>
<%String strname=[Link]("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"
"[Link] -->

<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 [Link].*;
import [Link].*;
import [Link].*;

public class Lab11 extends HttpServlet


{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
[Link]("!!!!!!!!!!!!!");
[Link]("text/html");
PrintWriter out= [Link]();
String s=[Link]();
[Link]("s is"+s);
if([Link]("/[Link]"))
{
String strname=[Link]("username");
RequestDispatcher rd=req.
getRequestDispatcher("./JSP/[Link]");
[Link](req,res);
}
else if([Link]("/[Link]"))
{
String strname1=[Link]("name");
RequestDispatcher rd=req.
getRequestDispatcher("./JSP/[Link]");
[Link](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/[Link]">
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=[Link]("username");
String strpassword=[Link]("password");

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

You might also like