Вы находитесь на странице: 1из 24

LAB MANUAL

IT 361 Internet Programming

For III/IV B.Tech


Information Technology
II Semester

Department of Information Technology


VELAGAPUDI RAMAKRISHNA SIDDHARTHA ENGINEERING COLLEGE
VIJAYAWADA-520 007(A.P)

1
INSTRUCTIONS TO BE FOLLOWED IN MAINTAINING THE RECORD BOOK

The Record should be written neatly with ink on the right hand page only. The left hand page being reserved
for diagrams.

The Record should contain:

1. The date
2. The number and name of the experiment
3. The aim of the experiment
4. Algorithm
5. Program
6. On the left hand side, Flow charts should be designed
7. On the left hand side, Input & Output should be mentioned.
8. Index must be filled in regularly.
9. You must get your record certified by the concerned staff on the very next class after completing the
experiment
10. You must get your record certified by the concerned staff at the end of every semester.

INSTRUCTIONS TO BE FOLLOWED IN THE LABORATORY

1. You must bring record observations notebook, while coming to the practical class without you may not be
allowed to the practical.

2. Don’t touch the equipment which is not connected with our experiment.

3. When the system /apparatus is issued, you are advised to check their conditions.

4. You should not leave the laboratory without obtaining the signature of the concerned lecturer after completing
the practical

Note:

1. Damaged caused to the property of the laboratory will be recovered


2. If 75 % of the experiments prescribed are not completed the candidate will not be allowed for attending
examinations.

2
INDEX

S.NO NAME OF THE EXPERIMENT PAGE.NO


LAB CYCLE1
1 Program for Over Loading and Over Riding methods in 4-5
Java.
2 Implementation of various Inheritance Forms: 5-8

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchial Inheritance
3 Implementing Multiple Inheritance by means of 9-10
Interfaces.
4 String Manipulation : 10-11
1. Alphabetical Ordering of Strings.
2. Frequency count of Words and
Characters in a Text.

5 Program to create the Packages in Java. 11-12


6 Program to create Multiple Threads in Java. 13-14

LAB CYCLE2
7 Program to write applets to draw various shapes 14-16
square inside a circle
circle inside a square
program to draw a polygon
program to draw a cylinder
program to draw a cube
8 Write a program for handling Mouse Events. 16-19
Mouse entered, Mouse pressed, Mouse released
key event handle.

9 Program to manipulate lists 20-23


i) lists
ii) text field
iii) scroll bars
iv) text area
v) menus
10 Write a program to select the values from the department 23-24
table.
11 Write a program to insert the values into student table 24
using JDBC
3
1. Over Loading and Over Riding

Aim: Program to overloading and over riding methods in java.

Program:
Program for method over loading
class A
{
int a,b,c;
void accept(int x)
{
a=x;
System.out.println("a is"+a);
}
void accept(int x,int y)
{
b=x;
c=y;

System.out.println("b is"+b);

System.out.println("c is"+c);
}
}
class ma
{
public static void main(String args[])
{
accept aa=new accept(10,20);
accept aa=new accept(30);
}
}
Program for method over riding
class A

{
int a;
A(int x)
{
a=x;
}
void display()
{

4
System.out.println("a is "+a);
}
}

