Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

DAVIET/IT/2003760

JAVA LAB FILE


(BTIT506-18)

SUBMITTED IN PARTIAL FULFILMENT OF THE REQUIREMENTS FOR


THE AWARD OF DEGREE OF
BACHELOR OF TECHNOLOGY
IN
INFORMATION TECHNOLOGY

Submitted By: Faculty Incharge:

Name: Damini Mehra Mr. Rajesh Kochher


University Roll no :- 2003760
Btech(IT) 5th Sem
Class Roll No :- 416/20

DEPARTMENT OF INFORMATION TECHNOLOGY


DAV INSTITUTE OF ENGINEERING &
TECHNOLOGY
Jalandhar – 144008
January 2022

1
DAVIET/IT/2003760

Table of contents:
PAGE [T]
S.NO. TOPICS
NO. SIGN.
1 WAP in Java to show implementation the 3-4
classes.
2 WAP in Java to show implementation of 5
Inheritance
3 WAP in Java to show implementation of 6
packages and inheritance.
4 WAP in Java to show implementation of 7
threads
5 WAP in Java using exception handling 8
mechanisms
6 WAP in Java to show implementation of 9-10
Applets.
7 WAP in Java to show implementation of 11-14
mouse events, and keyboard events
8 WAP in Java to show implementing basic 15-17
file reading and writing methods.
9 WAP in Java to show Connecting to Database 18-20
using JDBC.
10 Using basic networking features, WAP in 21-23
java.

2
DAVIET/IT/2003760

Task 1: WAP in Java to show implementation the classes.


public class Dog
{
// Instance Variables
String name;
String breed;
int age;
String color;

// Constructor Declaration of Class


public Dog(String name, String breed,
int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}

// method 1
public String getName()
{
return name;
}

// method 2
public String getBreed()
{
return breed;
}

// method 3
public int getAge()
{
return age;
}

// method 4

3
DAVIET/IT/2003760

public String getColor()


{
return color;
}

@Override
public String toString()
{
return("Hi my name is "+ this.getName()+
".\nMy breed,age and color are " +
this.getBreed()+"," + this.getAge()+
","+ this.getColor());
}

public static void main(String[] args)


{
Dog tuffy = new Dog("tuffy","papillon", 5, "white");
System.out.println(tuffy.toString());
}
}
Output
Hi my name is tuffy.
My breed,age and color are papillon,5,white

4
DAVIET/IT/2003760

Task 2: WAP in Java to show implementation of Inheritance.


class Animal{

void eat(){System.out.println("eating...");} }

class Dog extends Animal{

void bark(){System.out.println("barking...");} }

