0% found this document useful (0 votes)
116 views

Java File

The document contains programs to print Fibonacci series, grade calculation from percentage input, prime numbers, reverse of digits, patterns and arithmetic operations. It demonstrates the use of classes, objects, methods, conditionals, loops and takes command line input. Various concepts of OOPs like constructors, method overloading are also shown.

Uploaded by

Somya Arya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Java File

The document contains programs to print Fibonacci series, grade calculation from percentage input, prime numbers, reverse of digits, patterns and arithmetic operations. It demonstrates the use of classes, objects, methods, conditionals, loops and takes command line input. Various concepts of OOPs like constructors, method overloading are also shown.

Uploaded by

Somya Arya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 31

OOPS Lab(ECS-353)

Program11: To print the Fibbonaci Series

class Fib
{
public static void main(String args[])
{
int a=-1,b=1,c;
System.out.println("Fibonaccie series is ::");
for(int i=1;i<=10;i++)
{
c=a+b;
System.out.print(" "+c);
a=b;
b=c;
}}}

Output:

Fibonaccie Series is ::

0 1 1 2 3 5 8 13 21 34

12
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 12: To print grades for given input percentage from command line using
nested if-else.

class Grade{
String g;
double per;
public Grade(double per){
g="";
this.per=per;
}
public void grade(){
if(per>=80)
g="A+";
else if(per<80 && per>=70)
g="A";
else if(per<70 && per>=60)
g="B+";
else if(per<60 && per>=50)
g="B";
else if(per<50 && per>=40)
g="C";
else
g="Fail";
}
void display(){
System.out.println("Per(%)="+per+"\tGrade="+g);
}}
class GradeExp{
public static void main(String args[]){
double p=Double.parseDouble(args[0]);
Grade ami=new Grade(p);
ami.grade();
ami.display();
}}

Output:
Run using it by command line as:
Java GradeExp 75
Per(%)=75.0 Grade=A

13
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 13: To Print first n Prime numbers

class PrimeFirstN {
public static void main(String args[])
{
int n=11,t=0,num=1,c;
while(t!=n)
{
c=0;
for(int j=2;j<num/2;j++) {
if(num%j==0)
c++;
}
if(c==0) {
System.out.print("\t"+num);
t++;
}
num++;
}}}

Output:

1 2 3 5 7 9 11 13 17 19 23

14
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 14: To print series of prime numbers upto n.

class Prime{
public static void main(String args[]){
int n=Integer.parseInt(args[0]);
Prime p=new Prime();
p.listprime(n);
}
public void listprime(int a){
int i,j,c;
System.out.println("prime no.s upto "+a);
for(i=2;i<=a;i++){
c=0;
for(j=1;j<=i;j++){
if(i%j==0){
c++;
}}
if(c==2){
System.out.println(i);
}}}}
Output:
Run using it by command line as:

Java Prime 20

Prime no.s up to 20
2
3
5
7
11
13
17
19

15
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 15: To print reverse of digits of a given number

class Reverse
{
public static void main(String args[])
{
int n=Integer.parseInt(args[0]);
Reverse r=new Reverse();
r.rev(n);
}
public void rev(int a)
{
int rem,rev=0;
do
{
rem=a%10;
rev=(rev*10)+rem;
a=a/10;
}while(a!=0);
System.out.println("Reverse number= "+rev);
}
}

Output:
Run using it by command line as:

Java Reverse 123

Reverse number=321

16
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 16: To print


1
1 2 3
1 2 3 2 1
class PatternExp{
public static void main(String args[]){
int n=3;
int i,j,k,g,c;
for(i=1;i<=n;i++){
for(j=i;j<n;j++)
System.out.print(" ");
for(k=1;k<=i;k++)
System.out.print(k+" ");
for(g=i-1;g>=1;g--)
System.out.print(g+" ");
System.out.println();
}}}

Output:
1
1 2 3
1 2 3 2 1

17
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 17: Rewrite box program to modify volume method containing return statement.