class B extends a
{
int b,c;
B(int x,int y)
{
b=x;c=y;
}
void display()
{

System.out.println("b is "+b);
System.out.println("c is "+c);
}

class mr
{
public static void main(string args[])
{
A a=new A(10);
B b=new B(20,30);
}

2. Implementation of various Inheritance Forms:

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance

Aim: Program to implement single inheritance, multilevel inheritance, hierarchical inheritance.

Program:
Program for single inheritance
class A
{
int a,b;

void acc(int x,int y)


{
a=x;
5
b=y;
}

void display()
{
System.out.println("a is"+a);
System.out.println("b is"+b);
}
}

class B extends A
{
int c;

void accept(int z)
{
c=z;
}

void dis()
{
System.out.println("c is"+c);
}
}

class Inher
{
public static void main(String args[])
{
B bb=new B();
bb.acc(10,20);
bb.accept(30);
bb.display();
bb.dis();
}
}

Program for Multilevel inheritance


class A
{
int a,b;
void accept(int x,int y)
{
a=x;
b=y;
}
void display()
{
System.out.println("a is:"+a);
System.out.println("b is:"+b);

6
}
}
class B extends A
{
int c;
void acc(int z)
{
c=z;
}
void show()
{
System.out.println("c is:"+c);
}
}
class C extends B
{
int d;
void gets(int w)
{
d=w;
}
void show()
{
System.out.println("d is:"+d);
}
}
class MuInh
{
public static void main(String args[])
{
C cc=new C();
cc.accept(10,20);
cc.acc(30);
cc.gets(40);
cc.show();
}
}

Program for Hierarchical inheritance


import java.io.*;

class stud
{
String name;
int num;

void getdata() throws IOException


{
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter your name");
name=in.readLine();

7
System.out.println("Enter your number:");
num=Integer.parseInt(in.readLine());
}

void display()
{
System.out.println("Name:"+name);
System.out.println("number:"+num);
}
}

class stud1 extends stud


{
String branch;

void getdata1() throws IOException


{
DataInputStream ge=new DataInputStream(System.in);
System.out.println("Enter you branch:");
branch=ge.readLine();
}

void display()
{
System.out.println("\n");
System.out.println("branch"+branch);
}
}

class stud2 extends stud1


{
int total;

void getdata2() throws IOException


{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter your marks out of 100");
total=Integer.parseInt(in.readLine());
}

void display()
{
System.out.println("Name"+name+"number"+num+"total"+total);
}

class stud3 extends stud2


{
float percent;

8
void getdata3() throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter your percent");
percent=Float.parseFloat(in.readLine());
}

void display()
{
System.out.println("name"+name+"number"+num+"total"+total+"percent"+percent);
}
}

class hirac
{
public static void main(String args[])throws IOException
{
stud st=new stud();
stud1 st1=new stud1();
stud2 st2=new stud2();
stud3 st3=new stud3();
st.getdata();
st1.getdata1();
st2.getdata2();
st3.getdata3();
st.display();
st1.display();
st2.display();
st3.display();
}
}

3. Multiple Inheritance by means of Interfaces.

Aim: Program to implement hybrid inheritance by means of interfaces.

Program:

class student
{
int rollnumber;
void getNumber(int n)
{
rollnumber=n;
}
void putNumber()
{

9
System.out.println("Rollno:"+rollnumber);
}
}

class Test extends student


{
float part1,part2;
void getMarks(float m1,float m2)
{
part1=m1;
part2=m2;
}
void putMarks()
{
System.out.println("Marks Obtained:");
System.out.println("part 1=" +part1);
System.out.println("part 2=" +part2);
}
}

interface sports
{
float sportwt=6.0F;
void putwt();
}

class Results extends Test implements sports


{
float total;
public void putwt()
{
System.out.println("Sports Wt=" +sportwt);
}
void display()
{
total=part1+part2+sportwt;
putNumber();
putMarks();
putwt();
System.out.println("totalscore=" +total);
}
}

class multiple
{
public static void main(String args[])
{
Results res=new Results();
res.getNumber(1234);
res.getMarks(27.5F,33.0F);
res.display();

10
}
}

4. String Manipulation:
1. Alphabetical Ordering of Strings.
2. Frequency count of Words and Characters in a Text.

Aim: Program to implement alphabetical ordering of strings, Frequency count of words and characters in a text.

Program:
Program to implement alphabetical ordering of strings
class Stringordering
{
static String name[]={"nodes","delhi","ahmedabad","calcutta","bombay"};
public static void main(String args[])
{
int size=name.length;
String temp=null;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(name[j].compareTo(name[i])<0)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
for(int i=0;i<size;i++)
{
System.out.println(name[i]);
}
}
}

Program to implement frequency count of words and characters in a text


import java.io.*;

class str_manip
{
public static void main(String args[])throws IOException
{
int j=1;
11
StringBuffer str=new StringBuffer("madhuri nallamothu");
System.out.println("original string :"+str);
System.out.println("length of string :"+str.length());

for(int i=0;i<str.length();i++)
{
if(str.charAt(i)==' ')
j++;
}
System.out.println("No.of.words :"+j);
}
}

5. Packages.

Aim:
Program to create the Packages in Java.

Program:

package MyPack;

class account
{
String name;
double bal;

account(String n,double b)
{
name=n;
bal=b;
}

void display()
{
System.out.println("Name\t"+name+"\tin balance\t"+bal);
}
}

class pack
{
public static void main(String args[])
{
account ob[]=new account[3];
ob[0]=new account("madhuri",130.00);
ob[1]=new account("madhu",145.56);
ob[2]=new account("madhuri.n",1200.65);

12
for(int i=0;i<3;i++)
{
ob[i].display();
}
}
}

6. Multiple Threads in Java


Aim: A Program to create multiple threads in java.

Program:
class A extends Thread
{
Thread t=new Thread();

public void run()


{
for(int i=1;i<5;i++)
{
if(i==1)
t.yield();
System.out.println("it from thread A:I="+i);
}
System.out.println("Exit from A");
}
}

class B extends Thread


{
Thread t=new Thread();

public void run()


{
for(int j=1;j<=5;j++)
{
System.out.println("it from thread B:j="+j);
if(j==3)
t.stop();
}
System.out.println("Exit from B");
}
}

class C extends Thread


{
Thread t=new Thread();

13
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("it from thread C:k="+k);
if(k==3)
try
{
t.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("Exit from C");
}
}

class thread
{
public static void main(String args[])
{
A ThreadA=new A();
B ThreadB=new B();
C ThreadC=new C();

System.out.println("start thread A");


ThreadA.start();
System.out.println("start thread B");
ThreadB.start();
System.out.println("start thread C");
ThreadC.start();
System.out.println("End of main thread");
}
}

7. Program to write applets to draw various shapes


Square inside a circle
Circle inside a square
Program to draw a polygon
Program to draw a cylinder
Program to draw a cube

Aim: . Program to write applets to draw various shapes-Square inside a circle, Circle inside a square, draw a
polygon, draw a cylinder, Program to draw a cube

14
Program
Program to write applets to draw Square inside a circle
import java.awt.*;
import java.applet.*;

/*<applet code= "LineRect"


width=300
height=200>
</applet>*/
public class LineRect extends Applet
{
public void paint(Graphics g)
{
g.drawOval(10,10,60,60);
g.drawRect(20,20,30,30);
//g.fillRect(30,30,40,40);
}
}
Program to write applets to draw Circle inside a square
import java.awt.*;
import java.applet.*;

/*<applet code="linerect1"
width=300
height=200>
</applet>*/
public class linerect1 extends Applet
{
public void paint(Graphics g)
{
g.drawOval(20,20,30,30);
g.drawRect(10,10,60,60);

}
}
Program to write applets to draw polygon
import java.awt.*;
import java.applet.*;

/*<applet code="HouseGlass"
width=230
height=210>
</applet>*/
public class HouseGlass extends Applet
{
public void paint(Graphics g)
{
int xpoint[] = {25,10,15,35,40};
int Ypoint[] = {10,30,50,50,30};
int num = 5;
g.drawPolygon(xpoint,Ypoint,num);
15
}
}
Program to write applets to draw cylinder
import java.awt.*;
import java.applet.*;
/*<applet code="Linep" width=200
height=200>
</applet>*/

public class Linep extends Applet


{
public void paint(Graphics g)
{
g.drawLine(30,30,30,10);
g.drawLine(10,10,10,30);
g.drawOval(10,10,20,05);
g.drawArc(10,25,20,10,180,180);
}
}
Program to write applets to draw cube
import java.awt.*;
import java.applet.*;

/*<applet code="Cube" width=300


height=200>
</applet>*/
public class Cube extends Applet
{
public void paint(Graphics g)
{
g.drawRect(20,20,30,30);
g.drawRect(30,10,30,30);
g.setColor(Color.red);
g.fillRect(20,20,30,30);
g.setColor(Color.blue);
g.fillRect(30,10,30,30);
g.drawLine(20,20,30,10);
g.drawLine(50,20,60,10);
g.drawLine(50,50,60,40);
}
}

8. Handling Mouse Events and key events

Aim: A program to handle mouse events – mouse entered, mouse pressed, mouse released and key event handle

16
Program:
Program to handle mouse events – mouse entered, mouse pressed, mouse released
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MouseEvents" width=300 height=100>
</applet>
*/
public class MouseEvents extends Applet implements MouseListener, MouseMotionListener
{
String msg = " ";
int mouseX = 0, mouseY = 0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}

public void mouseClicked(MouseEvent me)


{

mouseX = 0;
mouseY = 10;
msg = "Mouse clicked.";
repaint();

public void mouseEntered(MouseEvent me)


{

mouseX = 0;
mouseY = 10;
msg = "Mouse entered.";
repaint();

public void mouseExited(MouseEvent me)


{

mouseX = 0;
mouseY = 10;
msg = "Mouse exited.";
repaint();
}
public void mousePressed(MouseEvent me)
{

mouseX = me.getX();
17
mouseY = me.getY();
msg = "Down";
repaint();

public void mouseReleased(MouseEvent me)


{

mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();

public void mouseDragged(MouseEvent me)


{
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
repaint();
}

public void mouseMoved(MouseEvent me)


{

showStatus("Moving mouse at " + me.getX() + ", " + me.getY());


}

public void paint(Graphics g)


{
g.drawString(msg, mouseX, mouseY);
}

}
Program to handle key events
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="SimpleKey" width=300 height=100>
</applet>
*/
public class SimpleKey extends Applet implements KeyListener
{
String msg = "";
int X = 10, Y = 20;

public void init()

18
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
msg+=ke.getKeyChar();
repaint();
}

public void paint(Graphics g)


{
g.drawString(msg, X, Y);
}

}
9. Program to manipulate lists
i) Lists
ii) Text field
iii) Scroll bars
iv)Text area
v) Menus