class BabyDog extends Dog{

void weep(){System.out.println("weeping...");}

class TestInheritance2{

public static void main(String args[]){

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

}}

Output:

weeping...

barking...

eating...

5
DAVIET/IT/2003760

Task 3: WAP in Java to show implementation of packages and


inheritance.
The package keyword is used to create a package in java.

//save as Simple.java

package mypack;

public class Simple{

public static void main(String args[]){

System.out.println("Welcome to package");

} }

Output: Welcome to package

INTERFACE

interface printable{

void print();

class A6 implements printable{

public void print(){System.out.println("Hello");}

public static void main(String args[]){

A6 obj = new A6();

obj.print();

} }

Output:

Hello
6
DAVIET/IT/2003760

Task 4: WAP in Java to show implementation of threads


class Multi extends Thread{

public void run(){

System.out.println("thread is running...");

public static void main(String args[]){

Multi t1=new Multi();

t1.start();

Output:

thread is running...

7
DAVIET/IT/2003760

Task 5: WAP in Java using exception handling mechanisms.


public class JavaExceptionExample{

public static void main(String args[]){

try{

//code that may raise exception

int data=100/0;

}catch(ArithmeticException e){System.out.println(e);}

//rest code of the program

System.out.println("rest of the code...");

Output:

Exception in thread main java.lang.ArithmeticException:/ by zero

rest of the code...

8
DAVIET/IT/2003760

Task 6: WAP in Java to show implementation of Applets.


import java.applet.Applet;

import java.awt.Graphics;

public class First extends Applet{

public void paint(Graphics g){

g.drawString("welcome",150,150);

myapplet.html

<html>

<body>

<applet code="First.class" width="300" height="300">

</applet>

</body>

</html>

9
DAVIET/IT/2003760

Simple example of Applet by appletviewer tool:

//First.java

import java.applet.Applet;

import java.awt.Graphics;

public class First extends Applet{

public void paint(Graphics g){

g.drawString("welcome to applet",150,150);

/*

<applet code="First.class" width="300" height="300">

</applet>

*/

c:\>javac First.java

c:\>appletviewer First.java

10
DAVIET/IT/2003760

Task 7: WAP in Java to show implementation of mouse events, and


keyboard events.
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,MouseWheelListener {

String msg = "";

int mouseX = 0, mouseY = 0; // coordinates of mouse

public void init() {

addMouseListener(this);

addMouseMotionListener(this);

addMouseWheelListener(this);

// Handle mouse clicked.

public void mouseClicked(MouseEvent me) {

// save coordinates

mouseX = 0;

mouseY = 10;

11
DAVIET/IT/2003760

msg = "Mouse clicked.";

repaint();

// Handle mouse entered.

public void mouseEntered(MouseEvent me) {

// save coordinates

mouseX = 0;

mouseY = 10;

msg = "Mouse entered.";

repaint();

// Handle mouse exited.

public void mouseExited(MouseEvent me) {

// save coordinates

mouseX = 0;

mouseY = 10;

msg = "Mouse exited.";

repaint();

// Handle button pressed.

12
DAVIET/IT/2003760

public void mousePressed(MouseEvent me) {

// save coordinates

mouseX = me.getX();

mouseY = me.getY();

msg = "Down";

repaint();

// Handle button released.

public void mouseReleased(MouseEvent me) {

// save coordinates

mouseX = me.getX();

mouseY = me.getY();

msg = "Up";

repaint();

// Handle mouse dragged.

public void mouseDragged(MouseEvent me) {

// save coordinates

mouseX = me.getX();

mouseY = me.getY();

msg = "*";

13
DAVIET/IT/2003760

showStatus("Dragging mouse at " + mouseX + ", " + mouseY);

repaint();

// Handle mouse moved.

public void mouseMoved(MouseEvent me) {

// show status

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

// Handle mouse wheel moved.

public void mouseWheelMoved(MouseWheelEvent me) {

// show status

showStatus("Mouse Wheel Moving at " + me.getX() + ", " + me.getY());}

// Display msg in applet window at current X,Y location.

public void paint(Graphics g) {

g.drawString(msg, mouseX, mouseY);

14
DAVIET/IT/2003760

Task 8: WAP in Java to show implementing basic file reading and


writing methods.
import java.io.*;

import java.net.*;

public class clientSide {

// initialize socket and input output streams

private Socket socket = null;

private DataInputStream input = null;

private DataOutputStream out = null;

// constructor to put ip address and port

public clientSide(String address, int port)

// establish a connection

try {

socket = new Socket(address, port);

System.out.println("Connected");

// takes input from terminal

input = new DataInputStream(System.in);

15
DAVIET/IT/2003760

// sends output to the socket

out = new DataOutputStream(

socket.getOutputStream());

catch (UnknownHostException u) {

System.out.println(u);

catch (IOException i) {

System.out.println(i);

// string to read message from input

String line = "";

// keep reading until "End" is input

while (!line.equals("End")) {

try {

line = input.readLine();

out.writeUTF(line);

catch (IOException i) {

System.out.println(i);

16
DAVIET/IT/2003760

// close the connection

try {

input.close();

out.close();

socket.close();

catch (IOException i) {

System.out.println(i);

public static void main(String[] args)

clientSide client

= new clientSide("127.0.0.1", 5000);

17
DAVIET/IT/2003760

Task 9: WAP in Java to show Connecting to Database using JDBC.


// Java Program to Establish Connection in JDBC

// Importing database

import java.sql.*;

// Importing required classes

import java.util.*;

// Main class

class Main {

// Main driver method

public static void main(String a[])

// Creating the connection using Oracle DB

// Note: url syntax is standard, so do grasp

String url = "jdbc:oracle:thin:@localhost:1521:xe";

// Username and password to access DB

// Custom initialization

String user = "system";

String pass = "12345";

// Entering the data

Scanner k = new Scanner(System.in);

System.out.println("enter name");

String name = k.next();

18
DAVIET/IT/2003760

System.out.println("enter roll no");

int roll = k.nextInt();

System.out.println("enter class");

String cls = k.next();

// Inserting data using SQL query

String sql = "insert into student1 values('" + name

+ "'," + roll + ",'" + cls + "')";

// Connection class object

Connection con = null;

// Try block to check for exceptions

try {

// Registering drivers

DriverManager.registerDriver( new oracle.jdbc.OracleDriver());

// Reference to connection interface

con = DriverManager.getConnection(url, user, pass);

// Creating a statement

Statement st = con.createStatement();

// Executing query

int m = st.executeUpdate(sql);

if (m == 1)

19
DAVIET/IT/2003760

System.out.println(

"inserted successfully : " + sql);

else

System.out.println("insertion failed");

// Closing the connections

con.close();

// Catch block to handle exceptions

catch (Exception ex) {

// Display message when exceptions occurs

System.err.println(ex);

Output:

20
DAVIET/IT/2003760

Task 10: Using basic networking features, WAP in java.


// A Java program for a serverSide

import java.io.*;

import java.net.*;

public class serverSide {

// initialize socket and input stream

private Socket socket = null;

private ServerSocket server = null;

private DataInputStream in = null;

// constructor with port

public serverSide(int port)

// starts server and waits for a connection

try {

server = new ServerSocket(port);

System.out.println("Server started");

System.out.println("Waiting for a client ...");

socket = server.accept();

System.out.println("Client accepted");

// takes input from the client socket

21
DAVIET/IT/2003760

in = new DataInputStream(

new BufferedInputStream(

socket.getInputStream()));

String line = "";

// reads message from client until "End" is sent

while (!line.equals("End")) {

try {

line = in.readUTF();

System.out.println(line);

catch (IOException i) {

System.out.println(i);

System.out.println("Closing connection");

// close connection

socket.close();

in.close();

catch (IOException i) { System.out.println(i);

22
DAVIET/IT/2003760

public static void main(String[] args)

serverSide server = new serverSide(5000);

23

You might also like