Java Programming
Java Programming
Java Programming
}
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[])
{
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);
}
}
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:");
}
}
}
}
{
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);
}
}
}
class Ex2 extends Exception{
public Ex2(String msg){
super(msg);
}
{}
}
}
}
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");
}
}
try
{
File infile =new File("G:\\origin.txt");
File outfile =new File("G:\\dest.txt");
int length;
while ((length = instream.read(buffer)) > 0)
{
outstream.write(buffer, 0, length);
}
instream.close();
outstream.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
System.out.println(files);
}
}
}
}
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));
}
}
/*
<applet code="Applet3" height="300" width="300">
<param name="no1" value="10">
<param name="no2" value="20">
</applet>
*/
public class Applet3 extends Applet
{
g.drawString( s,20,20);
}
}
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);
}
}
setFocusable(true);
requestFocusInWindow();
bone=new Button("Yes");
add(bone);
btwo=new Button("No");
add(btwo);
}
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();
}
}
/*
<applet code="Calcu" width=200 height=200>
</applet>
*/
{
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);
}
{
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;
}
}
}
}
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;
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
{
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();
}
}
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;
}
}
try
{
Scanner sc=new Scanner(System.in);
System.out.println("Connecting to " + "Localhost" + " on port " + port);
Socket client = new Socket("localhost",port);
Server program :
import java.net.*;
import java.io.*;
}
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 ");
}
}
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)
{
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>