Aim: program to manipulate lists-Lists, Text field, Scroll bars, Text area, Menus

Program:
Program to manipulate Lists
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/* <applet code="ListDemo" width=200 height=200>


</applet>
*/
public class ListDemo extends Applet implements ActionListener
{
List L1,L2;
Button b;
public void init()
{
L1=new List();
19
L1.add("CSIT");
L1.add("CSE");
L1.add("ECE");
add(L1);
b=new Button("click");
b.addActionListener(this);
add(b);
L2=new List();
add(L2);
}

public void actionPerformed(ActionEvent ae)


{
String st;
st=L1.getSelectedItem();
L2.addItem(st);
}
}

Program to manipulate Text field


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="TextFieldDemo" width=380 height=150>
</applet>
*/
public class TextFieldDemo extends Applet implements ActionListener
{
TextField name, pass;
public void init()
{
Label namep = new Label("Name: ", Label.RIGHT);
Label passp = new Label("Password: ", Label.RIGHT);
name = new TextField(12);
pass = new TextField(8);
pass.setEchoChar('?');
add(namep);
add(name);
add(passp);
add(pass);
name.addActionListener(this);
pass.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)


{
repaint();
}
public void paint(Graphics g)
{

20
g.drawString("Name: " + name.getText(), 6, 60);
g.drawString("Selected text in name: "+ name.getSelectedText(), 6, 80);
g.drawString("Password: " + pass.getText(), 6, 100);
}
}
Program to manipulate Scroll bars
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="SBDemo" width=300 height=200>
</applet>
*/
public class SBDemo extends Applet
implements AdjustmentListener, MouseMotionListener
{
String msg = "";
Scrollbar vertSB, horzSB;
public void init()
{
int width = Integer.parseInt(getParameter("width"));
int height = Integer.parseInt(getParameter("height"));
vertSB = new Scrollbar(Scrollbar.VERTICAL,0, 1, 0, height);
horzSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, width);
add(vertSB);
add(horzSB);
vertSB.addAdjustmentListener(this);
horzSB.addAdjustmentListener(this);
addMouseMotionListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
repaint();
}

