Advanced Java
Advanced Java
import java.lang.*;
class Object
{
static int i=0;
Object()
{
i++;
}
public static void main(String args[])
{
Object o1 = new Object();
System.out.print("object created no. ="+i);
Object o2 = new Object();
System.out.print("object creted no. ="+i);
}
}
OUTPUT
Client.java
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String[] args)
{
if(args.length != 2)
System.out.println("Usage : not done");
Else
{
String inp;
try
{
Socket sock = new
Socket(args[0],Integer.valueOf(args[1]).intValue());
DataInputStream is =new
DataInputStream(sock.getInputStream());
System.out.println("address :"+sock.getInetAddress());
System.out.println("port :"+sock.getPort());
System.out.println("Local
address :"+sock.getLocalAddress());
System.out.println("Local
port :"+sock.getLocalPort());
while((inp =is.readLine()) != null )
{
System.out.println(inp);
}
}
catch (UnknownHostException e)
{
System.out.println("Known Host:"+e.getMessage());
}
catch (IOException e)
{
System.out.println("error
IO:"+e.getMessage());
}
finally
{
System.out.println("end of program");
}
}
}
}
Server.java
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(1111);
Socket sock1 = sock.accept();
System.out.println(sock1.toString());
System.out.println("address :"+sock1.getInetAddress());
System.out.println("ports :"+sock1.getPort());
DataOutputStream out = new
DataOutputStream(sock1.getOutputStream());
out.writeBytes("Welcome"+sock1.getInetAddress().getHostName()
+"We are"+new Date());
sock1.close();
sock.close();
}catch(IOException err) {
System.out.println(err.getMessage());
}
finally {
System.out.println("end of program");
}
}
OUTPUT
Server window
Socket[addr=/192.168.1.5,port=1153,localport=1111]
address :/192.168.1.5
ports :1153
end of program
Client window
address :abc/192.168.1.5
port :1111
Local address :/192.168.1.5
Local port :1156
WelcomeabcWe areFri Mar 20 13:14:11 IST 2009
end of program
Ques4. Write a program showing hierarchal inheritance
class Info
{
int pid;
char branch;
char year;
void display()
{
System.out.println("\nPID\t: "+pid);
System.out.print("Branch\t: ");
if(branch == 'i')
System.out.println("Information Technology");
if(branch =='e')
System.out.println("Electronics and Telecommunication");
if(branch =='c')
System.out.println("Computer Science");
System.out.print("Year\t: ");
if(year == 'f')
System.out.println("FE");
if(year == 's')
System.out.println("SE");
if(year == 't')
System.out.println("TE");
}
}
void fdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tC\t"+c);
System.out.println("\tC++\t"+cpp);
}
}
void sdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tVB\t"+vb);
System.out.println("\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);
F.fdisplay();
S.sdisplay();
T.tdisplay();
}
}
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 java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.awt.*;
private int a;
this.a = a;
try{
Thread.sleep(1000);
}
class MainMyThread{
t1.start();
t2.start();
}
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 = shipment1.volume();
System.out.println("Volume of shipment1 is " + vol);
System.out.println("Weight of shipment1 is " + shipment1.weight);
System.out.println("Shipping cost: $" + shipment1.cost);
System.out.println();
vol = shipment2.volume();
System.out.println("Volume of shipment2 is " + vol);
System.out.println("Weight of shipment2 is " + shipment2.weight);
System.out.println("Shipping cost: $" + shipment2.cost);
}
}
OUTPUT
import java.io.*;
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 java.sql.*;
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 java.sql.*;
public class ColumnInfo {
public static void main(String[] args)
{
try
{
String str="select * from authors";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:MyData");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(str);
ResultSetMetaData rsmd=rs.getMetaData();
rs.next();
System.out.println("Numbers of attributes in the authors
table: "+rsmd.getColumnCount());
System.out.println("");
System.out.println("--------------------------------");
System.out.println("Attributes of the authors table");
System.out.println("--------------------------------");
for(int i=1;i<=rsmd.getColumnCount();i++)
{
System.out.println(rsmd.getColumnName(i)+" :
"+rsmd.getColumnTypeName(i));
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
System.out.println("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 java.io.*;
import java.util.*;
public class Room {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the width of a room");
int w=sc.nextInt();
System.out.println("Enter the height of a room");
int h=sc.nextInt();
System.out.println("Enter the depth of a room");
int d=sc.nextInt();
int volume=w*h*d;
System.out.println("Volume of a room : "+volume);
}
}
OUTPUT
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
//<applet code=DataInApp.class 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);
p.add(ls);
p.add(ts);
p.add(fname);
p.add(tfname);
p.add(lname);
p.add(tname);
p.add(ladd);
p.add(tadd);
p.add(b);
b.addActionListener(eobj);
}
class EventHandle implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:MyData");
PreparedStatement
s=con.prepareStatement("insert into authors values(?,?,?,?)");
s.setString(1,ts.getText());
s.setString(2,tfname.getText());
s.setString(3,tname.getText());
s.setString(4,tadd.getText());
s.executeUpdate();
getAppletContext().showStatus("done");
con.close();
con=DriverManager.getConnection("jdbc:odbc:MyData");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select *
from authors");
while(rs.next())
{
System.out.println(rs.getString(1)+"
"+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
}
con.close();
}
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()
{
System.out.println("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()
{
System.out.println("k: "+k);
}
}
public class Override
{
public static void main(String[] args)
{
B1 ob=new B1(1,2,3);
ob.show();
}
}
OUTPUT
k: 3
Ques19 Write a program showing dynamic method dispatch
class C1
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class D extends C1
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class E extends C1
{
void callme()
{
System.out.println("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;
f.callme();
f=d;
f.callme();
f=e;
f.callme();
}
}
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=a.volume( );
System.out.println("Volume is "+vol);
vol=b.volume( );
System.out.println("Volume is "+vol);
}
}
OUTPUT
Volume is 3000.0
Volume is 162.0
Ques21 Write a program to demonstrate method overloading
class OverloadDemo
{
void test()
{
System.out.println("No Parameters");
}
void test(int a)
{
System.out.println("a : "+a);
}
void test(int a,int b)
{
System.out.println("a and b : "+a+" "+b);
}
double test(double a)
{
System.out.println("a : "+a);
return a*a;
}
}
public class Overload
{
public static void main(String[] args)
{
OverloadDemo ob=new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10,20);
result=ob.test(123.25);
System.out.println("Result of ob.test(123.25) : "+result);
}
}
OUTPUT
No Parameters
a : 10
a and b : 10 20
a : 123.25
Result of ob.test(123.25) : 15190.5625
Ques22 Write a program to create a tiny editor
import java.io.*;
public class TinyEdit
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str[]=new String[100];
System.out.println("Enter lines of text");
System.out.println("Enter stop to quit");
for(int i=0;i<100;i++)
{
str[i]=br.readLine();
if(str[i].equals("stop"))
break;
}
System.out.println("\n Here is your file");
for(int i=0;i<100;i++)
{
if(str[i].equals("stop"))
break;
System.out.println(str[i]);
}
}
}
OUTPUT
package p1;
}
OUTPUT
J.J.Jaspers: $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
myInter.java
import java.rmi.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.*;
import java.rmi.server.*;
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>run.bat
C:\beans\beanbox>if "Windows_NT" == "Windows_NT" setlocal
C:\beans\beanbox>set
CLASSPATH=classes;..\lib\methodtracer.jar;..\infobus.jar
C:\beans\beanbox>java sun.beanbox.BeanBoxFrame
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 javax.swing.*;
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;
lname.setText(sname);
}
public String getSname()
{
return sname;
}
}
Now write the following code in the separate notepad and save it with .mft
extension
Name: Logo1.class
Java-Bean: true
C:>cd beans
C:/beans>cd beanbox
C:/beans/beanbox>run.bat
C:\beans\beanbox>if "Windows_NT" == "Windows_NT" setlocal
C:\beans\beanbox>set
CLASSPATH=classes;..\lib\methodtracer.jar;..\infobus.jar
C:\beans\beanbox>java sun.beanbox.BeanBoxFrame
After execute run file we have following output
<html>
<head>
<title>
JSP Example
</title>
</head>
<body>
<h1> WELCOME<h1>
<% out.println(“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 :
<% java.util.Date dt=new java.util.Date(); %>
<%=dt.getHours() %> : <%=dt.getMinutes() %> : <%=dt.getSeconds()%>
</html>
OUTPUT