48
Dept of Computer Application, QMC - Medavakkam STRING MANIPULATION USING CHARACTER ARRAY import java.io.*; import java.util.*; class string { public static void main(String args[])throws IOException { int num=0,v=0,co=0,b=0,w=0,s=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the string"); String a=br.readLine(); a=a.trim(); int l=a.length(); char c[]=new char[l]; a.getChars(0,l,c,0); for(int i=0;i<l;i++) { if(Character.isDigit(c[i])) { num=num+1; } else { switch(c[i]) { case 'a':case 'A': case 'e':case 'E': case 'i':case 'I': case 'o':case 'O': case 'u':case 'U': v++;break; case ' ':b++;++w;break; case '.':s++;break; default:co++;break; }

Java Lab Record by Prof Manikandan

Embed Size (px)

DESCRIPTION

This is java lab record published by prof.Manikandan Quaide Milleth College Medavakkam, Chennai.

Citation preview

Page 1: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

STRING MANIPULATION USING CHARACTER ARRAY

import java.io.*;

import java.util.*;

class string

{

public static void main(String args[])throws IOException

{

int num=0,v=0,co=0,b=0,w=0,s=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the string");

String a=br.readLine();

a=a.trim();

int l=a.length();

char c[]=new char[l];

a.getChars(0,l,c,0);

for(int i=0;i<l;i++)

{

if(Character.isDigit(c[i]))

{

num=num+1;

}

else

{

switch(c[i])

{

case 'a':case 'A':

case 'e':case 'E':

case 'i':case 'I':

case 'o':case 'O':

case 'u':case 'U':

v++;break;

case ' ':b++;++w;break;

case '.':s++;break;

default:co++;break;

}

Page 2: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

}

}

if(l==0)

System.out.println("\n\t words:"+w);

else

System.out.println("\n\t blankspaces"+b);

w=w+1;

System.out.println("\n\t word="+w);

System.out.println("\n\t sentence="+s);

System.out.println("\n\t volwels="+v);

System.out.println("\n\t number="+num);

System.out.println("\n\t consonants="+co);

System.out.println("\n\t length="+l);

System.out.println("\n\t the fifth character=");

System.out.println( a.charAt(5));

System.out.println("\n\t Upper case="+a.toUpperCase());

System.out.println("\n\t Lower case="+a.toLowerCase());

}

}

Page 3: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Enter the string

THIS IS II BCA STUDENTS

blankspaces4

word=5

sentence=0

volwels=7

number=0

consonants=12

length=23

the fifth character= I

Upper case=THIS IS II BCA STUDENTS

Lower case=this is ii bca students

Page 4: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

SUBSTRING REMOVAL USING STRINGBUFFER CLASS

import java.io.*;

class substring

{

public static void main(String args[]) throws IOException

{

BufferedReader br=new BufferedReader (new InputStreamReader(System.in));

int c=0,i;

System.out.print("\n\t Enter the string:");

String s=br.readLine();

StringBuffer s1=new StringBuffer(s);

System.out.print("\n\t Enter the substring:");

String search=br.readLine();

String res=" ";

System.out.println("\n\t The original string is="+s1);

do

{

i=s.indexOf(search);

if(i==-1)

break;

res=res+s.substring(0,i);

res=res+s.substring(i+search.length());

s=res;

res=" ";

c=c+1;

}

while(i!=-1);

if(c>0)

{

System.out.println("\n\t After removal:"+s);

}

else {

System.out.println("\n\t String"+search+"not found");

} } }

Page 5: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Enter the string: This program developed by II BCA

Enter the substring: developed

The original string is= This program developed by II BCA

After removal: This program by II BCA

Page 6: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

AREA AND PERIMETER OF A TRIANGLE

import java.io.*;

class triangle

{

int s1,s2,s3,b,h,pmtr=0,area=0;

void get()throws IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("\n\n\n ENTER THE VALUE OF FIRST SIDE:");

s1=Integer.parseInt(br.readLine());

System.out.println("\n\n\n ENTER THE VALUE OF SECONT SIDE:");

s2=Integer.parseInt(br.readLine());

System.out.println("\n\n\n ENTER THE VALUE OF THIRED SIDE:");

s3=Integer.parseInt(br.readLine());

System.out.println("\n\n\n ENTER THE VALUE OF BASE:");

b=Integer.parseInt(br.readLine());

System.out.println("\n\n\n ENTER THE VALUE OF HEIGHT:");

h=Integer.parseInt(br.readLine());

}

void calculate() {

pmtr=s1+s2+s3;

area=(b*h)/2; }

void display()

{

System.out.println("\n\t perimater of triangle="+pmtr);

System.out.println("\n\t area of triangle="+area);

} }

class process

{

public static void main(String Fr[])throws IOException

{

triangle t=new triangle();

t.get();

t.calculate();

t.display();

} }

Page 7: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

ENTER THE VALUE OF FIRST SIDE: 1

ENTER THE VALUE OF SECONT SIDE: 2

ENTER THE VALUE OF THIRED SIDE: 3

ENTER THE VALUE OF BASE: 4

ENTER THE VALUE OF HEIGHT: 5

perimater of triangle=6

area of triangle=10

Press any key to continue . . .

Page 8: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

ORDERING NUMBERS USING RANDOM CLASS

import java.util.*;

public class prog1

{

Random r=new Random();

int i,j;

int a[]=new int[100];

public void get_no()

{

System.out.println("\n\tGenerate Random Numbers");

for(i=0;i<5;i++)

{

a[i]=r.nextInt(10);

System.out.println("\n\t\t"+a[i]);

}

}

public void sort()

{

int temp;

for(i=0;i<5;i++)

{

for(j=i+1;j<5;j++)

{

if(a[i]<a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

}

public void show()

{

Page 9: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

System.out.println("Descending order");

for(i=0;i<5;i++)

System.out.println("\t\t"+a[i]);

System.out.println("Asdending order");

for(i=4;i>=0;i--)

System.out.println("\t\t"+a[i]);

}

public static void main(String arg[])

{

System.out.println("output");

prog1 r1=new prog1();

r1.get_no();

r1.sort();

r1.show();

}

}

Page 10: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT

Generate Random Numbers

4

4

0

2

9

Descending order

9

4

4

2

0

Ascending order

0

2

4

4

9

Page 11: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Implementation of Vector Class

import java.io.*;

import java.util.*;

public class test2

{

public static void main(String args[])

{

Vector v = new Vector();

v.add("BCA");

v.add("BBA");

v.add("B.COM");

v.add("M.COM");

v.add("BSC");

v.setSize(3);

System.out.println("Vector elements after setting size to 3");

for(int index=0; index <v.size(); index++)

System.out.println(v.get(index));

v.setSize(5);

System.out.println("Vector elements after setting size to 5");

for(int index=0; index <v.size(); index++)

System.out.println(v.get(index));

}

}

Page 12: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Vector elements after setting size to 3 :

BCA

BBA

B.COM

Vector elements after setting size to 5 :

BCA

BBA

B.COM

null

null

Page 13: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

IMPLEMENTATION OF CALENDAR CLASS

import java.util.*;

import java.io.*;

class prog1

{

public static void main(String arg[])

{

String months[]={"jan","feb","mar","apr","may","jun","july","aug","sep","oct","nov","dec"};

System.out.println("\t \tCALENDAR PROGRAM");

Calendar c=Calendar.getInstance();

GregorianCalendar gc= new GregorianCalendar();

System.out.print("\n\t Date->");

System.out.print(months [c.get(Calendar.MONTH)]);

System.out.print("-"+c.get(Calendar.DATE));

System.out.print("-"+c.get(Calendar.YEAR));

System.out.print("\n\t Time->");

System.out.print(c.get(Calendar.HOUR));

System.out.print(":"+c.get(Calendar.MINUTE));

System.out.print(":"+c.get(Calendar.SECOND));

c.set(Calendar.HOUR,11);

c.set(Calendar.MINUTE,59);

c.set(Calendar.SECOND,59);

System.out.print("\n\t Updated time is->");

System.out.print(c.get(Calendar.HOUR)+":");

System.out.print(c.get(Calendar.MINUTE)+":");

System.out.print(c.get(Calendar.SECOND));

if(gc.isLeapYear(c.get(Calendar.YEAR)))

System.out.println("\n\t"+c.get(Calendar.YEAR)+"is a leap year");

else

System.out.println("\n\t"+c.get(Calendar.YEAR)+"is not leap year");

}

}

Page 14: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT

CALENDAR PROGRAM

Date->feb-23-2013

Time->0:48:12

Updated time is->11:59:59

2013 is not leap year

Page 15: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

INTERFACES AND PACKAGES

Package Program: File Name:Pack\student.java

package pack;

public class student

{

int no;

String name;

public void get(int n,String s)

{

no=n;

name=s;

}

public void display()

{

System.out.println("\n\t The student number is:"+no);

System.out.println("\n\t The student name is:"+name);

}

public interface marks

{

public void gets(int no1,int no2,int no3);

public void view();

}

}

-----------------------

FileName: Intpackdemo.java

import java.io.*;

import pack.*;

class intpacdemo extends student implements marks

{

int m1,m2,m3,tot,avg;

public void gets(int no1,int no2,int no3)

{

m1=no1; m2=no2; m3=no3; tot=no1+no2+no3; avg=tot/3;

Page 16: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

}

public void view()

{

System.out.println("\n\t marks1="+m1);

System.out.println("\n\t marks2="+m2);

System.out.println("\n\t marks3="+m3);

System.out.println("\n\t Total="+tot);

System.out.println("\n\t Avarge="+avg);

}

public static void main(String arg[])throws IOException

{

System.out.println("\t\t\t INTERFACES AND PACKAGES");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("\n\t Enter the student number:");

int no=Integer.parseInt(br.readLine());

System.out.println("\n\t Enter the student name:");

String name=br.readLine();

System.out.println("\n\t Enter the student mark1:");

int m1=Integer.parseInt(br.readLine());

System.out.println("\n\t Enter the student mark2:");

int m2=Integer.parseInt(br.readLine());

System.out.println("\n\t Enter the student mark3:");

int m3=Integer.parseInt(br.readLine());

intpacdemo inp=new intpacdemo();

inp.get(no,name);

inp.display();

inp.gets(m1,m2,m3);

inp.view();

}

}

Page 17: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

INTERFACES AND PACKAGES

Enter the student number: 21

Enter the student name: Kumar

Enter the student mark1: 80

Enter the student mark2: 90

Enter the student mark3: 100

The student number is: 21

The student name is: Kumar

marks1=80

marks2=90

marks3=100

Total=270

Avarge=90

Press any key to continue . . .

Page 18: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Application Using Synchronization

import java.lang.*;

class test2

{

static int q=100,r=0;

static public synchronized void request(int order)

throws InterruptedException

{

for(int i=1;i<=3;i++)

{

Thread.sleep(300);

if(order<=q)

{

System.out.println("\n\t Quantity Ordered:"+order);

q=q-order;

r=r+order;

System.out.println("\n\t Quantity in hand:"+q);

System.out.println("\n\t Total order taken:"+r);

}

else

{

System.out.println("\n\t Ordered quantity more than in");

System.out.println("hand");

}

order=((int)(Math.random()*100));

}

}

public static void main(String args[ ]) throws InterruptedException

{

new ourthread();

Thread.sleep(50);

test2.request((int)(Math.random()*100));

System.out.println("\n\t Existing from main thread...");

}

Page 19: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

}

class ourthread extends Thread

{

ourthread()

{

super("Test thread");

System.out.println("\n\t Child thread:"+this);

start();

}

public void run()

{

try

{

test2.request((int)(Math.random()*100));

}

catch(InterruptedException e)

{

System.out.println("\n\t"+e);

}

System.out.println("\n\t Child thread existing…");

}

}

Page 20: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Child thread:Thread[Test thread,5,main]

Quantity Ordered:76

Quantity in hand:24

Total order taken:76

Ordered quantity more than in hand

Ordered quantity more than in hand

Child thread existing…

Ordered quantity more than in hand

Ordered quantity more than in hand

Quantity Ordered:19

Quantity in hand:5

Total order taken:95

Existing from main thread...

Page 21: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

DATABASE CREATION FOR STORING E-MAIL ADDRESS AND MANIPULATION

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Scanner;

public class db

{

public static void main(String[] args) {

System.out.println("IF U WANT TO INSERT THE VALUE PRESS 1 TO CONTINUE");

System.out.println("IF U WANT TO DISPLAY THE VALUE PRESS 2 TO CONTINUE");

System.out.println("IF U WANT TO UPDATE THE VALUE PRESS 3 TO CONTINUE");

Scanner sobj1=new Scanner(System.in);

int option=sobj1.nextInt();//getting option from user

Connection con;//create reference variable to conntection class;

switch (option) {

case 1:

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//this is Driver class ... first step to connect database

con = DriverManager.getConnection("jdbc:odbc:StudentDB"); //this is url for database file

System.out.println("Connection Established");

Statement stmt = con.createStatement();

// statement creation with help of connection reference variable

Scanner sobj2=new Scanner(System.in);

//create scanner reference variable to get input from user

System.out.println("Enter Student Register No:");

int StudReg=sobj2.nextInt();

System.out.println("Enter Student Name:");

String StudName=sobj2.next();

System.out.println("Enter Student E-Mail ID");

String StudEmail=sobj2.next();

Page 22: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

String com="insert into StudentInfo values('"+StudReg+"','"+StudName+"',

'"+StudEmail+"')"; //assign query to string variable

stmt.executeUpdate(com);

//passing that query to execute with help of stmt object reference variable

System.out.println("Successfully Inserted");

con.close(); //closing the connection

System.out.println("Connection Closed");

}

catch (Exception e) {

e.printStackTrace();

//this statement for display the exception

}

break;

case 2:

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//driver

System.out.println("Driver Loaded Please Wait..");//checking

con = DriverManager.getConnection("jdbc:odbc:StudentDB");//url path

System.out.println("Connection Established");

Statement stmt = con.createStatement();

ResultSet rs=stmt.executeQuery("select * from StudentInfo");

//select query to display the Data from the database

while(rs.next())//looping to display

{

int stuNo=rs.getInt(1);

String stuName=rs.getString(2);

String stuEmail=rs.getString(3);

System.out.println("Student Register No:"+stuNo+"\t Student Name:"+stuName+"\t

Student E-Mail ID:"+stuEmail);

}

}

catch (Exception e) {

e.printStackTrace();

}

Page 23: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

break;

case 3:

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

System.out.println("Driver Loaded");

con = DriverManager.getConnection("jdbc:odbc:StudentDB");

System.out.println("Connection Established");

Statement stmt = con.createStatement();

System.out.println("ENTER REGNUM FOR UPDATE");

Scanner sc=new Scanner(System.in);

int StudReg=sc.nextInt();

System.out.println("ENTER RE- MAILID");

String StudEmail=sc.next();

stmt.executeUpdate("UPDATE StudentInfo SET StudEmail='"+StudEmail+"' WHERE

StudReg="+StudReg+" ");

stmt.cancel();

con.close();

System.out.println("RECOED HAS BEEN UPDATED..");

}

catch (Exception e) {

e.printStackTrace();

}break;

default:

{

System.out.println("Please Select Right Option");

}

}

}

}

Page 24: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT

IF U WANT TO INSERT THE VALUE PRESS 1 TO CONTINUE

IF U WANT TO DISPLAY THE VALUE PRESS 2 TO CONTINUE

IF U WANT TO UPDATE THE VALUE PRESS 3 TO CONTINUE

1

Connection Established

Enter Student Register No: 1003

Enter Student Name: SANKAR

Enter Student E-Mail ID : [email protected]

Successfully Inserted

Connection Closed

IF U WANT TO INSERT THE VALUE PRESS 1 TO CONTINUE

IF U WANT TO DISPLAY THE VALUE PRESS 2 TO CONTINUE

IF U WANT TO UPDATE THE VALUE PRESS 3 TO CONTINUE

2

Driver Loaded Please Wait..

Connection Established

Student Register No:1001 Student Name:MANI Student E-Mail ID:[email protected]

Student Register No:1002 Student Name:PAPPU Student E-Mail ID:[email protected]

Student Register No:1003 Student Name:SANKAR Student E-Mail ID:[email protected]

IF U WANT TO INSERT THE VALUE PRESS 1 TO CONTINUE

IF U WANT TO DISPLAY THE VALUE PRESS 2 TO CONTINUE

IF U WANT TO UPDATE THE VALUE PRESS 3 TO CONTINUE

3

Driver Loaded

Connection Established

ENTER REGNUM FOR UPDATE : 1002

ENTER RE- MAILID : [email protected]

RECOED HAS BEEN UPDATED..

Page 25: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

IF U WANT TO INSERT THE VALUE PRESS 1 TO CONTINUE

IF U WANT TO DISPLAY THE VALUE PRESS 2 TO CONTINUE

IF U WANT TO UPDATE THE VALUE PRESS 3 TO CONTINUE

2

Driver Loaded Please Wait..

Connection Established

Student Register No:1001 Student Name:MANI Student E-Mail ID:[email protected]

Student Register No:1002 Student Name:PAPPU Student E-Mail ID:[email protected]

Student Register No:1003 Student Name:SANKA Student E-Mail ID:[email protected]

Page 26: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

IMPLEMENTATION OF THREAD BASED APPLICATIONS

AND EXCEPTION HANDLING

import java.*;

import java.io.*;

class thread1 extends Thread

{

public void run()

{

try

{

for(int i=0;i<5;i++)

{

System.out.println("\n\t Thread1 says gud morning"+i);

Thread.sleep(500);

} }

catch(InterruptedException e)

{

System.out.println("\tThread1 is running");

}

System.out.println("\tThread1 is end");

} }

class thread2 extends Thread

{

public void run()

{

try

{

for(int j=0;j<5;j++)

{

System.out.println("\n\t Thread2 says gud evening"+j);

Thread.sleep(800);

} }

catch(InterruptedException e)

{

System.out.println("\tThread2 is running");

Page 27: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

}

System.out.println("\tThread2 is end");

} }

class arithematic extends Thread

{

public void run()

{

int a=10,b=10,c,d;

{

c=a+b;

System.out.println("\n\tThe value of c : "+c);

}

try

{

d=2/(a-b);

{

System.out.println("\n\t The value of d"+d);

} }

catch(ArithmeticException e)

{

System.out.println("\n\t DIVIDE-BY-ZERO");

}

System.out.println("\tThread3 is end");

} }

class threaddemo

{

public static void main (String args[])

{

new thread1().start();

new thread2().start();

new arithematic().start();

}

}

Page 28: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Thread2 says gud evening0

Thread1 says gud morning0

The value of c : 20

DIVIDE-BY-ZERO

Thread3 is end

Thread1 says gud morning1

Thread2 says gud evening1

Thread1 says gud morning2

Thread1 says gud morning3

Thread2 says gud evening2

Thread1 says gud morning4

Thread2 says gud evening3

Thread1 is end

Thread2 says gud evening4

Thread2 is end

Page 29: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

IMPLEMENTATION OF POINT CLASS

import java.awt.*;

import java.applet.*;

/*<applet code="pointdemo.class" width=1100 height=690>

</applet>*/

public class pointdemo extends Applet

{

Point p1=new Point(20,200);

Point p2=new Point(120,200);

Point p3=new Point(120,20);

Point p4=new Point(20,20);

public void init()

{

setBackground(Color.pink);

}

public void paint(Graphics g)

{

g.setColor(Color.green);

g.drawLine(p1.x,p1.y,p2.x,p2.y);

g.drawLine(p2.x,p2.y,p3.x,p3.y);

g.drawLine(p3.x,p3.y,p4.x,p4.y);

g.drawLine(p4.x,p4.y,p1.x,p1.y);

}

}

Page 30: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Page 31: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Drawing Various Shapes Using Graphics Methods

import java.awt.*;

import java.applet.*;

/*<applet code=”face” width=1100 height=690>

</applet>*/

public class grapic extends Applet

{

public void paint(Graphics g)

{

setBackground(Color.white);

g.drawOval(40,40,120,150);

g.fillOval(68,81,10,10);

g.fillOval(120,81,10,10);

g.drawOval(75,125,50,20);

g.fillOval(25,92,15,30);

g.fillOval(160,92,15,30);

g.drawString("II BCA GUYS", 70, 20);

}

}

Page 32: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Page 33: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Working with Dialog Boxes and Menus

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="menu" width="1100" height="690">

</applet>*/

public class menu extends Applet implements ActionListener

{

Menu m1;

MenuItem mi1,mi2;

MenuBar mb;

Dialog d;

Frame f;

Label l;

Button b1,b2;

Panel p;

TextArea text1;

public void init()

{

f=new Frame("II BCA");

p=new Panel();

l=new Label("Are you sure you want to exit..?");

b1=new Button("OK");

b2=new Button("CANCEL");

d=new Dialog(f,"CONFIRM DIALOG BOX",true);

text1=new TextArea(" ",80,40);

mb=new MenuBar();

mi1=new MenuItem("New");

mi2=new MenuItem("Exit");

mi1.addActionListener(this);

mi2.addActionListener(this);

b1.addActionListener(this);

b2.addActionListener(this);

m1=new Menu("File");

Page 34: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

m1.add(mi1);

m1.add(mi2);

mb.add(m1);

f.setMenuBar(mb);

f.setSize(500,500);

f.add(text1);

f.setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getActionCommand().equals("New"))

text1.append("File Menu Option Clicked....");

if(ae.getActionCommand().equals("Exit"))

{

b1.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae1)

{

f.dispose();

} });

b2.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae2)