public void mouseDragged(MouseEvent me)


{
int x = me.getX();
int y = me.getY();
vertSB.setValue(y);
horzSB.setValue(x);
repaint();
}

public void mouseMoved(MouseEvent me)


{
}

public void paint(Graphics g)


{
msg = "Vertical: " + vertSB.getValue();

21
msg += ", Horizontal: " + horzSB.getValue();
g.drawString(msg, 6, 160);
g.drawString("*", horzSB.getValue(),
vertSB.getValue());
}
}
Program to manipulate Text area
import java.awt.*;
import java.applet.*;

/* <applet code= "TextAreaDemo" width=300 height=250 >


</applet>
*/

public class TextAreaDemo extends Applet


{
public void init()
{
String val="This is java programming";
TextArea text=new TextArea(val,10,30);
add(text);
}
}
Program to manipulate menu
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/* <applet code="MenuDemo" width=200 height=200>


</applet>
*/

public class MenuDemo extends Frame implements ActionListener


{
MenuBar mb;
Menu m;
MenuItem mi1,mi2;
TextField t1;

public MenuDemo()
{
mb=new MenuBar();
m=new Menu("color");
mi1=new MenuItem("RED");
mi2=new MenuItem("BLUE");
mi1.addActionListener(this);
mi2.addActionListener(this);
setLayout(new FlowLayout());
mb.add(m);
m.add(mi1);
m.add(mi2);

22
setMenuBar(mb);
setSize(400,400);
setVisible(true);

public void actionPerformed(ActionEvent ae)


{
if(ae.getSource()==mi1)
setBackground(Color.red);

if(ae.getSource()==mi2)
setBackground(Color.blue);

}
public static void main(String args[])
{
new MenuDemo();
}
}

10. Select the values from the department table


Aim: Program to select the values from the department table.

Program:
import java.sql.*;
import java.io.*;
import java.sun.jdbc.odbc.*;

class Department
{
public static void main(String args[]){
try{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
}catch(ClassNotFoundException e1)
{
System.out.println(“error was caught”+e1.getMessage());
}
try{
Connection conn=DriverManager.getConnection(“jdbc:odbc:suni”,”scott”,”tiger”);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(“select * from dept”);
While(rs.next())
{
System.out.println(rs.getInt(1)+rs.getString(2)+rs.getString(3));
}
}catch(SQLException e2)
{
23
System.out.println(“error was caught”+e2.getMessage());
}
}
}

11. insert the values into student table using JDBC


Aim:a program to insert the values into student table using JDBC

Program
import java.sql.*;
import java.io.*;
import java.sun.jdbc.odbc.*;

class Student
{
public static void main(String args[]){
try{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
}catch(ClassNotFoundException e1)
{
System.out.println(“error was caught”+e1.getMessage());
}
try{
Connection conn=DriverManager.getConnection(“jdbc:odbc:suni”,”scott”,”tiger”);
PreparedStatement psmt=conn.prepareStatement(“insert into std values(?,?,?)”);
psmt.setInt(1,104);
psmt.setString(2,”ddd”);
psmt.setString(3,”pmcs”);
psmt.executeUpdate();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(“select * from std”);
While(rs.next())
{
System.out.println(rs.getInt(1)+rs.getString(2)+rs.getString(3));
}
}catch(SQLException e2)
{
System.out.println(“error was caught”+e2.getMessage());
}
}
}

24

Вам также может понравиться