You are on page 1of 68

1.

Develop a program to implement the concept of interface, with variables and


methods.
Program
interface Sample
{
void show();
int a=10;
}
class ABC implements Sample
{
public void show()
{
System.out.println("Show method called" + a);
}
}
class Demo190
{
public static void main(String args[])
{
ABC ob=new ABC();
ob.show();

System.out.println("with class name "+ABC.a);


}
}

2. Develop a program to implement the concept of inheritance with method


overriding.
Program
import java.util.Scanner;
class ABC
{
int i;
int j;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of i:");
i=sc.nextInt();
System.out.println("Enter the value of j:");
j=sc.nextInt();
}
void display()
{
System.out.println("Value of i is "+i);
System.out.println("Value of j is "+j);
}

}
class XYZ extends ABC
{
int k;
ABC obj1=new ABC();
void accept()
{
super.accept();
Scanner ob=new Scanner(System.in);
System.out.println("Enter the value of k:");
k=ob.nextInt();
}
void display()
{
super.display();
System.out.println("The value of k is "+k);
}
}
class Inheritance
{
public static void main(String args[])
{

XYZ obj=new XYZ();


obj.accept();
obj.display();
}
}

3. Java program to demonstrate arrays:


a. Create an Array in Java
b. Create Different Array in Different ways in Java
c. Array of Objects in Java
d. Create Two Dimensional Array in Java
(Use the concept of packages and interfaces)

4. Develop a menu driven program for the following


a. Create an array
b. Sort an array
c. Search an element inside it
(Use the concept of packages and interfaces)

5. Develop a program to use for and foreach loop for printing values of an
array and display the number of even and odd numbers?
Program
import java.util.Scanner;
class Numbers
{
public static void main(String args[])
{
Scanner key=new Scanner(System.in);

int i,oc,ec,limit;
oc=0;
ec=0;
System.out.println("Enter the limit");
limit=key.nextInt();
int a[]=new int[limit];
System.out.println("Enter "+ limit +" numbers");
for(i=0;i<limit;i++)
{
a[i]=key.nextInt();
}

for(i=0;i<limit;i++)
{
if(a[i]%2==0)
ec++;
else
oc++;
}
System.out.println("Even count:"+ec);
System.out.println("Odd count:"+oc);

}
}

6. Develop a program to match regions in a string?


Program