class Box{
private int l,h,b;
public void getValue(int x,int y,int z){
l=x;
h=y;
b=z;
}
public long volume(){
return(l*h*b);
}
public void print(){
System.out.println("Box dimension is \nl="+l+"\th="+h+"\tb="+b);
}}
public class BoxExp2{
public static void main(String args[]){
long v;
Box ami=new Box();
ami.getValue(5,4,3);
v=ami.volume();
ami.print();
System.out.println("Volume is "+v);
Box ami1=new Box();
ami1.getValue(5,4,3);
v=ami1.volume();
ami1.print();
System.out.println("Volume is "+v);
}}

Output:
Box dimension is
l=5 h=4 b=3
Volume of Box is 60
Box dimension is
l=2 h=6 b=5
Volume is 60

18
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 18: Create a class to compute area of square, rectangle and triangle (use method
overloading concept)

class Area{
public void area(int l){
System.out.println("Area of Square is "+l*l);
}
public void area(int l,int b){
System.out.println("Area of rectangle is "+l*b);
}
public void area(int a,int b,int c){
int s=(a+b+c)/2;
double ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of triangle is "+ar);
}}
public class Overload{
public static void main(String args[]){
Area obj=new Area();
obj.area(5);
obj.area(3,8);
obj.area(3,4,5);
}}

Output:

Area os square is 25
Area of rectangle is 24
Area of triangle is 6.0

19
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 19: Add constructors in Box class

class Boxc
{
int l,w,b;
public Boxc()
{
l=1;
w=1;
b=1;
}
public Boxc(int x)
{
l=x;
w=x;
b=x;
}
public Boxc(int x,int y,int z)
{
l=x;
w=y;
b=z;
}
public double volume()
{
return l*w*b;
}
public static void main(String args[])
{
double v1,v2,v3;
Boxc b1=new Boxc();
v1=b1.volume();
Boxc b2=new Boxc(2);
v2=b2.volume();
Boxc b3=new Boxc(2,3,5);
v3=b3.volume();
System.out.println("v1="+v1+"v2="+v2+"v3="+v3);
}
}

Output:
V1=0 V2=8 V3=30

20
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 20: Constructor overloading in Box class

