Experiment No.
2
Write a program to Demonstrate Type Casting.
class typecast
{
public static void main(String args[])
{
byte h=127;
int a=300;
float a1=12.222f;
float g;
short b=200;
long c=999999;
float e=345.89F;
double f=45645.78222222222222;
g= (float)f;
[Link]("short b ="+g);
[Link]("short b ="+b);
[Link]("long c ="+c);
[Link]("float e="+e);
[Link]("double f="+f);
[Link]("short b="+b);
[Link]("short to byte "+(byte)b);
[Link]("int to byte "+(byte)a);
[Link]("int to float"+(float)a);
[Link]("long to byte "+(byte)c);
[Link]("double to long "+(long)f);
[Link]("double to int "+(int)f);
[Link]("double to byte "+(byte)f);
[Link]("double to short "+(short)f);
[Link]("double to float "+(float)f);
[Link]("float to int "+(int)e);
[Link]("float to byte "+(byte)e);
[Link]("float to short "+(short)e);
[Link]("float to long "+(long)e);
[Link]("float to double ="+(double)e);
[Link]("long to int"+(int)c);
[Link]("byte to int ="+(int)h);
}
}
Experiment No. 3
Write a progra m to Test the
Prime num.
import [Link].*;
class prime
{
public static void main(String args[])
{ int flag,x,i;
flag=0;
int a[]=new int[7];
for(x=0;x<[Link];x++)
{
a[x]=[Link](args[x]);
for(i=2;i<(a[x]/2);i++)
{
if((a[x]%i)==0)
{
break;
}
else flag=1;
}
if(flag==1)
[Link](a[x]+" is a prime no ");
else
[Link](a[x]+" is not a prime no ");
flag=0;
}
}
}
Experiment No. 4
Write a program to find out the HCF and
LCF .
import [Link].*;
class hcf
{
public static void main(String args[])
{
int a,b;
Scanner sc= new Scanner([Link]);
[Link]("Enter two nos :");
a=[Link]();
b=[Link]();
int big;
int small;
if(a>b)
{
}
else
{
} big=a;
small=b;
big=b;
small=a;
for(int i=1;i<=big;i++)
{
if(((big*i)%small)==0)
{
int lcm=big*i;
[Link]("The least common multiple is "+(lcm));
break;
}
}
int temp=1;
while(temp!=0)
{
temp=big%small;
if(temp==0)
{
}
else
{ [Link]("GCD is "+small);
big=small;
small=temp;}
}}}
Experiment No. 5
Write a program to calculate the Simple Interest and Input by the
user.
import [Link].*;
class si
{
int p,t;
float si,r;
public si()
{
r=0;
p=0;
}
public void getdata()
{
Scanner sc =new Scanner([Link]);
[Link]("Enter principle : ");
p=[Link]();
[Link]("Enter rate : ");
r=[Link]();
[Link]("Enter time period : ");
t=[Link]();
}
public void cal()
{
si=(p*r*t)/100;
}
public void display()
{
[Link]("Principle : Rs"+p);
[Link]("Rate : "+r);
[Link]("Time period : "+t);
[Link]("Simple Interest : Rs"+si);
}
public static void main(String args[])
{
si s = new si();
[Link]();
[Link]();
[Link]();
}
}
Experiment No. 6
Write a program to create a Simple class to find out the Area and
perimeter of rectangle and box using super and this keyword .
class rect
{
int l,b;
public rect(int l,int b)
{ this.l=l;
this.b=b;
}
public int area()
{
return l*b;
}
}
class box extends rect
{
int d;
public box(int l,int b,int d)
{
super(l,b);
this.d=d;
}
public int volume()
{
int vol = area()*d;
return vol;
}
public static void main(String args[])
{ int vol ,area;
[Link]("derived object in derived reference");
rect r= new rect(10,20);
area=[Link]();
[Link]("area is "+area+" \n");
[Link]("base object in base reference");
box b = new box(10,20,30);
vol=[Link]();
area=[Link]();
[Link]("area is "+area);
[Link]("volume is "+vol+" \n");
[Link]("derived object in base reference");
rect b1= new box(10,90,70);
area = [Link]();
method
assigned //vol=[Link](); as with refernce of base class we can't call derived's
[Link]("area is "+area);
//as super class doesn't knw abt the base class but reference can be
/*[Link]("base object in derived reference");
box b2=(new rect (10,20));
vol = [Link]();
[Link]("area is "+area);*/
r=b;
[Link]([Link]());
[Link]([Link]());
}
}
Experiment No. 7
Write a program to design a class account using the inheritance and
static that show all function of bank(withrowal,deposite) and generate
account number dyanamically.
import [Link].*;
class bank
{ static int acc_no =10001;
float amt;
public void display()
{
[Link]("Account no :"+acc_no );
[Link]("Current Amount :"+amt );
}
public bank()
{
amt=1000;
[Link]("Ur account no is "+acc_no);
acc_no++;
}
public void getamt()
{
[Link]("Current balance :"+amt);
}
public void withdraw(float x)
{
if(amt==1000 || amt<=x )
{
[Link]("Sorry u can't withdraw");
}
else
{
amt=amt -x;
[Link]("amount withdrawn :"+x);
[Link]("After withdrawl");
getamt();
}
}
public void deposit(float x)
{
if(x==0.0)
[Link]("OOPS 0 can't be deposited");
else {
amt+=x;
[Link]("After deposition");
getamt();}
}
public static void main(String args[])
{ Scanner sc = new Scanner([Link]);
bank b1 = new bank();
[Link](0);
[Link](120.5f);
[Link]();
[Link](" \n");
bank b2 = new bank();
[Link](1000.0f);
[Link](150.5f);
}
}
Experiment No. 8
Write a program to design a class Shape (Implement Runtime
polymorphim) using abstract Methods and Classes .
class AbstractDemo1
{
public static void main(String args[])
{
Shape shape;
Rectangle r = new Rectangle();
[Link](40,20);
shape = r;
[Link]([Link]());
[Link]([Link]());
}
}
abstract class Shape
{
void someMethod()
{
[Link]("This is some method");
}
abstract float getArea();
abstract float getPerimeter();
}
class Square extends Shape
{
float side;
Square()
{
side = 0;
}
Square(float side)
{
[Link] = side;
}
void setSide(float side)
{
[Link] = side;
}
float getArea()
{
return side * side;
}
float getPerimeter()
{
return 4 * side;
}
}
import [Link].*;
MyCircle extends MyShape
static
{
}
pi = 22 / 7.0f;
MyCircle()
{
super("circle");
radius = 0;
}
MyCircle(float radius)
{
super("circle");
[Link] = radius;
}
void setDimensions(float radius)
{
[Link] = radius;
}
void showDimensions()
{
[Link]("radius : " + radius);
}
float getArea()
{
return radius * radius * pi;
}
}
class Rectangle extends Shape
{
private float length;
private float breadth;
Rectangle()
{
length = breadth = 0;
}
Rectangle(float length, float breadth)
{
setDimensions(length, breadth);
}
void setDimensions(float length, float breadth)
{
[Link] = length;
[Link] = breadth;
}
float getArea()
{
return length * breadth;
}
float getPerimeter()
{
return (2 * (length + breadth));
}
Experiment No. 9
Write a program to design a String class that perform String
Method(Equal, Reverse the string, change case, trim etc. )
public class StringDemo
{
public static void main(String args[])
{
String str = "This is some sample String with some words that have been
repeated some times";
[Link]("Total no. of characters : " + [Link]());
[Link]("To Upper Case : " + [Link]());
[Link]("To Lower Case : " + [Link]());
[Link]("Original String : " + str);
[Link]([Link](8));
[Link]([Link](8,19));
[Link]([Link]("some"));
String s = " " + str + " ";
[Link](s);
[Link]("[" + [Link]() + "]");
[Link]([Link]("s","$$##"));
String sh = "parth is a good boy";
[Link](sh + " -> " + new StringBuffer(sh).reverse());
}}
Experiment No. 10
Write a program to handle the Exception using try and multiple catch
block.
class exception
{
public static void main(String args[]){
try{
}
int d=42;
int a =0;
int c=d/a;
catch(ArithmeticException e){
[Link]("Division by zero error");
}
}
}
Other Example:
public class ExceptionHandling
{
public static void main(String args[])
{
String num[]={"123","456","abc","789"};
int sum=0;
int i;
for(i=0;i<=[Link];i++)
{
try{
sum+=[Link](num[i]);
}
catch(NumberFormatException e)
{ [Link]("NUMBER FORMAT ERROR");
}
catch(ArrayIndexOutOfBoundsException e)
{[Link]("ARRAY ERROR");
}
finally
{ [Link]("i = "+i);
}
}
[Link]("sum is"+sum);
}
}
Experiment No. 11
Write a program that Implement the Nested
try Statements.
class NestedTry
{
public static void main(String args[])
{ int a=[Link];
try{
int d=42/a;
try
{
if(a==1){
int c= a/(a-a);}
if(a==2)
{
int c[]={2,3,4};
c[5]=90;
}
}
catch(ArrayIndexOutOfBoundsException e)
{[Link]();
}
}
catch(ArithmeticException e)
{
[Link]();
}
}
}
Experiment No. 12
Write a program that Implement Throw
and Throws.
import [Link];
class ThrowThrowsDemo {
static void checkNumber(int num) throws
ArithmeticException {
if (num < 0) {
throw new ArithmeticException("Number
cannot be negative");
} else {
[Link]("Valid number: " + num);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
try {
checkNumber(num); // method call
}
catch (ArithmeticException e) {
[Link]("Exception caught: " +
[Link]());
}
finally {
[Link]("Execution completed.");
}
[Link]();
}
}
Experiment No. 13
Write a program that Implement
Custom Exception. Import .
[Link].*;
class MyException extends Exception
{
private int e;
MyException (int a )
{
e=a;
}
public String toString()
{
return ("Error in entry"+e);
}
}
public class mine
{ public void compute(int a) throws MyException
{
int age=a;
if(age>150)
throw new MyException (age);
[Link]("COrrect age");
}
public static void main(String args[])
{
mine m=new mine();
try{
[Link](1);
[Link](789);
}
catch(MyException e)
{
[Link](e);
}
}
}
Experiment No. 14
Write a program to Create a package that access the member of
external class as well as same package.
package pack;
class base
{
public static void main(String arg[])
{
[Link]("Base class(p1)");
p1 w=new p1();
//w.f1();
[Link]("Derived class(p2)");
p2 x=new p2();
// x.f2();
[Link]("Simple class(p3)");
p3 y=new p3();
// y.f3();
}
}
package pack;
public class p1
{
int a=1;
public int b=2;
private int c=3;
protected int d=4;
public p1()
{
[Link]("Value of a="+a);
[Link]("Value of b="+b);
[Link]("Value of c="+c);
[Link]("Value of d="+d);
}
}
package pack;
class p2 extends p1
{
p2()
{
}
}
[Link]("Value of a="+a);
[Link]("Value of b="+b);
//[Link]("Value of c="+c);
[Link]("Value of d="+d);
package pack;
class p3
{
p1 p=new p1();
p3()
{
[Link]("Value of a="+(p.a));
[Link]("Value of b="+(p.b));
//[Link]("Value of c="+(p.c));
[Link]("Value of d="+(p.d));
}
Experiment No. 15
Write a program that show the partial implementation of
Interface.(calculation of Salary of Employee).
import [Link];
// Step 1: Interface
interface Employee {
void calculateSalary();
void displayDetails();
}
// Step 2: Abstract class (Partial Implementation)
abstract class PartialEmployee implements Employee {
String name;
int id;
PartialEmployee(String name, int id) {
[Link] = name;
[Link] = id;
}
// Implement only one method
public void displayDetails() {
[Link]("Employee Name: " + name);
[Link]("Employee ID: " + id);
}
}
// Step 3: Final class (Full Implementation)
class FullEmployee extends PartialEmployee {
double basicSalary, hra, da, totalSalary;
FullEmployee(String name, int id, double basicSalary) {
super(name, id);
[Link] = basicSalary;
}
// Implement remaining method
public void calculateSalary() {
hra = 0.2 * basicSalary;
da = 0.1 * basicSalary;
totalSalary = basicSalary + hra + da;
}
void showSalary() {
[Link]("Basic Salary: " + basicSalary);
[Link]("HRA: " + hra);
[Link]("DA: " + da);
[Link]("Total Salary: " + totalSalary);
}
}
// Main class
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Name: ");
String name = [Link]();
[Link]("Enter ID: ");
int id = [Link]();
[Link]("Enter Basic Salary: ");
double salary = [Link]();
FullEmployee emp = new FullEmployee(name, id, salary);
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Experiment No. 16
Write a program to create Arithmetic Math Calculator Using Applet Class ant
Event Handling.
import [Link];
import [Link].*;
import [Link].*;
/*
<applet code="CalculatorApplet" width=300 height=300>
</applet>
*/
public class CalculatorApplet extends Applet implements ActionListener {
TextField t1, t2, t3;
Button add, sub, mul, div;
public void init() {
// Create components
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
add = new Button("+");
sub = new Button(" -");
mul = new Button("*");
div = new Button("/");
// Add components to applet
add(new Label("Number 1:"));
add(t1);
add(new Label("Number 2:"));
add(t2);
add(new Label("Result:"));
add(t3);
add(add);
add(sub);
add(mul);
add(div);
// Register event handling
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
int num1 = [Link]([Link]());
int num2 = [Link]([Link]());
int result = 0;
if ([Link]() == add) {
result = num1 + num2;
}
else if ([Link]() == sub) {
result = num1 - num2;
}
else if ([Link]() == mul) {
result = num1 * num2;
}
else if ([Link]() == div) {
result = num1 / num2;
}
[Link]([Link](result));
}
}
Experiment No. 17
Write a program to Draw the line, Rectangle, oval, text etc using the
graphics method.
/*<applet code= "[Link]" width = "500" height = "300">
</applet>*/
import [Link];
import [Link].*;
public class AppletDemo extends Applet
{
public void init()
{setBackground([Link]);
}
public void paint(Graphics g)
{
Font f=new Font("TIMES NEW ROMAN ",[Link],32);
[Link](f);
[Link]([Link]);
[Link]("WELCOME TO APPLET ",30,30);
[Link](60,60,150,150);
[Link]([Link]);
[Link](90,100,20,20);
[Link](160,100,20,20);
[Link]([Link]);
[Link](120,150,150,150);
[Link](120,150,140,130);
[Link](90,130,90,60,0, -180);
} }
Experiment No. 18
Write a program to create a frame Window Using Frame Class.
public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import [Link].*;
/*
class MyWindowListener implements WindowListener
{
public void windowActivated(WindowEvent we){}
public void windowDeactivated(WindowEvent we){}
public void windowOpened(WindowEvent we){}
public void windowClosed(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
public void windowClosing(WindowEvent we)
{
[Link](0);
}
}
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
}
import [Link].*;
import [Link].*;
class MyFrame extends Frame //implements WindowListener
{
MyFrame()
{
super("Sample Java Frame");
//MyWindowListener mwl = new MyWindowListener();
//MyWindowAdapter mwa = new MyWindowAdapter();
//addWindowListener(mwa);
addWindowListener(new MyWindowAdapter());
//addWindowListener(this);
setSize(400,300);
setResizable(true);
//setUndecorated(true);
setVisible(true);
}
}
Experiment No. 19
Write a program to implement
ListBox.
public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import [Link].*;
import [Link].*;
public class MyFrame extends Frame implements ActionListener
{
List lst;
Button btn;
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(500,400);
addControls();
setVisible(true);
}
private void addControls()
{
setLayout(null);
lst = new List();
[Link](30,50);
[Link](200,300);
[Link](true);
add(lst);
[Link]("sfsdf");
[Link]("55656");
[Link]("dfgdfg");
[Link]("sfsdf");
[Link]("cvb");
[Link]("sfcvbcbcvbcvbsdf");
[Link]("bmmbnm");
[Link]("ioouo");
[Link]("qeqwe");
[Link](".m,.m,.");
btn = new Button("Click Me");
[Link](this);
[Link](100,24);
[Link](250,50);
add(btn);
public void actionPerformed(ActionEvent ae)
{
[Link]("Total Selected Items : " +
[Link]().length);
String aItem[] = [Link]();
int i;
for (i=0;i<[Link];i++)
[Link](aItem[i]);
}
}
import [Link].*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
}
Experiment No. 20
Write a program to implement Choice, Checkbox, radio button
With event handling.
public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import [Link].*;
import [Link].*;
class MyFrame extends Frame implements ItemListener
{
Checkbox c1, c2, c3, c4, c5, c6;
CheckboxGroup cbg1, cbg2;
Choice cbo;
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(500,400);
addControls();
setVisible(true);
}
private void addControls()
{
setLayout(new FlowLayout());
cbg1 = new CheckboxGroup();
cbg2 = new CheckboxGroup();
c1 = new Checkbox("C", true);
c2 = new Checkbox("C++",cbg2,true);
c3 = new Checkbox("Java",cbg2, true);
c4 = new Checkbox("Prolog", cbg1,false);
c5 = new Checkbox("Lisp", true, cbg1);
c6 = new Checkbox("Fortran");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
//[Link](true);
//[Link](false);
add(c1);
add(c2);
add(c3);
add(c4);
add(c5);
add(c6);
cbo = new Choice();
[Link]("Delhi");
[Link]("Ajmer");
[Link]("Jaipur");
[Link]("Mumbai");
[Link]("Beawar",0);
[Link]("Chandigarh");
[Link]("Jalandhar");
[Link]("Nasirabad");
[Link]("Bharatpur");
[Link](3);
[Link](this);
add(cbo);
}
public void itemStateChanged(ItemEvent ie)
{
if ([Link]() instanceof Checkbox)
{
Checkbox c = (Checkbox) [Link]();
[Link]([Link]() + " : " + [Link]());
}
else if ([Link]() instanceof Choice)
{
[Link]("Selected Index : " + [Link]());
[Link]("Selected Item : " + [Link]());
}
}
}
import [Link].*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
}
Experiment No. 21
Write a program to implement Layout Manager.
public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import [Link].*;
import [Link].*;
class MyFrame extends Frame implements ActionListener
{
Panel mainPanel, p1, p2, p3, p4, p5, topPanel;
CardLayout cl;
TextField txt[];
Button btn[], b1, b2, b3, b4, b5;
TextArea ta[];
Label lbl[];
Choice choice[];
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(400,300);
addControls();
setVisible(true);
}
private void addControls()
{
cl = new CardLayout();
mainPanel = new Panel();
[Link](cl);
int i;
GridLayout gl = new GridLayout(5,10,5,5);
p1 = new Panel();
[Link](gl);
txt = new TextField[50];
for (i=0;i<[Link];i++)
{
txt[i] = new TextField("Text " + (i+1));
[Link](txt[i]);
}
p2 = new Panel();
[Link](gl);
btn = new Button[50];
for (i=0;i<[Link];i++)
{
btn[i] = new Button("Button " + (i+1));
btn[i].addActionListener(this);
[Link](btn[i]);
}
p3 = new Panel();
[Link](gl);
ta = new TextArea[50];
for (i=0;i<[Link];i++)
{
ta[i] = new TextArea("Text " + (i+3));
[Link](ta[i]);
}
p4 = new Panel();
[Link](gl);
lbl = new Label[50];
for (i=0;i<[Link];i++)
{
lbl[i] = new Label("Label " + (i+4));
[Link](lbl[i]);
}
p5 = new Panel();
[Link](gl);
choice = new Choice[50];
for (i=0;i<[Link];i++)
{
choice[i] = new Choice();
[Link](choice[i]);
}
[Link](p1,"panel1");
[Link](p2,"panel2");
[Link](p3,"panel3");
[Link](p4,"panel4");
[Link](p5,"panel5");
add(mainPanel);
b1 = new Button("Card 1");
b2 = new Button("Card 2");
b3 = new Button("Card 3");
b4 = new Button("Card 4");
b5 = new Button("Card 5");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
topPanel = new Panel();
[Link](new FlowLayout());
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
add(topPanel, [Link]);
}
public void actionPerformed(ActionEvent ae)
{
if ([Link]() == b1)
[Link](mainPanel,"panel1");
else if ([Link]() == b2)
[Link](mainPanel,"panel2");
else if ([Link]() == b3)
[Link](mainPanel,"panel3");
else if ([Link]() == b4)
[Link](mainPanel,"panel4");
else if ([Link]() == b5)
[Link](mainPanel,"panel5");
else
{
int i;
boolean found = false;
for (i=0;i<[Link];i++)
{
if ([Link]() == btn[i])
{
found = true;
break;
}
}
if (found)
{
[Link]("Button Clicked from panel : " +
btn[i].getLabel());
}
}
}
}
import [Link].*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
}
Experiment No. 22
Write a program to implement Dialog box.
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code="DialogDemo" width =250 height = 250>
</applet>
*/
class SampleDialog extends Dialog implements ActionListener
{
SampleDialog(Frame parent,String title)
{
super(parent,title,false);
setLayout(new FlowLayout());
setSize(300,200);
add(new Label("Press this button: "));
Button b;
add(b= new Button("Cancel"));
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
dispose();
}
public void paint(Graphics g)
{
[Link]("This is in the dialog box",10,70);
}
}
class MenuFrame extends Frame
{
String msg = "";
CheckboxMenuItem debug,test;
MenuFrame(String title)
{
super(title);
MenuBar mbar = new MenuBar();
setMenuBar(mbar); //Menu Bar added on applet
Bar Menu file = new Menu("File"); //Menu File is created
MenuItem item1,item2,item3,item4,item5; //Menu items for File created
[Link](item1=new MenuItem("New...")); //Menu Items added in Menu
[Link](item2=new MenuItem("Open..."));
[Link](item3=new MenuItem("Close"));
[Link](item4=new MenuItem(" -"));
[Link](item5=new MenuItem("Quit..."));
[Link](file); // Menu File added on Menu
Menu edit = new Menu("Edit"); //Menu Edit is created
MenuItem item6,item7,item8,item9; //Menu items for Edit created
[Link](item6=new MenuItem("Cut")); //Menu Items added in Menu
[Link](item7=new MenuItem("Copy"));
[Link](item8=new MenuItem("Paste"));
[Link](item9=new MenuItem(" -"));
created
created
Bar Menu sub = new Menu("Special",true); //Menu Special is
MenuItem item10,item11,item12; //Menu items for Special
[Link](item10=new MenuItem("First")); //Menu Items added in Menu
[Link](item11=new MenuItem("Second"));
[Link](item12=new MenuItem("Third"));
[Link](sub); //Menu Special added in Edit Menu
debug = new CheckboxMenuItem("Debug");
[Link](debug);
test = new CheckboxMenuItem("Testing");
[Link](test);
[Link](edit); //Menu Edit added on Menu
menu
item MyMenuHandler handler = new MyMenuHandler(this);//added a handler for
[Link](handler); //added an Action Listener for each
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
[Link](handler);
MyWindowAdapter adapter = new MyWindowAdapter(this);
addWindowListener(adapter);
}
public void paint(Graphics g)
{
[Link](msg,10,200);
if([Link]())
[Link]("Debug is on...",10,220);
else
[Link]("Debug is off...",10,220);
if([Link]())
[Link]("Testing is on...",10,240);
else
}
}
[Link]("Testing is off...",10,240);
class MyWindowAdapter extends WindowAdapter
{
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuFrame)
{
[Link]=menuFrame;
}
public void WindowClosing(WindowEvent we)
{
[Link]();
}
}
class MyMenuHandler implements ActionListener,ItemListener
{
MenuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame)
{
[Link]=menuFrame;
}
public void actionPerformed(ActionEvent ae)
{
String msg="You selected";
String arg=(String)[Link]();
if([Link]("New..."))
{
msg+=" New.";
SampleDialog d= new SampleDialog(menuFrame,"New Dialog Box");
[Link](true);
}
else if([Link]("Open..."))
msg+=" Open.";
else if([Link]("Close"))
msg+=" Close.";
else if([Link]("Quit..."))
msg+=" Quit.";
else if([Link]("Edit"))
msg+=" Edit.";
else if([Link]("Cut"))
msg+=" Cut.";
else if([Link]("Copy"))
msg+=" Copy.";
else if([Link]("Paste"))
msg+=" Paste.";
else if([Link]("First"))
msg+=" First.";
else if([Link]("Second"))
msg+=" Second.";
else if([Link]("Third"))
msg+=" Third.";
else if([Link]("Debug"))
msg+=" Debug.";
else if([Link]("Testing"))
msg+=" Testing.";
[Link]=msg;
[Link]();
}
public void itemStateChanged(ItemEvent ie)
{
[Link]();
}
}
public class DialogDemo extends Applet
{
Frame f;
public void init()
{
f=new MenuFrame("Menu Demo");
int width=[Link](getParameter("width"));
int height=[Link](getParameter("height"));
setSize(new Dimension(width,height));
[Link](width,height);
[Link](true);
}
public void start()
{
[Link](true);
}
public void stop()
{
Experiment No. 23
Write a program to implement Smiley face
Using applet.
import [Link].*;
import [Link].*;
/*<APPLET
CODE = [Link]
WIDTH =250
HEIGHT = 200 >
<param name="a" value =10>
<param name="b" value =20>
</APPLET>*/
public class Face extends Applet
{
public void paint (Graphics g)
{
String a;
String b;
String c;
a=getParameter("a");
b=getParameter("b");
int p=[Link](a);
int q=[Link](b);
int sum=p+q;
c=[Link](sum);
[Link]("First value : -"+a,10,210);
[Link]("Second value :-"+b,10,230);
[Link]("Total sum : -"+c,10,250);
[Link](10,212,130,212);
[Link](10,232,130,232);
[Link](10,252,130,252);
Color c1=new Color(25,0,0);
setBackground(c1);
setForeground([Link]);
[Link](200,160,100,50);
[Link](200,40,100,50);
[Link](40,40,120,150);
[Link](57,75,30,20);
[Link](110,75,30,20);
[Link](68,81,10,10);
[Link](121,81,10,10);
[Link](85,100,30,30);
[Link](60,125,80,40,180,180);
[Link](25,92,15,30);
[Link](160,92,15,30);
}
}
Program 24: Write a program to create Frame that display
the student information.
import [Link].*;
import [Link].*;
public class Studentinfo
{ static StudFrame sf;
public static void main(String args[])
{
sf = new StudFrame();
}
}
class mywindowadapter extends WindowAdapter
{
// StudFrame sf;
// public mywindowadapter(StudFrame sf)
// {
// [Link]=sf;
// }
//
public void windowClosing(WindowEvent we)
{
// [Link](false);
[Link](0);
}
}
class StudFrame extends Frame implements ActionListener,ItemListener
{
Button b1,b2,b3,b4;
static TextField t1,t2;
static Choice c,c1,c2,cc;
static Label lh,l1,l2,l3,l4,l5,l6;
//static List lb;
static Checkbox cb1,cb2;
static CheckboxGroup gndr=new CheckboxGroup();
StudFrame()
{
super("Student Records Form");
//mywindowadapter mw=new mywindowadapter(this);
addWindowListener(new mywindowadapter());
addcontrols();
setSize(700,550);
setResizable(true);
setVisible(true);
}
void addcontrols()
{
setLayout(null);
lh=new Label("Student Records");
l1=new Label("Student ID");
l2=new Label();
[Link]("Name");
l3=new Label("Gender");
l4=new Label("Age");
l5=new Label("Qualification");
l6=new Label("Course");
t1=new TextField(8);
t2=new TextField(8);
cb1=new Checkbo x ("Male",gndr,true);
cb2=new Checkbox("Female",gndr,false);
cc=new Choice();
for(int i=15;i<=80;i++)
[Link]([Link](i));
c=new Choice();
[Link]("Under Graduate");
[Link]("Graduate");
c1=new Choice();
[Link]("B.A.");
[Link]("B.B.A.");
[Link]("B.C.A.");
[Link]("[Link]");
[Link]("B.E./[Link]");
[Link]("[Link]");
[Link]("[Link].");
c2=new Choice();
[Link]("M.B.A.");
[Link]("M.C.A.");
[Link]("M.E./[Link]");
b1=new Button("OK");
b2=new Button("Cancel");
b3=new Button("Reset");
b4=new Button("Exit");
[Link](100,30,100,30);
[Link](100,60,100,30);
[Link](100,90,100,30);
[Link](100,120,100,30);
[Link](100,150,100,30);
[Link](100,180,100,30);
[Link](100,210,100,30);
[Link](250,60,150,20);
[Link](250,90,150,20);
[Link](250,120,40,20); [Link](310,120,60,20);
[Link](250,150,150,20);
[Link](250,180,150,20);
[Link](250,210,150,20);
[Link](250,210,150,20);
[Link](500,90,100,35); [Link](500,180,100,35);
[Link](125,290,100,35); [Link](300,290,100,35);
add(lh);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(t1);
add(t2);
add(cb1); add(cb2);
add(cc);
add(c);
add(c1); [Link](true);
add(c2); [Link](false);
add(b1);
add(b2);
add(b3);
add(b4);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
public void actionPerformed(ActionEvent ae)
{
if([Link]()==b1)
{
subframe s=new subframe("Submission","Data entered successfully.");
String s1=[Link]();
String s2=[Link]();
if([Link]()==0 || [Link]()==0 )
{
}
else
{
} [Link](300,100);
[Link](350,350);
[Link](true);
[Link](false);
}
else if([Link]()==b2)
{
subframe s=new subframe("Cancellation","Data is not
accepted(Cancellation done).");
[Link](300,100);
[Link](true);
[Link](false);
}
else if([Link]()==b3)
{
[Link]("");
[Link]("");
[Link](cb1);
[Link](0);
[Link](0);
[Link](0);
[Link](true);
}
else if([Link]()==b4)
{
[Link](0);
}
}
public void itemStateChanged(ItemEvent ie)
{
String s = [Link]();
if(s=="Under Graduate")
{
[Link](true);
[Link](false);
}
if(s=="Graduate")
{
[Link](false);
[Link](true);
}
}
class subwindowadapter extends WindowAdapter
{
subframe subf;
public subwindowadapter(subframe subf)
{
[Link]=subf;
}
public void windowClosing(WindowEvent we)
{
[Link](true);
[Link](false);
}
}
class subframe extends Frame implements ActionListener
{
Button bsubok=new Button("OK");
subframe() {}
subframe(String title) {}
subframe(String title,String msg)
{
super(title);
String s1=[Link]();
String s2=[Link]();
subwindowadapter sw=new subwindowadapter(this);
addWindowListener(sw);
if(title=="Cancellation")
{
// [Link]("");
// [Link]("");
setLayout(new FlowLayout([Link]));
add(new Label(msg));
add(bsubok);
[Link](this);
}
else
{
if([Link]()==0)
{
//resize(300,100);
setLayout(new FlowLayout([Link]));
add(new Label("Please fill in Student Name."));
add(bsubok);
[Link](this);
}
else if([Link]()==0)
{
}
else setLayout(new FlowLayout([Link]));
add(new Label("Please fill in Student Roll Number."));
add(bsubok);
[Link](this);
//add(new Label([Link]() +", Student ID:
"+[Link]()+" Accepted.",[Link]));
{
setLayout(null);
Label lhl,ll1,ll2,ll3,ll4,ll5,ll6,la1,la2,la3,la4,la5,la6,ltl;
lhl=new Label("Your data is:");
ll1=new Label("Student ID");
ll2=new Label();
[Link]("Name");
ll3=new Label("Gender");
ll4=new Label("Age");
ll5=new Label("Qualification");
ll6=new Label("Course");
ltl=new Label(msg);
la1=new Label([Link]());
la2=new Label();
[Link]([Link]());
la3=new
Label([Link]().getLabel());
la4=new Label([Link]());
la5=new Label([Link]());
if([Link]()=="Under Graduate")
la6=new Label([Link]());
else
la6=new Label([Link]());
add(lhl);
add(ll1);
add(ll2);
add(ll3);
add(ll4);
add(ll5);
add(ll6);
add(la1);
add(la2);
add(la3);
add(la4);
add(la5);
add(la6);
add(ltl);add(bsubok);
[Link](50,30,100,30);
[Link](50,60,100,30);
[Link](50,90,100,30);
[Link](50,120,100,30);
[Link](50,150,100,30);
[Link](50,180,100,30);
[Link](50,210,100,30);
[Link](200,60,100,30);
[Link](200,90,100,30);
[Link](200,120,100,30);
[Link](200,150,100,30);
[Link](200,180,100,30);
[Link](200,210,100,30);
[Link](75,240,200,30);
[Link](100,280,100,30);
[Link](this);
}
}
}
public void actionPerformed(ActionEvent ae)
{
if([Link]()==bsubok)
{
[Link](true);
setVisible(false);
}
Experiment No. 25
Write a program to implement System Clock.
public class AWT1
{
public static void main(String args[])
{
MyFrame mf = new MyFrame();
}
}
import [Link].*;
import [Link].*;
import [Link].*;
class MyFrame extends Frame implements ItemListener, ActionListener
{
Choice c1;
Button b1;
Checkbox cb1, cb2, cb3, cb4;
CheckboxGroup cbg1, cbg2;
Label lblTime;
TimeThread tt;
MyFrame()
{
super("Sample Java Frame");
addWindowListener(new MyWindowAdapter());
setSize(400,300);
addControls();
setVisible(true);
tt = new TimeThread(this);
}
private void addControls()
{
setLayout(new FlowLayout());
lblTime = new Label("System Time Here");
add(lblTime);
c1 = new Choice();
[Link]("Ajmer");
[Link]("Jaipur");
[Link]("Alwar");
[Link]("Nasirabad");
[Link]("Bikaner");
[Link]("Kishangarh");
[Link]("Beawar");
[Link]("Bundi");
[Link]("Kota");
[Link]("Nagur");
[Link]("Jodhpur");
[Link]("Pali");
[Link](this);
add(c1);
b1 = new Button("Click Me");
[Link](this);
add(b1);
cbg1 = new CheckboxGroup();
cbg2 = new CheckboxGroup();
cb1 = new Checkbox("DOS",true,cbg1);
cb2 = new Checkbox("Windows",cbg1,true);
cb3 = new Checkbox("Linux",cbg2,false);
cb4 = new Checkbox("Unix",cbg2,false);
add(cb1);
add(cb2);
add(cb3);
add(cb4);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
public void itemStateChanged(ItemEvent ie)
{
if ([Link]() == c1)
{
if ([Link]() != -1)
{
[Link]([Link]());
[Link]([Link]());
}
}
else if ([Link]() == cb1)
{
[Link]([Link]());
}
else if ([Link]() == cb3 || [Link]() == cb4)
{
[Link]("Item Selected : " +
[Link]().getLabel());
}
}
public void actionPerformed(ActionEvent ae)
{
if ([Link]() == b1)
{
int i;
for (i=0;i<[Link]();i++)
[Link]([Link](i));
}
}
String getTime()
{
Calendar cal = new GregorianCalendar();
int hour;
int minute;
int second;
hour = [Link]([Link]);
minute = [Link]([Link]);
second = [Link]([Link]);
String timeStr = hour + ":" + minute + ":" + second;
return timeStr;
}
}
class TimeThread extends Thread
{
MyFrame mf;
TimeThread(MyFrame mf)
{
[Link] = mf;
start();
}
public void run()
{
while(true)
{
[Link]([Link]());
try
{
[Link](970);
}
catch (InterruptedException e1)
{
}
}
}
}
import [Link].*;
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
}
Experiment No. 26
Write a program to implement Interthread Communication.
class Consumer implements Runnable
{
Counter counter;
Thread t;
Consumer(Counter counter)
{
[Link] = counter;
t = new Thread(this);
[Link]();
}
public void run()
{
int i;
while ((i = [Link]()) < 50);
}
}
class Counter
{
int value;
boolean valueSet;
Counter()
{
valueSet = false;
}
synchronized void setValue(int value)
{
try
{
if (valueSet == true)
{
wait();
}
[Link] = value;
[Link]("Value produced : " + value);
valueSet = true;
notify();
}
catch (InterruptedException e1){}
}
synchronized int getValue()
{
try
{
if (valueSet == false)
{
wait();
}
[Link]("Value consumed : " + value);
valueSet = false;
notify();
}
catch (InterruptedException e1){}
return value;
}
}
class Producer implements Runnable
{
Counter counter;
Thread t;
Producer(Counter counter)
{
[Link] = counter;
t = new Thread(this);
[Link]();
}
public void run()
{
int i;
for (i=1;i<=50;i++)
[Link](i);
}
}
public class InterThreadComm
{
public static void main(String args[])
{
Counter counter = new Counter();
Producer p = new Producer(counter);
Consumer c = new Consumer(counter);
}}