{

d.dispose();

} });

d.setLayout(new BorderLayout());

d.add(l,BorderLayout.WEST);

p.add(b1);

p.add(b2);

d.add(p,BorderLayout.CENTER);

d.setSize(300,100);

d.setVisible(true);

}

} }

Page 35: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT

Page 36: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Working with Frames and other Controls

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

public class RMKFRAMES extends Applet implements ActionListener

{

TextField t;

Button b;

public void init()

{

Frame f=new Frame("Frame Demo by II BCA");

f.setLayout(new GridLayout(2,2));

f.setSize(300,200);

b=new Button("Click Me");

b.addActionListener(this);

t=new TextField(40);

f.add(t);

f.add(b);

f.show();

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b)

{

t.setText("This Program Developed By II BCA");

}

}

}

Page 37: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT

Page 38: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Working with Colors and Fonts

import java.applet.*;

import java.awt.*;

/*<applet code="font" width=300 height=100>

</applet>*/

public class RMKCOLORS extends Applet

{

public void paint(Graphics g)

{

g.setFont(new Font("Arial",Font.BOLD,35));

g.setColor(Color.BLUE);

g.drawString("COLORS AND FONT PROGRAM", 100, 100);

setBackground(Color.darkGray);

g.setFont(new Font("Times New Roman",Font.ITALIC,30));

g.setColor(Color.RED);

g.drawString("DEVELOPED BY II BCA", 100, 120);

}

}

