//Perimeter & Area of Triangle Using Stream Class
import java.io.*;
class Area
public static void main(String [] args)throws IOException
int i=0,b=0,h=0;
DataInputStream k=new DataInputStream(System.in);
System.out.println("ENTER LENGTH OF TRIANGLE:");
l=Integer.parseInt(k.readLine());
System.out.println("ENTER BREADTH OF TRIANGLE:");
b=Integer.parseInt(k.readLine());
System.out.println("ENTER HEIGHT OF TRIANGLE:");
h=Integer.parseInt(k.readLine());
int area=(b*h)/2;
System.out.println("AREA OF TRIANGLE:"+area);
int perimeter=l*b*h;
System.out.println("PERIMETER OF TRIANGLE:"+perimeter);
/*
COMPILE:javac Area.java
RUN:java Area
*/
//Substring Removal
class SubstringRemoval
public static void main(String[] args)
StringBuffer sb=new StringBuffer("hello world");
System.out.println("orginal string:"+sb);
String substr="hi";
sb.replace(0,5,"hi");
System.out.println("substring:"+substr);
System.out.println("Replaced string:"+sb);
/*
COMPILE:javac SubstringRemoval.java
RUN:java SubstringRemoval
*/
//RANDOM NUMBERS
import java.util.Arrays;
import java.util.Random;
class RandomNumbers
public static void main(String args[])
int i,j,temp;
Random n= new Random();
int a[]=new int[5];
System.out.println("Random Numbers Are:");
for(i=0;i<5;i++)
a[i]=n.nextInt(100);
System.out.println("\n"+a[i]);
Arrays.sort(a);
System.out.println("\n Ascending order of random numbers :");
for(i=0;i<5;i++)
System.out.println("\n"+a[i]);
}
/*COMPILE:javac RandomNumbers.java
RUN:java RandomNumbers */
//Calendar class
import java.util.*;
class CalendarExample
public static void main(String args[])
Calendar cal=Calendar.geInstance();
Date d=new Date();
System.out.println("current date:"+d);
System.out.println("Before manipulation");
System.out.println("Date:"+cal.get(Calendar.DATE));
System.out.println("Hour:"+cal.get(Calendar.HOUR));
System.out.println("Minute:"+cal.get(Calendar.MINUTE));
System.out.println("Second:"+cal.get(Calendar.SECOND));
System.out.println("After manipulation");
cal.add(Calendar.DATE,2);
cal.add(Calendar.HOUR,2);
cal.add(Calendar.MINUTE,2);
cal.add(Calendar.SECOND,2);
System.out.println("Date:"+cal.get(Calendar.DATE));
System.out.println("Hour:"+cal.get(Calendar.HOUR));
System.out.println("Minute:"+cal.get(Calendar.MINUTE));
System.out.println("Second:"+cal.get(Calendar.SECOND));
/*
COMPILE:javac CalendarExample.java
RUN:java CalendarExample
*/
//Image Manipulation
import java.awt.*;
import java.applet.*;
import java.net.*;
public class Image extends Applet
public void paint(Graphics g)
g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.drawOval(85,100,30,20);
g.drawOval(160,92,15,30);
g.drawOval(25,92,15,30);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.fillArc(60,125,80,40,180,180);
} }
/*<applet code=Image height=400 width=400></applet>*/
/*
COMPILE:javac Image.java
RUN:AppletViewer Image.java*/
//String Manipulation
import java.lang.*;
import java.io.*;
class StringManipulation
public static void main(String args[])
char ss1[]={'w','e','l','c','o','m','e'};
char ss2[]={'g','o','o','d','m','o','r','n','i','n','g'};
String s1=new String(ss1);
String s2=new String(ss2);
System.out.println("\n\n\tSTRING MANIPULATION");
System.out.println("***************************");
System.out.println("\n FIRST STRING:"+s1+"\tSECOND STRING:"+s2);
String s3=s1.concat(s2)
System.out.println("\n\nTHE CONCATINATED STRING IS:"+s3);
System.out.println();
String s4=s3.substring(0,1);
System.out.println("THE SUBSTRING IS:"+s4);
System.out.println();
if(s1.equals(s2))
System.out.println("THE STRING ARE EQUAL");
System.out.println();
else
System.out.println("THE STRING ARE NOT EQUAL");
System.out.println();
String s5="India".replace('i','y');
System.out.println("AFTER REPLACING INDIA i WITH y IS:"+s5);
System.out.println();
String s6="FLOWER";
System.out.println("CONVERSION OF LOWERCASE:"+s6.toLowerCase());
System.out.println();
System.out.println("Index of n in india ="+s5.indexOf('n'));
}
/*COMPILE:javac StringManipulation.java
RUN:java StringManipulation
*/
//Data base for E-mail
import java.sql.*;
public class email
public static void main(String args[])
Connection c;
Statement st;
try
Class.forName("sun.jdbc.odbc JdbcOdbcDriver");
catch(java.lang.ClassNotFoundException e)
System.out.println(e.getMessage());
try
{
c=DriverManager.getConnection("jdbc:odbc:sucess");
st=c.createStatement();
st.executeUpdate(insert into emailtab values('saraswathi','[email protected]')");
st.executeUpdate(insert into emailtab values('laxmi','[email protected]')");
Resultset rs=st.executeQuery(select*from emailtab");
System.out.println("name and email id");
while(rs.next())
String s=rs.getString("pname");
String f=rs.getString("email id");
System.out.println(s+""+f);
st.close();
c.close();
}catch(SQLException ex)
System.out.println("SQL EXCEPTION"+ex.getMessage());
/*COMPILE:javac email.java
RUN:java email
*/
//usage of vector class
import java.util.*;
class VectorExample
public static void main(String args[])
Vector v=new Vector();
v.add("1");
v.add("2");
v.add("3");
System.out.println("getting elements of vector");
System.out.println(v.get(0));
System.out.println(v.get(1));
System.out.println(v.get(2));
}
/*COMPILE:javac VectorExample.java
RUN:java VectorExample
*/
//Interface and packages
interface Area
final static float pi=3.14F;
float compute(float x, float y);
class Rectangle implements Area
public float compute(float x,float y)
return(x*y);
class Circle implements Area
public float compute(float x,float y)
{
return(pi*x*x);
class InterfaceTest
public static void main(String args[])
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area;
area=rect;
System.out.println("Area of rectangle:"+area.compute(10,20));
area=cir;
System.out.println("Area of circle:"+area.compute(10,0));
/*COMPILE:javac InterfaceTest.java
RUN:java InterfaceTest
*/
//Thread Based Application
import java.lang.*;
class A extends Thread
public void run()
for(int i=0;i<=;i++)
if(i==1)
resume();
System.out.println("From thread A:i="+i);
System.out.println(exit from thread A");
class B extends Thread
{
public void run()
for(int j=1;j<=5;j++)
System.out.println("From thread B:j="+j);
if(j==6)
try
wait();
catch(InterruptedException e){}
System.out.println("Exit from thread B");
class C extends Thread
public void run()
for(int k=1;k<=5;k++)
{
System.out.println("From thread C:k="+k);
if(k==1)
try
sleep(2000);
catch(InterruptedException e){}
System.out.println("Exit from thread C");
} }
class ThreadMethod
public static void main(String arga[])
A threadA=new A();
B threadB=new B();
C threadC=new C();
System.out.println("Start thread A");
threadA.start();
System.out.println("Start thread B");
threadB.start();
System.out.println("Start thread C");
threadC.start();
System.out.println("End of main thread");
} }
/*COMPILE:javac ThreadMethod.java
RUN:java ThreadMethod
*/
//Synchronization using threads
import java.io.*;
import java.lang.Thread.*;
import java.lang.System;
class SyncThread extends Thread
static String message[]{"java","is","a","programming","language"};
public SyncThread(String id)
super(id);
public void run()
Sync.displayList(getName(),message);
void waiting()
{
try
sleep(3000);
catch(InterruptedException e)
System.out.println("interrupted");
class Sync
public static Synchronized void displayList(String name,String list[])
for(int i=0;i<list.length;i++)
SyncThread thread=(SyncThread)Thread.currentThread();
thread.waiting();
System.out.println(name+":"+list[i]);
}
}
class SynchronizedThread
public static void main(String args[])
SyncThread thread1=new SyncThread("Thread1");
SyncThread thread2=new SyncThread("Thread2");
thread1.start();
thread2.start();
/*COMPILE:javac SynchronizedThread.java
RUN:java SynchronizedThread
*/
//Creating file to store the string and to count the number of lines
import java.io.*;
public class FileRead
long word(String line)
int index=0;
long numwords=0;
boolean prevwhitespaces=true;
while(index<line.length())
char c=line.charAt(index++);
boolean currwhitespace=Character.isWhitespace(c);
if(prevwhitespace&& !currwhitespace)
{
numwords++;
prevwhitespace=currwhitespace;
return numwords;
public static void main(String args[])throws IOException{
int l=0,count=0;
long w=0,temp=0,cc=0;
String strline;
char c;
String name=null;
int no=0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter String");
name=in.readLine();
File fin=new File("E:\\filecount.txt");
Writer writer=new BufferedWriter(new FileWriter(fin));
writer.write(name+"\n");
writer.close();
FileInputStream fstream=new FileInputStream("E:\\filecount.txt");
DataInputStream din=new DataInputStream(fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(din));
FileRead o=new FileRead();
strline=br.readLine();
while((strline!=null))
temp=0.word(strline)
w+=temp;
cc+=strline.length();
l++;
strline=br.readLine();
System.out.println("number of characters:"+cc);
System.out.println("number of words"+w);
System.out.println("number of lines:"+l);
/*compile:javac FileRead.java
run:java FileRead
*/
//Electric bill
import java.io.*;
class ElectricBill
public static void main(String args[])
try
FileWriter fstream=new FileWriter("out.txt",true)'
BufferedWriter out=new BufferedWriter(fstream);
String N;
double EB,PR,CR;
double c,rate=0.0;
double usage=0.0;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\tEnter the name:");
N=ob.readLine();
System.out.println("\n\n\tEnter the EB number:");
EB=Double.parseDouble(ob.readLine());
System.out.println("\n\n\tEnter the previous month reading:");
PR=Double.parseDouble(ob.readLine());
System.out.println("\n\n\tEnter the current month reading:");
CR=Double.parseDouble(ob.readLine());
c=CR-PR;
if(c<=100)
rate=1.5;
elseif(c>100&&c<=200)
rate=2.00;
elseif(c>200&&c<=250)
rate=4.00;
usage=c*rate;
System.out.prinln("Customer bill be charged:"+usage);
out.write("Name:"+N+"\n");
out.write("EB Number:"+EB);
out.write("previous reading"+PR);
out.write("current readinng:"+CR);
out.close();
catch(Exception e)
System.out.println("error:"+e.getMessage());
} } }
/*compile : javac ElectricBill.java
Run : java ElectricBill */
//Telephone Bill
import java.io.*;
class PhoneBill
public static void main(String args[])
try
FileWriter fstream=new FileWriter("out.txt",true);
BufferedWriter out=new BufferedWriter(fstream);
String N;
double CALLS,CCOST,PH;
double RATE=0.0;
double usage=0.0;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\n\tEnter the name:");
N=ob.readLine();
System.out.println("\n\n\tEnter the phone number:");
PH=Double.parseDouble(ob.readLine());
System.out.println("\n\n\tEnter the tariffvalue:");
CCOST=Double.parseDouble(ob.readLine());
System.out.println("\n\n\tEnter the number of calls:");
CALLS=Double.parseDouble(ob.readLine());
RATE=CALLS*CCOST;
System.out.println("Customer bill be charged:"+RATE);
out.write("Name:"+N+"\n");
out.write("Phone Number:"+PH);
out.write("Tariff Value:"+CCOST);
out.write("Number of calls:"+CALLS);
out.close();
catch(Exception e)
System.out.println("error:"+e.getMessage());
}
}
/*compile:javac PhoneBill.java
run:java PhoneBill
*/
//WORKING WITH FRAMES AND VARIOUS CONTROL
import java.awt.*;
import java.awt.event.*;
class Check12 extends Frame implements ItemListener
Checkbox cg1,cg2,cg3;
CheckboxGroup chg;
List l;
Check12()
setSize(1000,1000);
setLayout(new FlowLayout());
chg=new CheckboxGroup();
cg1=new Checkbox("COUNTRY:", chg,false);
cg2=new Checkbox("STATE:",chg,false);
cg3=new Checkbox("CITY:",chg,false);
add(cg1);
add(cg2);
add(cg3);
l=new List();
add(l);
cg1.addItemListener(this);
cg2.addItemListener(this);
cg3.addItemListener(this);
cg1.addItemListener(this);
setVisible(true);
public void itemStateChanged(ItemEvent ie)
if(cg1.getState())
l.removeAll();
l.add("india");
l.add("switzerland");
l.add("america");
}
else if(cg2.getState())
l.removeAll();
l.add("andhra pradesh");
l.add("tamil nadu");
l.add("kerala");
else if(cg3.getState())
l.removeAll();
l.add("anna nagar");
l.add("adayar");
l.add("avadi");
public static void main(String args[])
Check12 k=new Check12();
/*compile : javac Check12.java
Run : java Check12 */
//Color and Font
import java.awt.*;
public class applet extends java.Applet
public void paint(Graphics g)
Font f=new Font("Times Roman",Font.BOLD,20);
Font f1=new Font("courier",Font.ITALIC,20);
Font f2=new Font("helvetica",Font.PLAIN,20);
g.setColor(color.gray);
g.setFont(f);
g.drawString("Be Happy .Be Hopeful",30,30);
g.setColor(color.blue);
g.setFont(f1);
g.drawString("Be Happy .Be Hopeful",30,70);
g.setColor(color.pink);
g.setFont(f2);
g.drawString("Be Happy .Be Hopeful",30,110);
} }
//<applet code=applet height=500 width=500></applet>
/*Compile:javac applet.java
Run : AppletViewer applet.html */
// Drawing various shapes
import java.applet.*;
import java.awt.*;
public class shapes extends Applet
public void paint(Graphics g)
g.setColor(Color.orange);
g.fillRect(250,80,200,50);
g.setColor(Color.green);
g.fillRect(250,180,200,50);
g.setColor(Color.black);
g.drawOval(320,130,50,50);
/*<applet code=shapes width=500 height=500></applet>*/
/* compile:javac shapes.java
Run : java shapes */
// Panels and Layout
import java.awt.*;
import java.awt.event.MouseListener.*;
import java.awt.event.ActionListener.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=CardDemo1 height=500 width=500></applet>*/
public class CardDemo1 extends Applet implements
MouseListener,ActionListener
Checkbox dos,lan,nt,win95;
Button b1,b2;
Panel MainPan,DosPan,WinPan;
CardLayout clayout;
public void init()
b1=new Button("DosBased");
b2=new Button("WindowBased");
add(b1);
add(b2);
clayout=new CardLayout();
MainPan=new Panel();
MainPan.setLayout(clayout);
dos=new Checkbox("Ms-Dos");
lan=new Checkbox("Novell Netware");
DosPan=new Panel();
DosPan.add(dos);
DosPan.add(lan);
nt=new Checkbox("Nt Server");
win95=new Checkbox("windows95");
WinPan=new Panel();
WinPan.add(nt);
WinPan.add(win95);
MainPan.add(DosPan,"DosBased");
MainPan.add(WinPan,"Windows Based");
add(MainPan);
b1.addActionListener(this);
b2.addActionListener(this);
addMouseListener(this);
public void mousePressed(MouseEvent me)
clayout.next(MainPan);
public void mouseRelesed(MouseEvent me){}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseListener(MouseEvent me){}
public void actionPerformed(ActionEvent ae)
if(ae.getSource()==b1)
{
clayout.next(MainPan);
clayout.show(DosPan,"DosBased");
else
clayout.next(MainPan);
clayout.show(WinPan,"Windows Based");
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
/*Compile:javac CardDemo.java
Run : appletviewer CardDemo */
//SIMPLE CALCULATOR
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleCalculator extends JApplet implements ActionListener
JTextField xInput,yInput;
JLabel answer;
public void init()
Container content=getContentPane();
xInput=new JTextField("0");
xInput.setBackground(Color.white);
yInput=new JTextField("0");
yInput.setBackground(Color.white);
JPanel xPanel=new JPanel();
xPanel.setLayout(new BorderLayout());
xPanel.add(new Label("X="),BorderLayout.WEST);
xPanel.add(xInput,BorderLayout.CENTER);
JPanel yPanel=new JPanel();
yPanel.setLayout(new BorderLayout());
yPanel.add(new Label("Y="),BorderLayout.WEST);
yPanel.add(yInput,BorderLayout.CENTER);
JPanel buttonPanel=new JPanel();
buttonPanel.setLayout(new GridLayout(1,4));
JButton plus=new JButton("+");
plus.addActionListener(this);
buttonPanel.add(plus);
JButton minus=new JButton("-");
minus.addActionListener(this);
buttonPanel.add(minus);
JButton times=new JButton("*");
times.addActionListener(this);
buttonPanel.add(times);
JButton divide=new JButton("/");
divide.addActionListener(this);
buttonPanel.add(divide);
answer=new JLabel("result",JLabel.CENTER);
content.setLayout(new GridLayout(4,1,2,2));
content.add(xPanel);
content.add(yPanel);
content.add(buttonPanel);
content.add(answer);
xInput.requestFocus();
public Insets getInsets()
return new Insets(2,2,2,2);
public void actionPerformed(ActionEvent evt)
double x,y;
try
String xstr=xInput.getText();
x=Integer.parseInt(xstr);
catch(NumberFormatException e)
answer.setText("illigal data forX");
return;
try
String ystr=xInput.getText();
y=Double.parseDouble(ystr);
catch(NumberFormatException e)
answer.setText("illigal data for Y");
return;
String op=evt.getActionCommand();
if(op.equals("+"))
answer.setText("Addition:"+(x+y));
else if(op.equals("-"))
answer.setText("Subtraction:"+(x-y));
else if(op.equals("*"))
answer.setText("multiplication:"+(x*y));
else if(op.equals("/"))
if(y==0)
answer.setText("can't divide by zero");
else
answer.setText("Division:"+(x/y));
} }
/*<applet code=SimpleCalculaltor width=500 height=500></applet>*/
/*Compile : javac SimpleCalculator.java
Run : appletviewer SimpleCalculator.html
*/
//BUTTONS AND LABELS
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code=ButActDemo height=500 width=500></applet>*/
public class ButActDemo extends Applet implements ActionListener
Button b1,b2;
Label l1;
public void init()
{
setLayout(null);
b1=new Button("push");
b2=new Button("java");
l1=new Label();
add(l1);
add(b1);
add(b2);
l1.setBounds(100,100,350,20);
b1.setBounds(10,10,50,20);
b2.setBounds(40,40,50,20);
b1.addActionListener(this);
b2.addActionListener(this);
public void actionPerformed(ActionEvent ae)
String s;
s=ae.getActionCommand();
if(s=="push")
l1.setText("I like java");
if(s=="java")
l1.setText("it os internet based programming language");
}
}
/*compile:javac ButActDemo.java
Run : appletviewer ButAvtDemo
*/
//RADIO BUTTONS
import java.applet.Applet.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowListener;
public class RadioCheck extends Applet implements ItemListener
Choice itemChoice;
String s[]={"12","15","18","19","20","23","35","40"};
Checkbox ft1,ft2,st1,st2;
Label lbl,lblsize,lblfont,lblstyle;
CheckboxGroup FontGroup;
public void init()
setSize(600,200);
setLayout(new GridLayout(6,2));
lblfont=new Label("fonts"Label.LEFT);
lblStyle=new Label("style"Label.RIGHT);
lblSize=new Label("SIZE"Label.LEFT);
lbl=new Label("select Fontstyle and Size");
lbl.setFont(new Font("san serif",Font PLAIN,14));
lbl.setForeground(Color.red);
lbl.setAlignment(Label.CENTER);
st1=new Checkbox("bold");
st2=new Checkbox("italic");
FontGroup=new CheckboxGroup();
ft1=new Checkbox("Times New Roman",FontGroup,true);
ft2=new CheckBox("Arial",FontGroup,false);
itemChoice=new Choice();
for(int i=0;i<=s.length;i++)
itemChoice.add(s[i]);
itemChoice.addItemListener(this);
add(lblfont);
add(lblSize);
add(ft1);
add(st1);
add(ft2);
add(st2);
add(lblStyle);
add(itemChoice);
add(lbl);
public void itemStateChanged(ItemEvent e)
String str,strs;
strs="";
if(ft1.getState())
str=ft1.getLabel();
else
str=ft2.getLabel();
if(st1.getState())
strs=st1.getLabel();
if(st2.getState())
strs=strs+st2.getLabel();
lbl.setText("U had choosen font "+strs+"size"+itemChoice.getSelectedItem());
/*<applet code=RadioCheck width=500 height=500>
</applet>
*/
/*compile:javac RadioCheck.java
run:appletviewer RadioCheck.html
*/