class Box{
int l,h,b;
public Box(){
System.out.println("\nBox fields initialize with zeroes ");
l=0;
h=0;
b=0;
}
public Box(int x){
System.out.println("\nOne parameters found,Box fields initialize with this
value");
l=x;
h=x;
b=x;
}
public Box(int x,int y,int z){
System.out.println("\nThree parameters found,Box fields initialize with
these value");
l=x;
h=y;
b=z;
}
public void display(){
System.out.println("Box dimension are as::\nl="+l+"\th="+h+"\tb="+b);
}}
public class BoxExp{
public static void main(String args[]){
Box ami=new Box();
ami.display();
Box ami1=new Box(5);
ami1.display();
Box ami2=new Box(4,5,6);
ami2.display();
}}

Output:
Box fields initialize with zero
Box dimension are as ::
l=0 h=0 b=0
One parameter found Box fields initialize with this value
Box dimension are as ::
l=5 h=5 b=5

Three parameters found,Box fields initialize with these values


Box dimension are as ::
l=4 h=5 b=6

21
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 21: Write a class arithmetic for calculation of addition, subtraction,


multiplication and division of two numbers.

class Arithmetic{
int a,b;
public void getVal(int a,int b){
this.a=a;
this.b=b;
}
public void add(){
System.out.println("Addition of "+a+" and "+b+" is "+(a+b));
}
public void sub(){
System.out.println("Subtraction of "+a+" and "+b+" is "+(a-b));
}
public void multi(){
System.out.println("Multiplication of "+a+" and "+b+" is "+(a*b));
}
public void div(){
System.out.println("Division of "+a+" and "+b+" is "+(a/b));
}}
class ArithmeticExp{
public static void main(String args[]){
Arithmetic ami=new Arithmetic();
ami.getVal(8,4);
ami.add();
ami.sub();
ami.multi();
ami.div();
}}

Output:
Addition of 8 and 4 is 12
Subtraction of 8 and 4 is 4
Multiplication of 8 and 4 is 32
Division of 8 and 4 is 2

22
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 22: To demonstrate call by value and call by reference .


class ArgumentPass{
int a,b;
public ArgumentPass(int v,int v1){
a=v;
b=v1;
}
public void callByValue(int a,int b){
a=a*5;
b=b%10;
}
public void callByReference(ArgumentPass obj){
obj.a=5;
obj.b=9;
}}
class Arguments{
public static void main(String args[]){
int a=55,b=5;
ArgumentPass ami=new ArgumentPass();
System.out.println("Call by value");
System.out.println("Before call the method a= "+a+"\tb="+b);
ami.callByValue(a,b);
System.out.println("After call the method a= "+a+"\tb="+b);
System.out.println("Call by reference");
ArgumentPass ami1=new ArgumentPass(2,3);
System.out.println("Before call the method a= "+ami1.a+"\tb="+ami1.b);
ami1.callByReference(ami1);
System.out.println("After call the method a= "+ami1.a+"\tb="+ami1.b);
}}

Output:
Call by value
Before call the method a=55 b=5
After call the method a=55 b=5
Call by reference
Before call the method a=2 b=3
After call the method a=5 b=9

23
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 23: Create a base class shape. It contains 2 methods get() and print () to
accept and display parameters of shape respectively. Create a subclass
Rectangle. It contains a method to display length and breadth of rectangle
(Method overrifing).

class Shape{
int a,b;
String shapetype;
public Shape(String s){
a=b=0;
shapetype=s;
}
public void get(int a,int b){
this.a=a;
this.b=b;
}
public void print(){
System.out.println("Shape Dimension\ta="+a+"\tb="+b);
}}
class Rectangle extends Shape{
public Rectangle(){
super("Rectangle");
}
public void print(){
System.out.println("Shape type is Rectangle");
super.print();
}}
class ShapeExp {
public static void main(String args[]){
Rectangle ami=new Rectangle();
ami.get(5,6);
ami.print(); } }

Output:
Shape type is rectangle
Shape dimension length=5 breadth=6

24
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 24: Write a program to show Dynamic method dispatch concept

class A{
void print(){
System.out.println("you call the class A method");
}}
class B extends A{
void print(){
System.out.println("you call the class B method");
}}
class C extends A{
void print(){
System.out.println("you call the class A method");
}}
class DynamicMethod {
public static void main(String args[]){
A obj1=new A();
B obj2=new B();
C obj3=new C();
obj1.print();
obj2.print();
obj3.print();
A r;
r=obj2;
r.print();
r=obj3;
r.print(); } }

Output:
You call the class A method
You call the class Bmethod
You call the class C method
You call the class B method
You call the class C method

25
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 25: Write an abtract class Employee with three variables name, sal and
Grosssal , suitable constructors, mprint method and two abstract methods
calculategrosssalary() and annualincrement(). Create a manager sub class
of employee with Hra as member variable and write implimentation of the
abstract method. Also create a subclass of manager as sales manger with
commision as member variable and override the calulategrosssalary() method.

abstract class Employee{


String name;
double basic,gross;
abstract double calculateSal();
abstract void annualIncrement();
}
class Manager extends Employee{
double hra;
public Manager(){
hra=0;
basic=1000;
name="Amit";
}
public Manager(double b,String c){
hra=0;
basic=b;
name=c;
}
double calculateSal(){
hra=basic*5/100;
gross=basic+hra;
return gross;
}
void annualIncrement(){
double insal=basic*10/100;
basic=basic+insal;
System.out.println("Annual Increment in salary is "+insal);
}
void print(){
System.out.println("Employee Name::"+name+"\tBasic
pay::"+basic+"\tHra::"+hra);
}}
class SalesPerson extends Employee{
double ta,com;
public SalesPerson(){
ta=0;
com=0;
basic=1000;
name="Amit";
26
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

}
public SalesPerson(double b,String c){
ta=0;
com=0;
basic=b;
name=c;
}
double calculateSal(){
ta=1000;
com=basic*5/100;
gross=basic+ta+com;
return gross;
}
void annualIncrement(){
double insal=basic*5/100;
basic=basic+insal;
System.out.println("Annual Increment in salary is "+insal);
}
void print(){
System.out.println("Employee Name::"+name+"\tBasic
pay::"+basic+"\tTa::"+ta+"\tCommision::"+com);
}}
class Emp{
public static void main(String args[]){
double v;
Manager obj1=new Manager(5000.0,"Suneel");
System.out.println("For Manager class \n");
v=obj1.calculateSal();
obj1.print();
System.out.println("Gross Salary::"+v);
obj1.annualIncrement();
v=obj1.calculateSal();
System.out.println("After annual Increment ");
obj1.print();
System.out.println("Gross Salary::"+v);
System.out.println("\n\nFor Sales Person class \n");
SalesPerson obj2=new SalesPerson(5000.0,"Suneel");
v=obj2.calculateSal();
obj2.print();
System.out.println("Gross Salary::"+v);
obj2.annualIncrement();
v=obj2.calculateSal();
System.out.println("After annual Increment ");
obj2.print();
System.out.println("Gross Salary::"+v);
}}

27
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 26: Write a program to show the use of Interfaces (show multipe inheritence as
discussed in class).
class Student{
String name;
int rollNo;
Student(){
name="";
rollNo=0;
}
public final void input(String name,int rollNo){
this.name=name;
this.rollNo=rollNo;
}
public void output(){
System.out.println("Roll No::"+rollNo+"\tName::"+name);
}}
interface Exam
{
final static int TOTAL=300;
public void total(double phy,double chem,double math);
public void per();
}
class StudentDetails extends Student implements Exam{
double phy,chem,math,t,p;
public StudentDetails(){
super();
phy=chem=math=0;
}
public void total(double phy,double chem,double math){
t=phy+chem+math;
}
public void per(){
p=(t/TOTAL)*100;
}
public void output(){
super.output();
System.out.println("Total="+t+"\tMarks(%)="+p);
}}
class Students{
public static void main(String args[]){
StudentDetails ami=new StudentDetails();
ami.input("Arun",105);
ami.total(78,79,88);
ami.per();
ami.output();
}}
28
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 27: Demonstrate Exception handling using single try catch block.

class Exception1{
public static void main(String args[]){
int a=5,b=0,c=0;
try{
c=a/b;
System.out.println("a/b= "+c);
}
catch(ArithmeticException e){
System.out.println("Error Detect:"+e);
}}}

Output:

Error detect:Java.lang.ArithmeticException;/by zero

29
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 28: Show use of multiple catch block.

class Exception2{
public static void main(String args[]){
int arr[]={3,5,6,7,4,12};
int a=8,b=0,c;
try{
int ar[]=new int[5];
for(int i=0;i<arr.length;i++)
ar[i]=arr[i]*2;
c=a/b;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error Detect "+e);
}
catch(NegativeArraySizeException e){
System.out.println("Error Detect "+e);
}
catch(ArithmeticException e){
System.out.println("Error Detect "+e);

}}}

Output:

Error Detect java.lang.ArrayIndexOutOfBoundsException:5

30
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 29: Show use of finally block

class Exception3{
public static void main(String args[]){
int arr[]={3,5,6,7,4,12};
int a=8,b=0,c;
try{
int ar[]=new int[5];
for(int i=0;i<arr.length;i++)
ar[i]=arr[i]*2;
c=a/b;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Error Detect "+e);
}
catch(NegativeArraySizeException e){
System.out.println("Error Detect "+e);
}
catch(ArithmeticException e){
System.out.println("Error Detect "+e);
}
finally{
System.out.println("Finally block");
for(int i=0;i<arr.length;i++)
System.out.print("\t"+arr[i]);
}}}

Output:

Error Detect java.lang.ArrayIndexOutOfBoundsException:5

Finally block
3 5 6 7 4 12

31
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 30: Write a program to show use of throw and trhows keyword. Create your own
exception class AgeException. Also create two subclassess TooYoungException and
InvalidAgeException of AgeExcep-tion as discussed in class.

class AgeException extends Exception{


int age;
public AgeException(String msg){
super(msg);
}}
class TooYoungException extends AgeException{
int agelimit;
public TooYoungException(int age,int al,String msg){
super(msg);
this.age=age;
agelimit=al;
}
public TooYoungException(){
super("Too Young");
}}
class NegAgeException extends AgeException{
public NegAgeException(String m){
super(m);
}}
class Exception4{
public static void vote(int age)throws TooYoungException,NegAgeException{
System.out.println("Age="+age);
if(age<18&&age>0)
throw new TooYoungException();
else if(age<0)
throw new NegAgeException("Invalid age");
else
System.out.println("You are eligible for vote");
}
public static void main(String args[]){
int ages[]={-2,12,25,19};
for(int i=0;i<ages.length;i++){
try{
vote(ages[i]);
}
catch(NegAgeException e){
System.out.println(e);
}
catch(TooYoungException e){
System.out.println(e);
}
catch(AgeException e){
System.out.println(e);
}}}}

32
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 31:Tto create an applet showing different AWT controls like buttons,check box,list
box,choice,labels

/*<applet code=”AWT create” height=600 width=600>


</applet>*/

import java.awt.*;
import java.applet.*;

public class AWTcreate extends Applet


{
public void init()
{

Button button1 = new Button();


button1.setLabel("My Button 1");
add(button1);

Checkbox checkBox1 = new Checkbox();


checkBox1.setLabel("My Checkbox 1");
add(checkBox1);

List list = new List(5, true);


list.add("One");
list.add("Two");
list.add("Three");
list.add("Four");
list.add("Five");
add(list);
list.select(1);

Choice language = new Choice();


language.add("Java");
language.add("C++");
language.add("VB");
add(language);

Label label1 = new Label();


label1.setText("My Label 1");
add(label1);
}
}

33
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 32:WAP to demonstrate events to buttons, checkbox,checkbox group, Labels.

/*<applet code=”EventApplet” height=500 width=350>


</applet>*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class EventApplet extentds Applet implements ActionListener,ItemListener
{String actionMessage="";
Checkbox java = null;
Checkbox vb = null;
Checkbox c = null;
public void init()
{Button Button1 = new Button("Ok");
add(Button1);
Button1.addActionListener(this);
java = new Checkbox("Java");
vb = new Checkbox("Visual Basic");
c = new Checkbox(“C");
add(java);
add(vb);
add(c);
java.addItemListener(this);
vb.addItemListener(this);
c.addItemListener(this);
CheckboxGroup lngGrp = new CheckboxGroup();
Checkbox java = new Checkbox("Java", lngGrp, true);
Checkbox vb = new Checkbox("Visual Basic", lngGrp, true);
add(java);
add(vb);
Label lb=new Label();
lb.setText(“Java”);
add(lb);}
public void paint(Graphics g){
g.drawString(actionMessage,10,50);
}
public void actionPerformed(ActionEvent ae)
{
String action = ae.getActionCommand();
if(action.equals("Ok"))
actionMessage = "Ok Button Pressed";
repaint();
if(action.equals(“Java”))
actionMessage=”Java is selected”;
if(action.equals(“Visual Basic”))
actionMessage=”Visual Basic is selected”;
if(action.equals(“C”)
actionMessage=”C is selected”;
repaint();}
public void itemStateChanged(ItemEvent ie){
Reapint();}
34
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 33:mouse event

/*<applet code=”MouseDemo” height=300 width=600>


</applet>*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MouseDemo extends Applets implements MouseListener,MouseMotion Listener{
String args=” “;
int x=0;
int y=0;
public void init(){
Add MouseListener(this);
Add MouseMotionListener(this);}
public void mouseClicked(MouseEvent m){
x=0, y=10;
x=m.getX();
y=m.getY();
showStatus(“Mouse is clicked”);
repaint();}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
showStatus(“Mouse is entered”);
repaint();
}
public void mouseExited(MouseEvent m)
{
x=m.getX();
y=m.getY();
showStatus(“Mouse is exited from the screen”);
repaint();
}
public void mousePressed(MouseEvent m)
x=m.getX();
y=m.getY();
showStatus(“Mouse is pressed now”);
repaint();
}
public void mouseReleased(MouseEvent m)
{
x=m.getX();
y=m.getY();
showStatus(“Mouse is released now”);
repaint();
}
public void mouseMoved(MouseEvent m)
{
x=m.getX();
y=m.getY();
35
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

showStatus(“the mouse is moved”);


repaint();
}
public void mouseDragged(MouseEvent m)
{
x=m.getX();
y=m.getY();
showStatus(“the mouse is dragged”);
repaint();
}
public void paint(Graphics GA)
{
GA.setFont(f1);
GA.setColor(Color.red);
GA.drawString(“This applet is for mouse event”,20,120);
}
}

36
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 34: key event

/*<applet code=KeyEvent1 width=500 height=700>


</applet>*/

import java.awt.*;
import java.applet.*;
inport java.awt.event.*;

public class KeyEvent1 extends Applet implements KeyListener


{
public void init()
{
addKeyListener(this);
}
Public void keyTyped(KeyEvent KB){}
Public void keyReleased(KeyEvent KB)
{
showStatus(“Key on the keyboard is released”);
}
Public void keyPressed(KeyEvent KB)
{
showStatus(“A key is pressed”);
}
Font f1=new Font(“Times new Roman”,Font.BOLD, 20);
public void paint(Graphics GA)
{
GA.setFont(f1);
GA.setColor(Color.red);
GA.drawString(“This applet is for key event”,20,120);
}
}

37
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 35: Applet calculator

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*<applet code="MouseDemo" height=500 width=300>


</applet>*/

public class MouseDemo extends Applet implements ActionListener


{

Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4;

public void init()


{
l1=new Label("Number 1");
l2=new Label("Number 2");
l3=new Label("Result");

t1=new TextField(20);
t2=new TextField(20);
t3=new TextField(20);

b1=new Button("+");
b2=new Button("-");
b3=new Button("*");
b4=new Button("/");

add(l1);
add(t1);

add(l2);
add(t2);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
add(b1);
add(b2);
add(b3);
add(b4);

add(l3);
38
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

add(t3);
}

public void actionPerformed(ActionEvent e)


{
String s=e.getActionCommand();
double a,b,c=0;
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t2.getText());

if(s=="+")
{
c=a+b;
}
if(s=="-")
{
c=a-b;
}
if(s=="*")
{
c=a*b;
}
if(s=="/")
{
c=a/b;
}

String r=Double.toString(c);
t3.setText(r);
}
}

39
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 36: Menu driven


import java.awt.*;
import java.awt.event.*;

public class MenuDemo extends Frame implements ActionListener


{
Label l1=new Label();

public MenuDemo()
{
add(l1);
MenuBar m=new MenuBar();
setMenuBar(m);

Menu file=new Menu("File");


m.add(file);

Menu edit=new Menu("Edit");


m.add(edit);

MenuItem n=new MenuItem("New");


file.add(n);

MenuItem o=new MenuItem("Open");


file.add(o);

MenuItem s=new MenuItem("Save");


file.add(s);

MenuItem cut=new MenuItem("Cut");


edit.add(cut);

MenuItem copy=new MenuItem("Copy");


edit.add(copy);

MenuItem paste=new MenuItem("Paste");


edit.add(paste);

n.addActionListener(this);
o.addActionListener(this);
s.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
}

public void actionPerformed(ActionEvent e)


40
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

{
String s=e.getActionCommand();

if(s=="New")

l1.setText("New is clicked");

if(s=="Open")
l1.setText("Open is clicked");

if(s=="Save")

l1.setText("Save is clicked");

if(s=="Cut")

l1.setText("Cut is clicked");

if(s=="Copy")

l1.setText("Copy is clicked");

if(s=="Paste")

l1.setText("Paste is clicked");

public static void main(String args[])


{
MenuDemo somya=new MenuDemo();
somya.setVisible(true);
}
}

41
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)

Program 37: : IO console


// ConsoleIO.java - Reads and prints lines from/to console.

import java.io.*;
class ConsoleIO {
public static void main(String[] args) throws IOException {
// In order to read a line at a time from System.in,
// which is type InputStream, it must be wrapped into
// a BufferedReader, which requires wrapping it first
// in an InputStreamReader.
// Note the "throws" clause on the enclosing method (main).

InputStreamReader isr = new InputStreamReader(System.in);


BufferedReader input = new BufferedReader(isr);

String line; // holds each input line

// Read and print lines in a loop.


// Terminate with EOF: control-Z (Windows) or control-D (other)
while ((line = input.readLine()) != null) {
System.out.println(line); // process each line
}
}
}

42
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114

You might also like