Page 39: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Page 40: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

USAGE OF BUTTONS, LABELS AND TEXT COMPONENTS IN SUITABLE APPLICATIONS

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="RMKCALC" width=300 height=300>

</applet>

*/

public class RMKCALC extends Applet

implements ActionListener

{

String msg=" ";

int v1,v2,result;

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(10);

GridLayout gl=new GridLayout(4,5);

setLayout(gl);

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);

Page 41: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

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());

Page 42: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

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("");

}

}

}

Page 43: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT

Page 44: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Usage of Radio Buttons, Check Box, Choice List in Suitable Applications

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class RMKCHECK extends Applet implements ItemListener

{

String str=" ";

String msg=" ";

String str1=" ";

Checkbox c1,c2,c3;

Checkbox cg1,cg2,cg3;

CheckboxGroup cbg;

Choice Subject;

public void init()

{

c1=new Checkbox("FIRST BATCH");

c2=new Checkbox("SECOND BATCH");

c3=new Checkbox("THIRD BATCH");

cbg=new CheckboxGroup();

cg1=new Checkbox("B.C.A",cbg,true);

cg2=new Checkbox("B.Com",cbg,false);

cg3=new Checkbox("B.Sc",cbg,false);

Subject=new Choice();

Subject.add("DOT NET");

Subject.add("C++");

Subject.add("JAVA");

Subject.add("PHP");

Subject.add("JSP");

add(c1);

add(c2);

add(c3); add(cg1); add(cg2); add(cg3); add(Subject);

c1.addItemListener(this);

c2.addItemListener(this);

c3.addItemListener(this);

Page 45: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

cg1.addItemListener(this);

cg2.addItemListener(this);

cg3.addItemListener(this);

Subject.addItemListener(this);

}

public void itemStateChanged(ItemEvent ie)

{

repaint();

}

public void paint(Graphics g)

{

if(c1.getState()==true)

str="FIRST BATCH";

if(c2.getState()==true)

str="SECOND BATCH";

if(c3.getState()==true)

str="THIRD BATCH";

msg=cbg.getSelectedCheckbox().getLabel();

g.drawString("DEPARTMENT NAME: ",50,175);

g.drawString(msg,75,200);

g.drawString("LAB BATCH NUMBER :",50,250);

g.drawString(str,75,275);

str1=Subject.getSelectedItem();

g.drawString("SUBJECT NAME :",50,325);

g.drawString( str1,75,350);

}

}

Page 46: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT:

Page 47: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

Working with panels and Layouts

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.util.*;

/* <applet code = RMKPAN height = 350 width = 350>

</applet> */

public class RMKPAN extends Applet

{

public void init()

{

setLayout(new BorderLayout());

add(new Button("TOP"),BorderLayout.NORTH);

add(new Button("BOTTOM"),BorderLayout.SOUTH);

add(new Button("LEFT"),BorderLayout.WEST);

add(new Button("RIGHT"),BorderLayout.EAST);

String str = "\n\t QUAIDE MILLETH COLLEGE\n" + "\n\t Department of BCA\n"+"\n\t

HTTP - Hyper Text Markup Language\n"+ "\t FTP - File Transfer Protocal\n"+"\t JDK - Java

Development Kit\n"+ "\t JVM - Java Virtual Machine\n";

add(new TextArea(str),BorderLayout.CENTER);

}

}

Page 48: Java Lab Record by Prof Manikandan

Dept of Computer Application, QMC - Medavakkam

OUTPUT