import java.util.*;
public class StrPgm6
{
public static void main(String args[])
{
String str1,str2;
int st1,st2,stop;
Scanner sc=new Scanner(System.in);
System.out.println("enter the first string : ");
str1=sc.nextLine();
System.out.println("enter the second string : ");
str2=sc.nextLine();
System.out.println("enter the start position of the first
string");
st1=sc.nextInt();
System.out.println("enter the start and end position of the
second string");
st2=sc.nextInt();

stop=sc.nextInt();
System.out.println("return value:");
System.out.println(str1.regionMatches(st1,str2,st2,stop));
System.out.println("return value:");
}
}

7. Develop a program to remove a particular character from a string.


Program
import java.io.*;
class StringRemoval
{
String string1=new String();
void read()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the string ");
string1=br.readLine();
}
void display()
{
System.out.println("the string is "+string1);
}
void remove()throws IOException
{

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("enter the charcter to be removed ");
char c=(char)br.read();
string1=string1.replace(""+c,"");
}
}
class Pgm7
{
public static void main (String args[])
{
StringRemoval sr=new StringRemoval();
try{
sr.read();
sr.display();
sr.remove();
sr.display();
}
catch (Exception e)
{

}
}

8. Develop a program to demonstrate exception handling use finally block?


Program
import java.util.Scanner;
class ABC
{
int a,b;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a:");
a=sc.nextInt();
System.out.println("Enter the value of b:");
b=sc.nextInt();
}
void displayValues()
{
try
{
System.out.println("The result of a/b is "+a/b);
}
catch(ArithmeticException e)

{
e.printStackTrace();
System.out.println("Exception caught "+e.getMessage());
}
finally
{
System.out.println("Finally");
}
}
}
class DemoExce
{
public static void main(String args[])
{
ABC obj=new ABC();
obj.accept();
obj.displayValues();
}
}

9. Develop a program to create user defined Exception? Input two numbers and
find its sum. If any one of the number is zero generate user defined exception
One of the operand is zero.
Program
import java.util.Scanner;
class Use {
public static void main(String args[]){
int a;
int b;
int c;
Scanner ax=new Scanner(System.in);
Scanner bx=new Scanner(System.in);
try{
System.out.println("Enter a");
a=ax.nextInt();
System.out.println("Enter b");
b=bx.nextInt();
if(a==0 || b==0){
throw new Ex2("one of the operand is zero");
}
else

c=a+b;
System.out.println("The sum is "+c);
}

catch (Exception Ex2){


System.out.println("one of the operand is zero");
}

}
}
class Ex2 extends Exception{
public Ex2(String msg){
super(msg);
}

10. Implement the concept of thread programming.


Program
class MyThread implements Runnable
{
Thread t;
MyThread(String msg)
{
System.out.println("Thread executed");
t=new Thread(this,msg);
t.start();
}
public void run()
{
for(int i=0;i<10;i++)
{
try{
System.out.println(Thread.currentThread()+"thread i
value"+i);
Thread.sleep(1000);
}
catch(InterruptedException e)

{}
}
}
}
class DemoRunn
{
public static void main(String args[])
{
System.out.println("main thread"+Thread.currentThread());
MyThread obj=new MyThread("one");
System.out.println("one");
MyThread obj2=new MyThread("two");
MyThread obj3=new MyThread("three");
/*try{
obj.t.join();
obj2.t.join();
obj3.t.join();
}
catch(InterruptedException e)*/
{}
System.out.println("main Thread exited");

}
}

11. Develop a program to copy one file into another file?


Program
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyExample


{
public static void main(String[] args)
{
FileInputStream instream = null;
FileOutputStream outstream = null;

try
{
File infile =new File("G:\\origin.txt");
File outfile =new File("G:\\dest.txt");

instream = new FileInputStream(infile);


outstream = new FileOutputStream(outfile);

byte[] buffer = new byte[1024];

int length;
while ((length = instream.read(buffer)) > 0)
{
outstream.write(buffer, 0, length);
}

instream.close();
outstream.close();

System.out.println("File copied successfully!!");

}catch(IOException ioe){
ioe.printStackTrace();
}
}
}

12. Develop a program to search all files inside a directory?


Program
import java.io.*;
public class ListFiles
{

public static void main(String[] args)


{

String path = "F:/java/java";

String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++)


{

if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();

System.out.println(files);
}
}
}
}

13. Develop a program to demonstrate the generic methods and classes.


Program
class GenExample<T extends Number>
{
public T x;
GenExample(T a)
{
x=a;
}
boolean calculate(GenExample<T> obj)
{
if(x.doubleValue()==obj.x.doubleValue())
{
return true;
}
else
{
return false;
}
}
}

class Demo201
{
public static void main(String args[])
{
GenExample<Integer> obj1=new GenExample<Integer>(10);
GenExample<Integer> obj2=new GenExample<Integer>(20);
System.out.println(obj1.calculate(obj2));

}
}

14. Applet program for passing parameters


Program
import java.applet.*;
import java.awt.*;

/*
<applet code="Applet3" height="300" width="300">
<param name="no1" value="10">
<param name="no2" value="20">
</applet>
*/
public class Applet3 extends Applet
{

public void paint(Graphics g)


{
int n1 = Integer.parseInt(this.getParameter("no1"));
int n2 = Integer.parseInt(this.getParameter("no2"));

String s = "Java Applet Parameter Example : sum of n1 and n2 is :"


+ (n1+n2);

g.drawString( s,20,20);
}
}

15. Applet program to draw human face.


Program
import java.applet.*;
import java.awt.*;

/* <applet code = "GraFace" width = 300 height = 300> </applet> */

public class GraFace extends Applet


{
public void paint(Graphics g)
{
g.drawOval(100,100,100,100);
g.fillOval(120,125,20,20);
g.fillOval(160,125,20,20);
g.drawLine(150,150,140,165);
g.drawLine(150,150,160,165);
g.drawLine(140,165,160,165);
g.fillArc(138,170,30,20,0,180);
g.fillArc(138,173,30,20,0,-180);
}
}

16. Develop a program to create a banner using Applet?


Program
import java.awt.*;
import java.applet.*;
/*
<applet code="SampleBanner" width=500 height=500>
</applet>
*/
public class SampleBanner extends Applet implements Runnable
{
String str = "This is a simple Banner ";
Thread t ;
boolean b;
public void init()
{
setForeground(Color.pink);
}
public void start()
{
t = new Thread(this);
b = false;

t.start();
}
public void run ()
{
char ch;
for( ; ; )
{
try
{
repaint();
Thread.sleep(250);
ch = str.charAt(0);
str = str.substring(1, str.length());
str = str + ch;
}
catch(InterruptedException e) {}
}
}
public void paint(Graphics g) {
g.drawRect(100,100,300,150);
g.setColor(Color.pink);
g.fillRect(100,100,300,150);

g.setColor(Color.blue);
g.drawString(str, 200, 150);
}
}

17. Write a program to implement concept of playing audio file.


Program
import java.awt.*;
import java.applet.*;
import java.net.*;
/*
<applet code="DemoAudio" width=300 height=200>
</applet>
*/
public class DemoAudio extends Applet
{
AudioClip ac;
public void init()
{
try
{
ac=getAudioClip(new URL(getCodeBase(),"audio.wav"));
}
catch(MalformedURLException e)
{
System.out.println("Exception caught");

public void start()


{
ac.play();
}
}

18. Event driven program for Graphical Drawing Application.


a. Mouse Event,
b. Window Event,
c. Action Event
Program
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="Event" width=300 height=200>
</applet>
*/
public class Event extends Applet implements
MouseListener,KeyListener,ActionListener
{
String msg="";
Button bone;
Button btwo, bthree;
public void init()
{

setFocusable(true);
requestFocusInWindow();

bone=new Button("Yes");
add(bone);

btwo=new Button("No");
add(btwo);

bthree=new Button("Get Focus on Applet ");


add(bthree);
bone.addActionListener(this);
btwo.addActionListener(this);
bthree.addActionListener(this);
addMouseListener(this);
addKeyListener(this);

}
public void paint(Graphics g)

{
g.drawString(msg,20,70);
}
public void keyPressed(KeyEvent ke)
{
System.out.println("key pressed ");
msg="Key Pressed";
repaint();
}
public void keyReleased(KeyEvent ke)
{
msg="Key Released "+ke.getKeyChar();
repaint();
}
public void keyTyped(KeyEvent ke)
{
msg="Key Typed";
repaint();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==bone)

{
msg="Yes button clicked";

}
else if(ae.getSource() == bthree)
{
requestFocusInWindow();

}
else
{
msg="No button clicked";

}
repaint();
}
public void mouseClicked(MouseEvent me)
{
msg="Mouse Clicked ("+me.getX()+","+me.getY()+")";
repaint();
}
public void mouseEntered(MouseEvent me)

{
msg="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
msg="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
msg="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
msg="Mouse Released";
repaint();
}
}

19. Develop a Simple calculator using GUI?


Program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

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

public class Calcu extends Applet


implements ActionListener
{
String msg=" ";
int v1,v2,result,j,k;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()

{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField();

for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);

}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)

{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';

t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')

result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
public void paint(Graphics g)
{
t1.setLocation(20,20);
t1.setSize( 170,20);
this.add.setLocation(20, 40);
this.add.setSize(50, 20);
this.sub.setLocation(60, 40);
this.sub.setSize(50, 20);

this.mul.setLocation(100, 40);

this.mul.setSize(50, 20);
this.div.setLocation(140, 40);
this.div.setSize(50, 20);
this.mod.setLocation(20, 60);
this.mod.setSize(50, 20);
this.EQ.setLocation(60, 60);
this.EQ.setSize(50, 20);
this.clear.setLocation(110, 60);
this.clear.setSize(80, 20);
j=20;
for(int i=0;i<5;i++)
{
this.b[i].setLocation(j,80);
this.b[i].setSize(50,20);
j=j+30;
}
k=20;
for(int i=5;i<10;i++)
{
this.b[i].setLocation(k,100);
this.b[i].setSize(50,20);
k=k+30;

}
}

20. Write a program to determine


a. IP Address
b. Hostname
Program
import java.net.*;
class DemoIP
{
public static void main(String args[]) throws UnknownHostException
{
InetAddress ineta = InetAddress.getLocalHost();
System.out.println(ineta);
ineta=InetAddress.getByName("google.com");
System.out.println(ineta);
InetAddress obj[]=InetAddress.getAllByName("yahoo.com");
for(int i=0;i<obj.length;i++)
{
System.out.println(obj[i]);
}

}
}

21. Write a program to


a. Create a database named db_marin_<roll> and table named
tbl_marian with fields fld_name and fld_age
b. insert values to tbl_marian
c. display values in tbl_marian
Program
import java.io.*;
import java.sql.*;
import java.util.*;

class Pgm21
{
public static void main(String args[])
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
boolean f=true;
try
{

Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/test";
String username="root";
String password="";

conn=DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
String sql1="create database db_marian";
System.out.println("database created
"+stmt.executeUpdate(sql1));
conn.close();
url="jdbc:mysql://localhost/db_marian";

conn=DriverManager.getConnection(url,username,password);
stmt=conn.createStatement();
System.out.println("database connected");
Scanner sc=new Scanner(System.in);
sql1 ="create table tbl_marian(fld_name varchar(30),fld_age
integer)";
System.out.println(""+stmt.executeUpdate(sql1));
System.out.print("table created \n");
String name="";
int age;

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


name=sc.nextLine();
System.out.println("enter age : ");
age=sc.nextInt();
sql1="insert into
tbl_marian(fld_name,fld_age)values('"+name+"',"+age+")";
System.out.println(""+stmt.executeUpdate(sql1));
sql1="select * from tbl_marian";
rs=stmt.executeQuery(sql1);
while(rs.next())
{
if(f)
{
System.out.println("FIRST NAME, AGE");
f=false;
}
System.out.println();
System.out.print(""+rs.getString("fld_name"));
System.out.print(" "+rs.getInt("fld_age"));
}

catch(SQLException e)
{
e.printStackTrace();

}
catch(Exception e)
{
System.out.println("Class not exception found ");
}
}
}

22. Create a client server program for the following using UDP
a. Client should send a number to server
b. Server should returns the number into words to client.
c. Print the reverse of that number in server also.
Program
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPChatOne
{

public static void main(String[] args) {


try {
DatagramSocket socket = null;
socket = new DatagramSocket(9876);
byte[] incomingData = new byte[1024];
while (true) {
DatagramPacket incomingPacket = new
DatagramPacket(incomingData, incomingData.length);

socket.receive(incomingPacket);
String message = new String(incomingPacket.getData());
int value=Integer.parseInt(message.trim());
System.out.println("Received Value : " + value);
System.out.println("Reversed value :" + new
StringBuilder(message).reverse().toString());
InetAddress IPAddress = incomingPacket.getAddress();
int port = incomingPacket.getPort();
String reply = intoWords(message.trim());
byte[] data = reply.getBytes();
DatagramPacket replyPacket =
new DatagramPacket(data, data.length, IPAddress, port);
socket.send(replyPacket);
Thread.sleep(2000);
}
} catch (Exception e) {
e.printStackTrace();
}
}

static String intoWords(String message)


{

String word="";
String INTOWORD []= {" Zero "," One "," Two " ," Three "," Four "," Five ","
Six "," Seven "," Eight "," Nine "," Ten "};
for(int i=0;i<message.length();i++)
{
word +=
INTOWORD[Integer.parseInt(String.valueOf(message.charAt(i)))].toString();
}
return word;
}
}

23. Create an authentication application using TCP. Client should insert


username and password and this credentials validated by server.
Program
Client program :
import java.net.*;
import java.io.*;
import java.util.*;

public class Pgm23_Client


{
public static void main(String [] args)
{

int port = Integer.parseInt(args[0]);

try
{
Scanner sc=new Scanner(System.in);
System.out.println("Connecting to " + "Localhost" + " on port " + port);
Socket client = new Socket("localhost",port);

System.out.println("Just connected to "+


client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
System.out.println("Enter your user name ");
String username=sc.next();
System.out.println("Enter the password");
String password=sc.next();
out.writeUTF(username);
out.writeUTF(password);
out.writeUTF("bye");
InputStream inFromServer = client.getInputStream();
DataInputStream in =new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}

Server program :
import java.net.*;
import java.io.*;

public class Pgm23_Server extends Thread


{

public static void main(String [] args)


{
int port = Integer.parseInt(args[0]);
ServerSocket serverSocket;
try
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
while(true)
{
try
{
System.out.println("Waiting for client on port "
+serverSocket.getLocalPort() + "...");

Socket server = serverSocket.accept();


DataInputStream in =new DataInputStream(server.getInputStream());
String line="";
String array[] = new String[2];
int i=0;
while(true)
{
line=in.readUTF();
if(line.equals("bye") == true)
{
break;
}
array[i]=line;
i++;
System.out.println(line);

}
DataOutputStream out = new
DataOutputStream(server.getOutputStream());
if(array[0].equals("hari") && array[1].equals("god")){
out.writeUTF(""+array[0]+" Thank you for connecting to "+
server.getLocalSocketAddress() + "\nGoodbye!");

server.close();
}
else {
out.writeUTF("incorrect credentials");
server.close();
}
}catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
catch(Exception e)
{
System.out.println(" Exception ");
}
}

24. Create a web application for department using html.


Program

25. Create a page with JavaScript to do the following. These can all be on one
page.
a. Prompt the user for their name.
b. Use a pop-up box to welcome the user by name
c. Display the current date on the page in the following format: April 30,
2012. Do not display the time. Do not "hard code" the date; if I load
the page tomorrow, I should get a different date than if I load it today.
d. Display the last modified date of the document
e. Put some useful advice, on any subject, in the status line of the browser
f. Display a message saying Good Morning if it is in the morning, Good
Afternoon if it is in the afternoon, and Good Evening if it is in the
Evening.
Program
<!doctype html>
<html>
<head>
<title>
JAVA SCRIPT ENABLED PAGE
</title>
</head>
<body>
<form>

<script>
var txt= prompt("enter the name :");

if (txt !=null)
{

alert('hello user '+txt+' have a nice day');


}
</script>
<p id="p1"> <script language="javascript">
try
{
var d=new Date();
var dd=d.getDate();
var mm=d.getMonth();
var mmm="";
switch(mm){
case 0:
mmm="JANUARY";break;
case 1: mmm="FEBRUARY";break;
case 2: mmm="MARCH";break;
case 3: mmm="APRIL";break;

case 4: mmm="MAY";break;
case 5: mmm="JUNE";break;
case 6: mmm="JULY";break;
case 7: mmm="AUGUST";break;
case 8: mmm="SEPTEMBER";break;
case 9: mmm="OCTOBER";break;
case 10: mmm="NOVEMBER";break;
case 11:mmm="DECEMBER";break;
default: break;
}
var yy=d.getFullYear();
document.write("current date is "+mmm+ " "+dd+" ,"+yy);
}
catch(e)
{
console.log("error")
}
</script> </p>
<p><script language="Javascript">
document.write("This page was last modified on: " +
document.lastModified +"");
</SCRIPT></p><p>

<script>
try{
var dt=new Date();
var ddf=dt.getHours();
var grtngs="";
if(ddf<12){
grtngs="good morning";
}
else
{
grtngs="good after noon";
}
document.write(" user "+grtngs);
}
catch(e)
{
console.log("error")
}

</script></p>
<p>

<script>window.status="java is powerful, robust,secure and


reliable";</script>
</p>
</form>
</body>
</html>

You might also like