Documente Academic
Documente Profesional
Documente Cultură
import java.io.*;
abstract class cal
{
final float PI=3.14f; //Declaring PI
}
//implementing interface
interface functions
{
//Declaring methods
public float area();
public float perimeter();
}
//Class for circle
class Circle extends cal implements functions
{
float rad;
void cir(float rad)
{
this.rad=rad;
}
public float area()
{
return PI*rad*rad;
}
public float perimeter()
{
return 2*PI*rad;
}
}
//Main class
class area
{
public static void main(String args[])
{
String s;
float radius;
try
{
System.out.println("Enter the value for radius: ");
//Creating Buffered Reader to get value during runtime
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
s=br.readLine();
radius=Float.parseFloat(s); //Typecasting String to Float
Circle c=new Circle(); //Creating an object
c.cir(radius);
float ar=c.area();
float pr=c.perimeter();
//Printing result
System.out.println("Area of a circle : "+ar);
System.out.println("Perimeter of a circle : "+pr);
}
catch(IOException ie){System.out.println(ie);}
}
}
OUTPUT:
>java area
Enter the value for radius
5
Area of a circle : 78.5
Perimeter of a circle : 31.400002
>java area
Enter the value for radius
6
Area of a circle : 113.04
Perimeter of a circle : 37.68
>java area
Enter the value for radius
7
Area of a circle : 153.86002
Perimeter of a circle : 43.960003
>java area
Enter the value for radius
8
Area of a circle : 200.96
Perimeter of a circle : 50.24
***********************
Using String Buffer class
***********************
import java.io.*;
class strbuf
{
public static void main(String ar[])
{
StringBuffer sb=new StringBuffer("jav");
System.out.println("Before insert:");
System.out.println(sb);
System.out.println("After insert:");
System.out.println(sb.insert(0,"Welcome to "));
System.out.println("After Append:");
System.out.println(sb.append('a'));
System.out.println("Length of the string:");
System.out.println(sb.length());
System.out.println("Change the char");
System.out.println(sb.substring(0,5));
System.out.println("After delete: "+sb.delete(0,5));
System.out.println("After replace: "+sb.replace(0,2,"Welcome"));
}
}
OUTPUT:
>java strbuf
Before insert:
jav
After insert:
Welcome to jav
After Append:
Welcome to java
Length of the string:
15
Change the char
Welco
After delete: me to java
After replace: Welcome to java
***************************
Usage of Random Class
***************************
import java.util.*;
import java.lang.*;
public class rand
{
public static void main(String arg[])
{
int e;
Random n=new Random();
int y[]=new int[6];
int a;
int no=Integer.parseInt(arg[0]);
for(int i=0;i<5;i++)
{
e=Math.abs(n.nextInt());
a=e%10;
y[i]=a;
System.out.println("a="+a);
}
for(int i=0;i<5;i++)
{
if(y[i]==no)
{
System.out.println("You are a Lucky Winner");
System.exit(0);
}
}
System.out.println("You are not a lucky winner");
}
}
OUTPUT:
>java rand 3
a=0
a=0
a=3
a=4
a=6
You are a Lucky Winner
>java rand 9
a=1
a=8
a=2
a=5
a=0
You are not a lucky winner
>java rand 2
a=9
a=0
a=8
a=9
a=2
You are a Lucky Winner
***************************
Implementation of Point Class
****************************
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.util.*;
class calend
{
public static void main(String args[])
{
int numdays=0;
Calendar c=Calendar.getInstance();
GregorianCalendar gc=new GregorianCalendar();
String month[ ]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
String m=Integer.toString(c.get(Calendar.MONTH)+1);
String d=Integer.toString(c.get(Calendar.DATE));
String y=Integer.toString(c.get(Calendar.YEAR));
System.out.println("Calendar for the Year - "+y);
System.out.print("Month of ");
System.out.println(month[gc.get(Calendar.MONTH)]);
System.out.print("Current Date: ");
System.out.println(m+"-"+d+"-"+y);
String h=Integer.toString(c.get(Calendar.HOUR));
String i=Integer.toString(c.get(Calendar.MINUTE));
String s=Integer.toString(c.get(Calendar.SECOND));
System.out.print("Current Time: ");
System.out.println(h+" : "+i+" : "+s);
System.out.println();
if(gc.isLeapYear(c.get(Calendar.YEAR)))
System.out.println("The current year is a leap year"+"\n");
else
System.out.println("The current year is not a leap year"+"\n");
switch(c.get(Calendar.MONTH)+1)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
numdays=31;
break;
}
case 4:
case 6:
case 9:
case 11:
{
numdays=30;
break;
}
case 2:
{
if(gc.isLeapYear(gc.get(Calendar.YEAR)))
numdays=29;
else
numdays=28;
break;
}
}
System.out.println("SU MO TU WE TH FR SA");
int dow=gc.get(Calendar.DAY_OF_WEEK);
int td=gc.get(Calendar.DATE);
int diff=(dow-(td%7));
if(diff<0)
{ diff=diff+7; }
switch(diff)
{
case 1:
{
System.out.print(" ");
break;
}
case 2:
{
System.out.print(" ");
break;
}
case 3:
{
System.out.print(" ");
break;
}
case 4:
{
System.out.print(" ");
break;
}
case 5:
{
System.out.print(" ");
break;
}
case 6:
{
System.out.print(" ");
break;
}
}
for(int day=1;day<=numdays;day++)
{
if(((day+diff)%7)==0)
{
if(day<10){System.out.println(" "+day+" ");}
else {System.out.println(day+" ");}
}
else
{
if(day<10){System.out.print(" "+day+" ");}
else{System.out.print(day+" ");}
}
}
}
}
OUTPUT:
>java calend
Calendar for the Year - 2008
Month of February
Current Date: 2-21-2008
Current Time: 5: 25: 27
The current year is a leap year
SU MO TU WE TH FR SA
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29
*********************************
String Manipulation using char Array
********************************
//Creating stringclass
class strmanip
{
public static void main(String args[])
{
System.out.println("Welcome 2 String Manipulation");
String s1="Welcome 2 ";
char ch[]={'j','a','v','a'};
String s2=new String(ch);
String s3="Live";
System.out.println("Length:"+s1.length());
System.out.println("Given String : "+s2);
System.out.println("Length:"+s2.length());
System.out.println("Concatenated String : "+s1.concat(s2));
StringBuffer s=new StringBuffer("Be Always");
System.out.println("Before Insertion:"+s);
s.insert(2," Happy");
System.out.println("After insertion:"+s);
System.out.println("Before Replace:"+s3);
String s4=s3.replace('v','f');
System.out.println("After Replace : "+s4);
String upper=s2.toUpperCase();
String lower=s2.toLowerCase();
System.out.println("Upper Case : "+upper);
System.out.println("Lower Case : "+lower);
}
}
OUTPUT:
>java strmanip
Welcome 2 String Manipulation
Length:10
Given String : java
Length:4
Concatenated String : Welcome 2 java
Before Insertion:Be Always
After insertion:Be Happy Always
Before Replace:Live
After Replace : Life
Upper Case : JAVA
Lower Case : java
*****************
Database creation
*****************
import java.util.*;
import java.sql.*;
public class database
{
public static void main(String a[]) throws SQLException
{
Connection c;
Statement s;
ResultSet r;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:Employee.dsn");
s=c.createStatement();
r=s.executeQuery("select * from Emp");
System.out.println("Number\tName\t\tEmail\t\tContact\n");
while(r.next())
{
System.out.println(r.getString("Emp_No")+
"\t"+r.getString("Emp_name")+"\t\t"+r.getString("Emp_email")+
"\t\t"+r.getString("Emp_tel"));
}
}
catch(Exception e){System.out.println("Table not found");}
}
}
OUTPUT:
>java database
import java.util.*;
import java.io.*;
class vect
{
public static void main(String a[])
{
String s;
int rad;
Vector v=new Vector(5,2);
System.out.println("Initial size:"+v.size());
System.out.println("Initial capacity:"+v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Double(5));
v.addElement(new Double(6));
v.addElement(new Integer(7));
v.addElement(new Float(8));
v.addElement(new Integer(9));
v.addElement(new Integer(10));
v.addElement(new Integer(11));
>java vect
Initial size:0
Initial capacity:5
After adding 6 elements The initial size is:6
The current capacity is:7
After adding 5 elements The initial size is:11
Current capacity:11
First Element : 1
After Deleting the first Element:
First Element : 2
Last Element : 11
Enter the integer value to be searched in your vector
3
Vector contains 3
Elements in vector
2
3
4
5.0
6.0
7
8.0
9
10
11
*****************************************************
Implementing thread based applications and Exception handling
*****************************************************
import java.util.*;
import java.lang.*;
public clicker(int p)
{
t=new Thread(this);
t.setPriority(p);
}
public void run()
{
while(running)
{
click++;
}
}
public void start()
{
t.start();
}
public void stop()
{
running=false;
}
}
class prior
{
public static void main(String a[])
{
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
clicker hi=new clicker(Thread.MAX_PRIORITY-2);
clicker lo=new clicker(Thread.MIN_PRIORITY+5);
lo.start();
hi.start();
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
System.out.println("Main Thread Interrupted");
}
lo.stop();
hi.stop();
try
{
hi.t.join();
lo.t.join();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException Caught");
}
System.out.println("Low_priority thread: "+lo.click);
System.out.println("High_priority thread: "+hi.click);
}
}
OUTPUT:
>java prior
Low_priority thread: 481470775
High_priority thread: 599937989
>java prior
Low_priority thread: 5990756
High_priority thread: 621690068
>java prior
Low_priority thread: 463016430
High_priority thread: 597236667
>java prior
Low_priority thread: 471146769
High_priority thread: 597067840
>java prior
Low_priority thread: 458286114
High_priority thread: 597245940
>java prior
Low_priority thread: 414278057
High_priority thread: 593878183
****************************
Application using Synchronization
****************************
import java.io.*;
class Q
{
int n;
boolean value=false;
synchronized int get()
{
if(!value)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("Got:"+n);
value=false;
notify();
return n;
}
synchronized void put(int n)
{
if(value)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
this.n=n;
value=true;
System.out.println("Put:"+n);
notify();
}
}
class producer implements Runnable
{
Q q;
producer(Q q)
{
this.q=q;
new Thread(this,"producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class consumer implements Runnable
{
Q q;
consumer(Q q)
{
this.q=q;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class THsynch
{
public static void main(String a[])
{
Q q=new Q();
new producer(q);
new consumer(q);
System.out.println("PRESS CTRL+C TO STOP.");
}
}
OUTPUT:
>java THsynch
PRESS CTRL+C TO STOP.
Put:0
Got:0
Put:1
Got:1
Put:2
Got:2
Put:3
Got:3
Put:4
Got:4
Put:5
Got:5
Put:6
Got:6
Put:7
Got:7
Put:8
Got:8
Put:9
Got:9
Put:10
Got:10
***********************************
Working with Frames and Various Controls
***********************************
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MenuDemo" width=600 height=600>
</applet>
*/
/*
<applet code="lay.java" width=300 height=300>
</applet>
*/
public class lay extends Applet implements ActionListener
{
Panel p1,p2;
Button b1,b2;
Button b[]=new Button[6];
String msg="";
public void init()
{
p1=new Panel();
p2=new Panel();
b1=new Button("First");
b2=new Button("Second");
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
b1.addActionListener(this);
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
p2.setLayout(new GridLayout(2,3));
for(int i=0;i<6;i++)
{
b[i]=new Button("Panel2: "+(i+1));
b[i].addActionListener(this);
p2.add(b[i]);
}
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ae)
{
String st=ae.getActionCommand();
if(st.equals("First"))
{
msg="you pressed first";
}
else if(st.equals("second"))
{
msg="u pressed second";
}
else
{
msg="u pressed"+ae.getActionCommand();
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,6,100);
}
}
OUTPUT:
**************************
Implementation of Graphics class
***************************
import java.awt.*;
import java.applet.*;
/*
<applet code="smile.java" width=750 height=450>
</applet>
*/
add(li,"center");
li1.addItem("red");
li1.addItem("blue");
li1.addItem("green");
li1.addItem("yellow");
li1.addItem("cyan");
li1.addItem("pink");
add(li1,"center");
li.addItemListener(this);
li1.addItemListener(this);
f.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent me)
{
Font fo=new Font("TimesNewRoman",Font.BOLD,10);
f.setFont(fo);
}
public void mouseExited(MouseEvent me)
{
Font fo1=new Font("CourierNew",Font.BOLD,12);
f.setFont(fo1);
}
});
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
String st="Currently selected in the first Choice :
"+li.getSelectedItem();
g.drawString(st,15,100);
String st1="Currently selected in the second Choice:
"+li1.getSelectedItem();
g.drawString(st1,15,120);
switch(li1.getSelectedIndex())
{
case 0:
setBackground(Color.red);
break;
case 1:
setBackground(Color.blue);
break;
case 2:
setBackground(Color.green);
break;
case 3:
setBackground(Color.yellow);
break;
case 4:
setBackground(Color.cyan);
break;
case 5:
setBackground(Color.pink);
break;
}
}
}
OUTPUT: