Professional Documents
Culture Documents
Java File
Java File
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
// 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
@Override
public String toString()
{
return("Hi my name is "+ this.getName()+
".\nMy breed,age and color are " +
this.getBreed()+"," + this.getAge()+
","+ this.getColor());
}
4
DAVIET/IT/2003760
void eat(){System.out.println("eating...");} }
void bark(){System.out.println("barking...");} }
void weep(){System.out.println("weeping...");}
class TestInheritance2{
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating...
5
DAVIET/IT/2003760
//save as Simple.java
package mypack;
System.out.println("Welcome to package");
} }
INTERFACE
interface printable{
void print();
obj.print();
} }
Output:
Hello
6
DAVIET/IT/2003760
System.out.println("thread is running...");
t1.start();
Output:
thread is running...
7
DAVIET/IT/2003760
try{
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
Output:
8
DAVIET/IT/2003760
import java.awt.Graphics;
g.drawString("welcome",150,150);
myapplet.html
<html>
<body>
</applet>
</body>
</html>
9
DAVIET/IT/2003760
//First.java
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("welcome to applet",150,150);
/*
</applet>
*/
c:\>javac First.java
c:\>appletviewer First.java
10
DAVIET/IT/2003760
import java.awt.event.*;
import java.applet.*;
MouseMotionListener,MouseWheelListener {
addMouseListener(this);
addMouseMotionListener(this);
addMouseWheelListener(this);
// save coordinates
mouseX = 0;
mouseY = 10;
11
DAVIET/IT/2003760
repaint();
// save coordinates
mouseX = 0;
mouseY = 10;
repaint();
// save coordinates
mouseX = 0;
mouseY = 10;
repaint();
12
DAVIET/IT/2003760
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Down";
repaint();
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
13
DAVIET/IT/2003760
repaint();
// show status
// show status
14
DAVIET/IT/2003760
import java.net.*;
// establish a connection
try {
System.out.println("Connected");
15
DAVIET/IT/2003760
socket.getOutputStream());
catch (UnknownHostException u) {
System.out.println(u);
catch (IOException i) {
System.out.println(i);
while (!line.equals("End")) {
try {
line = input.readLine();
out.writeUTF(line);
catch (IOException i) {
System.out.println(i);
16
DAVIET/IT/2003760
try {
input.close();
out.close();
socket.close();
catch (IOException i) {
System.out.println(i);
clientSide client
17
DAVIET/IT/2003760
// Importing database
import java.sql.*;
import java.util.*;
// Main class
class Main {
// Custom initialization
System.out.println("enter name");
18
DAVIET/IT/2003760
System.out.println("enter class");
try {
// Registering drivers
// Creating a statement
Statement st = con.createStatement();
// Executing query
int m = st.executeUpdate(sql);
if (m == 1)
19
DAVIET/IT/2003760
System.out.println(
else
System.out.println("insertion failed");
con.close();
System.err.println(ex);
Output:
20
DAVIET/IT/2003760
import java.io.*;
import java.net.*;
try {
System.out.println("Server started");
socket = server.accept();
System.out.println("Client accepted");
21
DAVIET/IT/2003760
in = new DataInputStream(
new BufferedInputStream(
socket.getInputStream()));
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();
22
DAVIET/IT/2003760
23