Lab PGM
Lab PGM
Lab PGM
SUBSTRING REMOVAL
import java.lang.*;
import java.io.*;
class Removestring
{
public static void main (String args[]) throws IOException
{
String s,rs,s1,s2;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\t\tSUBSTRING REMOVAL");
System.out.println("\t\t********* *******");
System.out.print("\nEnter the String : ");
s = new String(in.readLine());
System.out.print("Enter the SubString : ");
String ss = new String(in.readLine());
int i =s.indexOf(ss);
int len =ss.length();
int k=i+len;
s1=s.substring(0,i);
s2=s.substring(k);
rs=s1+s2;
System.out.println("Resultant String is : " + rs);
}
catch (Exception e)
{
System.out.println("SUBSTRING NOT FOUND");
}
}
}
1
/*
Compile: javac Removestring .java
Run: java Removestring
*/
OUTPUT:
SUBSTRING REMOVAL
********* *******
E:\java\bin\java>java Removestring
SUBSTRING REMOVAL
********* *******
2
// 2.AREA AND PERIMETER OF TRIANGLE
import java.io.*;
class Triangle
{
int width,height,length;
Triangle(int width1, int height1, int length1)
{
width=width1;
height=height1;
length=length1;
}
void area()
{
System.out.println("Area of the Triangle:" +(length*width)/2);
}
void perimeter()
{
System.out.println("Perimeter of the Triangle:" +(length+width+height));
}
public static void main(String args[])
{
try
{
int w,h,l;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Width:");
w=Integer.parseInt(ob.readLine());
System.out.print("Enter Height:");
h=Integer.parseInt(ob.readLine());
System.out.print("Enter Length:");
l=Integer.parseInt(ob.readLine());
Triangle tri=new Triangle(w,h,l);
tri.area();
tri.perimeter();
}
catch(Exception e)
{
System.out.println(e);
}
}
3
/*
OUTPUT:
Enter Width : 10
Enter Height : 10
Enter Length : 10
4
//3.ORDERING RANDOM NUMBERS
import java.util.Arrays;
import java.util.Random;
public class randomnos
{
public static void main(String arg[])
{
int i, j,temp;
Random no=new Random();
int a[]=new int[10];
System.out.println("RANDOM NUMBERS");
System.out.println("******** **********");
for(i=0;i<10;i++)
{
a[i]=no.nextInt(100);
System.out.println(a[i]+" ");
}
Arrays.sort(a);
System.out.println("Ascending order arrangement of random numbers");
for(i=0;i<10;i++)
System.out.println(a[i]+" ");
}
}
5
OUTPUT:
RANDOM NUMBERS
********* **********
43
43
59
78
47
77
51
68
53
10
6
//4.DISPLAYING DATE USING CALENDAR CLASS
import java.util.*;
import java.util.Calendar;
class calen
{
public static void main(String arg[])
{
String months[]={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
Calendar cal=Calendar.getInstance();
System.out.println("DATE:");
System.out.print(months[cal.get(Calendar.MONTH)]);
System.out.print(""+cal.get(Calendar.DATE)+"");
System.out.println("TIME:");
System.out.print(cal.get(Calendar.HOUR)+":");
System.out.print(cal.get(Calendar.MINUTE)+":");
System.out.print(cal.get(Calendar.SECOND)+"");
cal.set(Calendar.HOUR,10);
cal.set(Calendar.MINUTE,29);
cal.set(Calendar.SECOND,22);
System.out.println("Updated time:");
System.out.print(cal.get(Calendar.HOUR)+":");
System.out.print(cal.get(Calendar.MINUTE)+":");
System.out.print(cal.get(Calendar.SECOND)+"");
}
}
7
OUTPUT:
DATE:
Feb 28
TIME:
9:52:55
Updated time:
10:29:22
8
// 5. IMAGE MANIPULATION
import java.awt.*;
import java.applet.*;
import java.net.*;
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=imag height = 400 width=400>
</applet>*/
9
OUTPUT:
10
//6. STRING MANIPULATION
import java.lang.*;
import java.io.*;
class manip
{
public static void main(String arg[])
{
11
OUTPUT:
STRING MANIPULATION
******* *****************
Index of n in India = 1
12
//7.USAGE OF VECTOR CLASS
import java.lang.*;
import java.io.*;
import java.util.*;
class SampleVector
{
public static void main(String args[])
{
13
/* compile : javac SampleVector.java
OUTPUT:
C
C++
COBOL
VB
JAVA
14
//8.USAGE OF PACKAGE AND INTERFACE
package Mypackage;
{
return(num * num * num);
}
15
import Mypackage.MathFun;
import java.io.*;
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*Y);
}
}
class InterfaceTest
{
public static void main(String arg[])
{
MathFun mf = new MathFun();
System.out.println(Square = + mf(5));
System.out.println(Cube = + mf(3));
Rectangle rect=new Rectangle();
Circle cir=new Circle();
System.out.print("Area of the rectangle: "+rect.compute(10,20));
System.out.print("Area of the circle: "+cir.compute(10,0));
}
}
16
/* compile : javac InterfaceTest.java
Run : java InterfaceTest */
OUTPUT:
Square = 25
Cube = 27
Area of the rectangle: 200.0
Area of the circle: 0.0
17
// 9. THREAD BASED APPLICATION AND EXCEPTION HANDLING
import java.lang.*;
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1)
resume();
System.out.println("From Thread A:i=" +i);
}
18
{
sleep(2000);
}
catch(InterruptedException e){}
}
System.out.println("Exit from Thread C");
}
}
class ThreadMethod
{
public static void main(String args[])
{
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
*/
19
OUTPUT:
Start thread A
Start thread B
Start thread C
End of main thread
FromThread C: k=1
From Thread B: j=1
FromThread A: i=1
From Thread B: j=2
FromThread A: i=2
From Thread B: j=3
FromThread A: i=3
From Thread A: i=4
FromThread A: i=5
Exit from Thread A
From Thread B: j=4
FromThread B: j=5
Exit from Thread B
From Thread C: k=2
From Thread C: k=3
From Thread C: k=4
From Thread C: k=5
Exit from Thread C
20
//10. SYNCHRONIZED METHOD
import java.io.*;
import java.lang.Thread;
import java.lang.System;
class SyncThread extends Thread{
static String message[]={"I","LOVE","MY","MOM","VERY"," MUCH"};
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");
21
thread1.start();
thread2.start();
}
}
/*
COMPILE: javac SynchronizedThread.java
RUN: java SynchronizedThread
*/
OUTPUT:
Thread1:I
Thread1:LOVE
Thread1:MY
Thread1:MOM
Thread1:VERY
Thread1:MUCH
Thread2:I
Thread2:LOVE
Thread2:MY
Thread2:MOM
Thread2:VERY
Thread2:MUCH
22
// 11.COUNTING NO. OF CHARACTERS, WORDS & LINES
import java.io.*;
class FileRead
{
long word(String line)
{
int index =0;
long numwords = 0;
boolean prevwhitespace = true;
while(index < line.length())
{
char c = line.charAt(index++);
boolean currwhitespace = Character.isWhitespace(c);
if(prevwhitespace && !currwhitespace)
{
numwords++;
}
prevwhitespace = currwhitespace;
}
return numwords;
}
23
in.close();
}
catch(Exception e){
System.err.println("Error :" + e.getMessage());
}
}
}
OUTPUT:
Number of characters : 61
Number of words : 11
Number of Lines : 2
24
// 12. ELECTRICITY BILL
import java.io.*;
class Elect
{
public static void main(String arg[])
{
try
{
FileWriter fStream=new FileWriter("EBOutput.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("Enter the name:");
n=ob.readLine();
System.out.println("Enter the EB No:");
eb=Double.parseDouble(ob.readLine());
System.out.println("Enter the previous month reading:");
pr=Double.parseDouble(ob.readLine());
System.out.println("Enter the current month reading:");
cr=Double.parseDouble(ob.readLine());
c=cr-pr;
if(c<=100)
rate=1.5;
else if(c>100 && c<=200)
rate=2.00;
else if(c>200 && c<=250)
rate=2.50;
else if(c>250)
rate=4.00;
usage=c*rate;
System.out.println("Customer will be charged:" + usage);
out.write("Customer Name:" + n);
out.write("EB.no" + eb);
out.write("Previous reading"+pr);
out.write("Current Reading"+cr);
out.close();
}catch(Exception e){
System.err.println("Error"+e.getMessage());
}
}
25
}
OUTPUT:
26
//13.TELEPHONE BILLING
import java.io.*;
class Phone
{
public static void main(String arg[])
{
try
{
FileWriter fStream=new FileWriter("out.txt",true);
BufferedWriter out=new BufferedWriter(fStream);
String N;
double calls,ccost,ph;
double rate=0.0;
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Customer Name:");
N=ob.readLine();
System.out.println("Enter the phone.no:");
ph=Double.parseDouble(ob.readLine());
System.out.println("Enter the Tarrif value:");
ccost=Double.parseDouble(ob.readLine());
System.out.println("Enter the no.of calls:");
calls=Double.parseDouble(ob.readLine());
rate = calls*ccost+200;
System.out.println("Customers will be charged:" + rate);
out.write("Name:"+N);
out.write("eb.no"+ph);
out.write("Tarrif value"+ccost);
out.write("no.of calls"+calls);
out.close();
}catch(Exception e){
System.err.println("Error"+e.getMessage());
}
}
}
27
OUTPUT:
28
// 17. WORKING WITH FRAMES AND CONTROLS
import java.awt.*;
import java.awt.event.*;
29
else if(ch3.getState())
{
l.removeAll();
l.add("AVADI");
l.add("ANNA NAGAR");
l.add("ADYAR");
}
}
public static void main(String args[])
{
FramCtrl ob=new FramCtrl();
}
}
30
OUTPUT:
31
// 20.WORKING WITH DIALOG BOX AND MENUS
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet code=MenuDemo height=4000 width=4000></applet>
menudialog(String t)
{
super(t);
MenuBar mbar=new MenuBar();
Menu file=new Menu("File");
MenuItem it1,it2;
file.add(it1=new MenuItem("Open"));
file.add(it2=new MenuItem("Close"));
mbar.add(file);
setMenuBar(mbar);
it1.addActionListener(this);
it2.addActionListener(this);
}
32
{
Frame f;
public void init()
{
f=new menudialog("MenuDialog");
f.setSize(200,100);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
}
33
OUTPUT:
34
//21. IMPLEMENTATION OF COLORS AND FONT CLASS
import java.awt.*;
import java.applet.Applet;
35
OUTPUT:
36
//18. SHAPES USING GRAPHICAL STATEMENTS
import java.applet.*;
import java.awt.*;
37
OUTPUT:
38
//19. WORKING WITH PANELS AND LAYOUT
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
b1.addActionListener(this);
b2.addActionListener(this);
addMouseListener(this);
}
39
{
c.next(MainPan);
}
40
OUTPUT:
41
//14. SIMPLE CALCULATOR
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Simcal 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();
}
42
public Insets getInsets()
{
return new Insets(2,2,2,2);
}
public void actionPerformed(ActionEvent evt)
{
double X,Y;
try
{
String XStr=XInput.getText();
X=Double.parseDouble(XStr);
}
catch(NumberFormatException e)
{
answer.setText("illegal data for x");
return;
}
try
{
String YStr=YInput.getText();
Y=Double.parseDouble(YStr);
}
catch(NumberFormatException e)
{
answer.setText("illegal 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("Cant divide by Zero!!!!!");
else
answer.setText("Division :"+(X/Y));
}
}
}
/*<applet code="Simcal" width=500 height=500></applet>*/
43
OUTPUT:
44
//15. USAGE OF 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;
L1.setText("Hai Friends");
if(s == "Java")
L1.setText("It is an Internet based programming Language");
}
}
45
OUTPUT:
46
//16.USAGE OF RADIO BUTTONS, CHECKBOX AND CHOICE LIST
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowListener;
st1=new Checkbox("Bold");
st2=new Checkbox("Italic");
add(lbl);
add(lfont);
add(lsize);
add(lstyle);
add(ft1);
add(ft2);
47
add(st1);
add(st2);
add(itemchoice);
}
if(st1.getState())
str1=st1.getLabel();
if(st2.getState())
str1=str1 + st2.getLabel();
lbl.setText("U had chosen Font : " + str + " Style : " + str1 + " Size : " +
itemchoice.getSelectedItem());
}
}
48
OUTPUT:
49