Advanced Java Programming Examples
Advanced Java Programming Examples
import [Link].*;
class Object
{
static int i=0;
Object()
{
i++;
}
public static void main(String args[])
{
Object o1 = new Object();
[Link]("object created no. ="+i);
Object o2 = new Object();
[Link]("object creted no. ="+i);
}
}
OUTPUT
[Link]
import [Link].*;
import [Link].*;
public class Client
{
public static void main(String[] args)
{
if([Link] != 2)
[Link]("Usage : not done");
Else
{
String inp;
try
{
Socket sock = new
Socket(args[0],[Link](args[1]).intValue());
DataInputStream is =new
DataInputStream([Link]());
[Link]("address :"+[Link]());
[Link]("port :"+[Link]());
[Link]("Local
address :"+[Link]());
[Link]("Local
port :"+[Link]());
while((inp =[Link]()) != null )
{
[Link](inp);
}
}
catch (UnknownHostException e)
{
[Link]("Known Host:"+[Link]());
}
catch (IOException e)
{
[Link]("error
IO:"+[Link]());
}
finally
{
[Link]("end of program");
}
}
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(1111);
Socket sock1 = [Link]();
[Link]([Link]());
[Link]("address :"+[Link]());
[Link]("ports :"+[Link]());
DataOutputStream out = new
DataOutputStream([Link]());
[Link]("Welcome"+[Link]().getHostName()
+"We are"+new Date());
[Link]();
[Link]();
}catch(IOException err) {
[Link]([Link]());
}
finally {
[Link]("end of program");
}
}
OUTPUT
Server window
Socket[addr=/[Link],port=1153,localport=1111]
address :/[Link]
ports :1153
end of program
Client window
address :abc/[Link]
port :1111
Local address :/[Link]
Local port :1156
WelcomeabcWe areFri Mar 20 [Link] IST 2009
end of program
Ques4. Write a program showing hierarchal inheritance
class Info
{
int pid;
char branch;
char year;
void display()
{
[Link]("\nPID\t: "+pid);
[Link]("Branch\t: ");
if(branch == 'i')
[Link]("Information Technology");
if(branch =='e')
[Link]("Electronics and Telecommunication");
if(branch =='c')
[Link]("Computer Science");
[Link]("Year\t: ");
if(year == 'f')
[Link]("FE");
if(year == 's')
[Link]("SE");
if(year == 't')
[Link]("TE");
}
}
void fdisplay()
{
display();
[Link]("Performance:");
[Link]("\tC\t"+c);
[Link]("\tC++\t"+cpp);
}
}
void sdisplay()
{
display();
[Link]("Performance:");
[Link]("\tVB\t"+vb);
[Link]("\tHTML\t"+html);
}
}
class Te extends Info
{
int matlab;
int java;
class Language
{
public static void main(String args[])
{
Fe F = new Fe(1074,'i','f',9,8);
Se S = new Se(1064,'e','s',6,8);
Te T = new Te(1054,'c','t',9,9);
[Link]();
[Link]();
[Link]();
}
}
OUTPUT
PID : 1074
Branch : Information Technology
Year : FE
Performance:
C 9
C++ 8
PID : 1064
Branch : Electronics and Telecommunication
Year : SE
Performance:
VB 6
HTML 8
PID : 1054
Branch : Computer Science
Year : TE
Performance:
MATLAB 9
SJava 9
Ques9. Write a program to create GUI in which event handling is
done using AWT
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
private int a;
this.a = a;
try{
[Link](1000);
}
class MainMyThread{
[Link]();
[Link]();
}
OUTPUT
Thread-0 is 1
Thread-1 is 1
Thread-0 is 2
Thread-1 is 2
Thread-0 is 3
Thread-1 is 3
Thread-0 is 4
Thread-1 is 4
Thread-0 is 5
Thread-1 is 5
Thread-1 is 6
Thread-1 is 7
Thread-1 is 8
Thread-1 is 9
Thread-1 is 10
Ques3. Write a program related to multilevel inheritance
class Box {
private double width;
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
BoxWeight() {
super();
weight = -1;
}
Shipment() {
super();
cost = -1;
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 = new Shipment(10, 20, 15, 10, 3.41);
Shipment shipment2 = new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = [Link]();
[Link]("Volume of shipment1 is " + vol);
[Link]("Weight of shipment1 is " + [Link]);
[Link]("Shipping cost: $" + [Link]);
[Link]();
vol = [Link]();
[Link]("Volume of shipment2 is " + vol);
[Link]("Weight of shipment2 is " + [Link]);
[Link]("Shipping cost: $" + [Link]);
}
}
OUTPUT
import [Link].*;
67
gf
For input string: "gf" is not a numeric value.
Ques5. Write a program to demonstrate threads using thread class
0 Jamaica
0 Fiji
0 Bora Bora
1 Fiji
2 Fiji
1 Jamaica
1 Bora Bora
2 Jamaica
3 Fiji
3 Jamaica
2 Bora Bora
3 Bora Bora
4 Fiji
4 Jamaica
5 Fiji
4 Bora Bora
5 Jamaica
6 Fiji
5 Bora Bora
6 Bora Bora
6 Jamaica
7 Fiji
7 Bora Bora
8 Fiji
9 Fiji
7 Jamaica
8 Bora Bora
DONE! Fiji
8 Jamaica
9 Bora Bora
9 Jamaica
DONE! Bora Bora
DONE! Jamaica
Ques11. Write a program to extract data from database using
JDBC connectivity
import [Link].*;
public class AuthorsInfo {
}
Table made to fetch data from “authors”
authors
ID au_fname au_lname city
4 Marie Green Oakland
5 Dean Straight Greenland
6 Livia Karsen Roorkie
7 JK Copper New York
8 Dirk Stringer Oakland
OUTPUT
import [Link].*;
public class ColumnInfo {
public static void main(String[] args)
{
try
{
String str="select * from authors";
[Link]("[Link]");
Connection
con=[Link]("jdbc:odbc:MyData");
Statement stmt=[Link]();
ResultSet rs=[Link](str);
ResultSetMetaData rsmd=[Link]();
[Link]();
[Link]("Numbers of attributes in the authors
table: "+[Link]());
[Link]("");
[Link]("--------------------------------");
[Link]("Attributes of the authors table");
[Link]("--------------------------------");
for(int i=1;i<=[Link]();i++)
{
[Link]([Link](i)+" :
"+[Link](i));
}
[Link]();
[Link]();
[Link]();
}
catch(Exception e)
{
[Link]("Error : "+e);
}
}
}
OUTPUT
--------------------------------
Attributes of the authors table
--------------------------------
ID : COUNTER
au_fname : VARCHAR
au_lname : VARCHAR
city : VARCHAR
Ques2. Write a program to store width, height and depth of a room
import [Link].*;
import [Link].*;
public class Room {
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the width of a room");
int w=[Link]();
[Link]("Enter the height of a room");
int h=[Link]();
[Link]("Enter the depth of a room");
int d=[Link]();
int volume=w*h*d;
[Link]("Volume of a room : "+volume);
}
}
OUTPUT
import [Link].*;
import [Link].*;
import [Link].*;
//<applet code=[Link] width="500" height="200"> </applet>
public class DataInApp extends JApplet
{
JPanel p;
JLabel ls,lname,ladd,fname;
JTextField ts,tname,tadd,tfname;
JButton b;
public void init()
{
p=new JPanel();
ls=new JLabel("ID");
fname=new JLabel("First name");
lname=new JLabel("Last name");
ladd=new JLabel("City");
ts=new JTextField(10);
tfname=new JTextField(10);
tname=new JTextField(10);
tadd=new JTextField(10);
b=new JButton("Insert");
getContentPane().add(p);
[Link](ls);
[Link](ts);
[Link](fname);
[Link](tfname);
[Link](lname);
[Link](tname);
[Link](ladd);
[Link](tadd);
[Link](b);
[Link](eobj);
}
class EventHandle implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try{
[Link]("[Link]");
Connection
con=[Link]("jdbc:odbc:MyData");
PreparedStatement
s=[Link]("insert into authors values(?,?,?,?)");
[Link](1,[Link]());
[Link](2,[Link]());
[Link](3,[Link]());
[Link](4,[Link]());
[Link]();
getAppletContext().showStatus("done");
[Link]();
con=[Link]("jdbc:odbc:MyData");
Statement stmt=[Link]();
ResultSet rs=[Link]("select *
from authors");
while([Link]())
{
[Link]([Link](1)+"
"+[Link](2)+" "+[Link](3)+" "+[Link](4));
}
[Link]();
}
catch(Exception e)
{
getAppletContext().showStatus("Exception
"+ e);
}
}
}
}
Table before inserting was done
authors
ID au_fname au_lname city
1 Sarita Juneja America
2 Neha Jain Delhi
3 George Gunman Guhana
4 Marie Green Oakland
5 Dean Straight Greenland
6 Livia Karsen Roorkie
7 JK Copper New York
8 Dirk Stringer Oakland
9 Ruby Goel Madras
10 Shahnaz Hussain Mumbai
OUTPUT
Table after the insertion of an item
}
OUTPUT
Length of strob1: 12
Char at index 3 in strob1: s
strob1 != strob2
strob1 = = strob3
B) Write a program to bubble sort the string
}
OUTPUT
Now
aid
all
come
country
for
good
is
men
of
the
the
their
time
to
to
C) Write a program to demonstrate use of indexof( ) and
lastindexof( )
}
OUTPUT
Now is the time for all the good mento come to the aid of their country.
indexof(t) = 7
lastIndexof(t) = 68
indexof(the) = 7
lastIndexof(the) = 58
indexof(t,10) = 11
lastIndexof(t,60) = 58
indexof(the,10) = 24
lastIndexof(the,60) = 58
Ques15
A)Write a program to demonstrate use of if else statements
}
OUTPUT
}
OUTPUT
Not prime
C) Write a program to demonstrate use of switch statements
i is zero
i is one
i is two
i is three
i is greater than 3
i is greater than 3
D) Write a program to demonstrate use of while loop
}
OUTPUT
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
E) Write a program using do-while to process a menu selection
Help on:
1: if:
2: switch:
3: while:
4: do-while:
5: for:
Choose one:
2
The Switch:
switch(expression) {
Case contant:
Statement sequence
break;
//.....
}
Ques16
A)Write a program to average an array of values
01234
56789
10 11 12 13 14
15 16 17 18 19
Ques17
A)Write a program to demonstrate the basic arithmetic
operators
Integer Arithmetic
a=2
b=6
c=1
d = -1
e=1
Floating point arithmetic
da = 2.0
db = 6.0
dc = 1.5
dd = -0.5
de = 0.5
B) Write a program to demonstrate the bitwise logical
operators
}
OUTPUT
a= 0011
b= 0110
c= 0111
d= 0010
e= 0101
f= 0101
g= 1100
C) Write a program to demonstrate use of ternary operator
Absolute value of
10 is 10
Absolute value of
-10 is 10
Ques18 Write a program to demonstrate method overriding
class A1
{
int i,j;
A1(int a,int b)
{
i=a;
j=b;
}
void show()
{
[Link]("i and j: "+i+" "+j);
}
}
class B1 extends A1
{
int k;
B1(int a,int b,int c)
{
super(a,b);
k=c;
}
void show()
{
[Link]("k: "+k);
}
}
public class Override
{
public static void main(String[] args)
{
B1 ob=new B1(1,2,3);
[Link]();
}
}
OUTPUT
k: 3
Ques19 Write a program showing dynamic method dispatch
class C1
{
void callme()
{
[Link]("Inside A's callme method");
}
}
class D extends C1
{
void callme()
{
[Link]("Inside A's callme method");
}
}
class E extends C1
{
void callme()
{
[Link]("Inside A's callme method");
}
}
public class Dispatch
{
public static void main(String[] args)
{
C1 c=new C1();
D d=new D();
E e=new E();
C1 f;
f=c;
[Link]();
f=d;
[Link]();
f=e;
[Link]();
}
}
OUTPUT
class Box2
{
double width,height,depth;
Box2(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
double volume()
{
return width*height*depth;
}
}
public class BoxDemo7
{
public static void main(String[] args)
{
Box2 a=new Box2(10,20,15);
Box2 b=new Box2(3,6,9);
double vol;
vol=[Link]( );
[Link]("Volume is "+vol);
vol=[Link]( );
[Link]("Volume is "+vol);
}
}
OUTPUT
Volume is 3000.0
Volume is 162.0
Ques21 Write a program to demonstrate method overloading
class OverloadDemo
{
void test()
{
[Link]("No Parameters");
}
void test(int a)
{
[Link]("a : "+a);
}
void test(int a,int b)
{
[Link]("a and b : "+a+" "+b);
}
double test(double a)
{
[Link]("a : "+a);
return a*a;
}
}
public class Overload
{
public static void main(String[] args)
{
OverloadDemo ob=new OverloadDemo();
double result;
[Link]();
[Link](10);
[Link](10,20);
result=[Link](123.25);
[Link]("Result of [Link](123.25) : "+result);
}
}
OUTPUT
No Parameters
a : 10
a and b : 10 20
a : 123.25
Result of [Link](123.25) : 15190.5625
Ques22 Write a program to create a tiny editor
import [Link].*;
public class TinyEdit
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
String str[]=new String[100];
[Link]("Enter lines of text");
[Link]("Enter stop to quit");
for(int i=0;i<100;i++)
{
str[i]=[Link]();
if(str[i].equals("stop"))
break;
}
[Link]("\n Here is your file");
for(int i=0;i<100;i++)
{
if(str[i].equals("stop"))
break;
[Link](str[i]);
}
}
}
OUTPUT
package p1;
}
OUTPUT
[Link]: $99.88
B) Write a program to demonstrate use of interfaces
}
OUTPUT
Stack in mystack1
4
3
2
1
0
Stack in mystack2
7
6
5
4
3
2
1
0
Ques24 Write a program demonstrates remote method
invocation
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
C:>cd rmi
C:/rmi>policytool
C:/rmi>javac *.java
C:/rmi>start rmiregistry
C:/rmi>java myServer
Server registered
C:/rmi>java myClient
Ans from server 55
Ques25 Write a program demonstrating use of predefined
beans
C:>cd beans
C:/beans>cd beanbox
C:/beans/beanbox>[Link]
C:\beans\beanbox>if "Windows_NT" == "Windows_NT" setlocal
C:\beans\beanbox>set
CLASSPATH=classes;..\lib\[Link];..\[Link]
C:\beans\beanbox>java [Link]
14 A)Write a program to demonstrate string
methods
B) Write a program to bubble sort the string
C) Write a program to demonstrate use of
indexof( ) and lastindexof( )
15 A)Write a program to demonstrate use of if
else statements
B) Write a program showing whether the
number is prime or not.
C) Write a program to demonstrate use of
switch statements
D) Write a program to demonstrate use of
while loop
E) Write a program using do-while to
process a menu selection
16 A)Write a program to average an array of
values
B) Write a program to demonstrate 2-D array
17 A)Write a program to demonstrate the basic
arithmetic operators
B)Write a program to demonstrate the
bitwise logical operators
C)Write a program to demonstrate use of
ternary operator
18 Write a program to demonstrate method
overriding
19 Write a program showing dynamic method
dispatch
20 Write a program showing use of
parameterized constructor
21 Write a program to demonstrate method
overloading
22 Write a program to create a tiny editor
23 A)Write a program to demonstrate use of
packages
B)Write a program to demonstrate use of
interfaces
24 Write a program demonstrates remote
method invocation
25 Write a program demonstrating use of
predefined beans
26 Write a program demonstrating use of
userdefined beans
27 Write a program to show usage of JSP pages
28 Write a program to generate current date and
time with the use of JSP pages
Ques26 Write a program demonstrating use of user defined
beans
import [Link].*;
public class Logo1 extends JPanel
{
private String sname=" MY LOGO";
JLabel lname;
JTextField tname;
public Logo1()
{
lname=new JLabel(sname);
tname=new JTextField(10);
add(lname);
add(tname);
}
public void setSname(String str)
{
sname=str;
[Link](sname);
}
public String getSname()
{
return sname;
}
}
Now write the following code in the separate notepad and save it with .mft
extension
Name: [Link]
Java-Bean: true
C:>cd beans
C:/beans>cd beanbox
C:/beans/beanbox>[Link]
C:\beans\beanbox>if "Windows_NT" == "Windows_NT" setlocal
C:\beans\beanbox>set
CLASSPATH=classes;..\lib\[Link];..\[Link]
C:\beans\beanbox>java [Link]
After execute run file we have following output
<html>
<head>
<title>
JSP Example
</title>
</head>
<body>
<h1> WELCOME<h1>
<% [Link](“hello”); %>
</body>
<html>
OUTPUT
WELCOME
Hello
Ques28 Write a program to generate current date and time
with the use of JSP pages
<html>
<h1>Welcome to JSP</h1>
<hr>server Time :
<% [Link] dt=new [Link](); %>
<%=[Link]() %> : <%=[Link]() %> : <%=[Link]()%>
</html>
OUTPUT