OOPS Lab(ECS-353)
Program11: To print the Fibbonaci Series
class Fib
{
public static void main(String args[])
{
int a=-1,b=1,c;
[Link]("Fibonaccie series is ::");
for(int i=1;i<=10;i++)
{
c=a+b;
[Link](" "+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="";
[Link]=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(){
[Link]("Per(%)="+per+"\tGrade="+g);
}}
class GradeExp{
public static void main(String args[]){
double p=[Link](args[0]);
Grade ami=new Grade(p);
[Link]();
[Link]();
}}
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) {
[Link]("\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=[Link](args[0]);
Prime p=new Prime();
[Link](n);
}
public void listprime(int a){
int i,j,c;
[Link]("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){
[Link](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=[Link](args[0]);
Reverse r=new Reverse();
[Link](n);
}
public void rev(int a)
{
int rem,rev=0;
do
{
rem=a%10;
rev=(rev*10)+rem;
a=a/10;
}while(a!=0);
[Link]("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++)
[Link](" ");
for(k=1;k<=i;k++)
[Link](k+" ");
for(g=i-1;g>=1;g--)
[Link](g+" ");
[Link]();
}}}
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(){
[Link]("Box dimension is \nl="+l+"\th="+h+"\tb="+b);
}}
public class BoxExp2{
public static void main(String args[]){
long v;
Box ami=new Box();
[Link](5,4,3);
v=[Link]();
[Link]();
[Link]("Volume is "+v);
Box ami1=new Box();
[Link](5,4,3);
v=[Link]();
[Link]();
[Link]("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){
[Link]("Area of Square is "+l*l);
}
public void area(int l,int b){
[Link]("Area of rectangle is "+l*b);
}
public void area(int a,int b,int c){
int s=(a+b+c)/2;
double ar=[Link](s*(s-a)*(s-b)*(s-c));
[Link]("Area of triangle is "+ar);
}}
public class Overload{
public static void main(String args[]){
Area obj=new Area();
[Link](5);
[Link](3,8);
[Link](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=[Link]();
Boxc b2=new Boxc(2);
v2=[Link]();
Boxc b3=new Boxc(2,3,5);
v3=[Link]();
[Link]("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(){
[Link]("\nBox fields initialize with zeroes ");
l=0;
h=0;
b=0;
}
public Box(int x){
[Link]("\nOne parameters found,Box fields initialize with this
value");
l=x;
h=x;
b=x;
}
public Box(int x,int y,int z){
[Link]("\nThree parameters found,Box fields initialize with
these value");
l=x;
h=y;
b=z;
}
public void display(){
[Link]("Box dimension are as::\nl="+l+"\th="+h+"\tb="+b);
}}
public class BoxExp{
public static void main(String args[]){
Box ami=new Box();
[Link]();
Box ami1=new Box(5);
[Link]();
Box ami2=new Box(4,5,6);
[Link]();
}}
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(){
[Link]("Addition of "+a+" and "+b+" is "+(a+b));
}
public void sub(){
[Link]("Subtraction of "+a+" and "+b+" is "+(a-b));
}
public void multi(){
[Link]("Multiplication of "+a+" and "+b+" is "+(a*b));
}
public void div(){
[Link]("Division of "+a+" and "+b+" is "+(a/b));
}}
class ArithmeticExp{
public static void main(String args[]){
Arithmetic ami=new Arithmetic();
[Link](8,4);
[Link]();
[Link]();
[Link]();
[Link]();
}}
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();
[Link]("Call by value");
[Link]("Before call the method a= "+a+"\tb="+b);
[Link](a,b);
[Link]("After call the method a= "+a+"\tb="+b);
[Link]("Call by reference");
ArgumentPass ami1=new ArgumentPass(2,3);
[Link]("Before call the method a= "+ami1.a+"\tb="+ami1.b);
[Link](ami1);
[Link]("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(){
[Link]("Shape Dimension\ta="+a+"\tb="+b);
}}
class Rectangle extends Shape{
public Rectangle(){
super("Rectangle");
}
public void print(){
[Link]("Shape type is Rectangle");
[Link]();
}}
class ShapeExp {
public static void main(String args[]){
Rectangle ami=new Rectangle();
[Link](5,6);
[Link](); } }
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(){
[Link]("you call the class A method");
}}
class B extends A{
void print(){
[Link]("you call the class B method");
}}
class C extends A{
void print(){
[Link]("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();
[Link]();
[Link]();
[Link]();
A r;
r=obj2;
[Link]();
r=obj3;
[Link](); } }
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;
[Link]("Annual Increment in salary is "+insal);
}
void print(){
[Link]("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;
[Link]("Annual Increment in salary is "+insal);
}
void print(){
[Link]("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");
[Link]("For Manager class \n");
v=[Link]();
[Link]();
[Link]("Gross Salary::"+v);
[Link]();
v=[Link]();
[Link]("After annual Increment ");
[Link]();
[Link]("Gross Salary::"+v);
[Link]("\n\nFor Sales Person class \n");
SalesPerson obj2=new SalesPerson(5000.0,"Suneel");
v=[Link]();
[Link]();
[Link]("Gross Salary::"+v);
[Link]();
v=[Link]();
[Link]("After annual Increment ");
[Link]();
[Link]("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){
[Link]=name;
[Link]=rollNo;
}
public void output(){
[Link]("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(){
[Link]();
[Link]("Total="+t+"\tMarks(%)="+p);
}}
class Students{
public static void main(String args[]){
StudentDetails ami=new StudentDetails();
[Link]("Arun",105);
[Link](78,79,88);
[Link]();
[Link]();
}}
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;
[Link]("a/b= "+c);
}
catch(ArithmeticException e){
[Link]("Error Detect:"+e);
}}}
Output:
Error detect:[Link];/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<[Link];i++)
ar[i]=arr[i]*2;
c=a/b;
}
catch(ArrayIndexOutOfBoundsException e){
[Link]("Error Detect "+e);
}
catch(NegativeArraySizeException e){
[Link]("Error Detect "+e);
}
catch(ArithmeticException e){
[Link]("Error Detect "+e);
}}}
Output:
Error Detect [Link]
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<[Link];i++)
ar[i]=arr[i]*2;
c=a/b;
}
catch(ArrayIndexOutOfBoundsException e){
[Link]("Error Detect "+e);
}
catch(NegativeArraySizeException e){
[Link]("Error Detect "+e);
}
catch(ArithmeticException e){
[Link]("Error Detect "+e);
}
finally{
[Link]("Finally block");
for(int i=0;i<[Link];i++)
[Link]("\t"+arr[i]);
}}}
Output:
Error Detect [Link]
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);
[Link]=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{
[Link]("Age="+age);
if(age<18&&age>0)
throw new TooYoungException();
else if(age<0)
throw new NegAgeException("Invalid age");
else
[Link]("You are eligible for vote");
}
public static void main(String args[]){
int ages[]={-2,12,25,19};
for(int i=0;i<[Link];i++){
try{
vote(ages[i]);
}
catch(NegAgeException e){
[Link](e);
}
catch(TooYoungException e){
[Link](e);
}
catch(AgeException e){
[Link](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 [Link].*;
import [Link].*;
public class AWTcreate extends Applet
{
public void init()
{
Button button1 = new Button();
[Link]("My Button 1");
add(button1);
Checkbox checkBox1 = new Checkbox();
[Link]("My Checkbox 1");
add(checkBox1);
List list = new List(5, true);
[Link]("One");
[Link]("Two");
[Link]("Three");
[Link]("Four");
[Link]("Five");
add(list);
[Link](1);
Choice language = new Choice();
[Link]("Java");
[Link]("C++");
[Link]("VB");
add(language);
Label label1 = new Label();
[Link]("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 [Link].*;
import [Link].*;
import [Link].*;
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);
[Link](this);
java = new Checkbox("Java");
vb = new Checkbox("Visual Basic");
c = new Checkbox(“C");
add(java);
add(vb);
add(c);
[Link](this);
[Link](this);
[Link](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();
[Link](“Java”);
add(lb);}
public void paint(Graphics g){
[Link](actionMessage,10,50);
}
public void actionPerformed(ActionEvent ae)
{
String action = [Link]();
if([Link]("Ok"))
actionMessage = "Ok Button Pressed";
repaint();
if([Link](“Java”))
actionMessage=”Java is selected”;
if([Link](“Visual Basic”))
actionMessage=”Visual Basic is selected”;
if([Link](“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 [Link].*;
import [Link].*;
import [Link].*;
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=[Link]();
y=[Link]();
showStatus(“Mouse is clicked”);
repaint();}
public void mouseEntered(MouseEvent m)
{
x=[Link]();
y=[Link]();
showStatus(“Mouse is entered”);
repaint();
}
public void mouseExited(MouseEvent m)
{
x=[Link]();
y=[Link]();
showStatus(“Mouse is exited from the screen”);
repaint();
}
public void mousePressed(MouseEvent m)
x=[Link]();
y=[Link]();
showStatus(“Mouse is pressed now”);
repaint();
}
public void mouseReleased(MouseEvent m)
{
x=[Link]();
y=[Link]();
showStatus(“Mouse is released now”);
repaint();
}
public void mouseMoved(MouseEvent m)
{
x=[Link]();
y=[Link]();
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=[Link]();
y=[Link]();
showStatus(“the mouse is dragged”);
repaint();
}
public void paint(Graphics GA)
{
[Link](f1);
[Link]([Link]);
[Link](“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 [Link].*;
import [Link].*;
inport [Link].*;
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”,[Link], 20);
public void paint(Graphics GA)
{
[Link](f1);
[Link]([Link]);
[Link](“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 [Link].*;
import [Link].*;
import [Link].*;
/*<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);
[Link](this);
[Link](this);
[Link](this);
[Link](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=[Link]();
double a,b,c=0;
a=[Link]([Link]());
b=[Link]([Link]());
if(s=="+")
{
c=a+b;
}
if(s=="-")
{
c=a-b;
}
if(s=="*")
{
c=a*b;
}
if(s=="/")
{
c=a/b;
}
String r=[Link](c);
[Link](r);
}
}
39
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 36: Menu driven
import [Link].*;
import [Link].*;
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");
[Link](file);
Menu edit=new Menu("Edit");
[Link](edit);
MenuItem n=new MenuItem("New");
[Link](n);
MenuItem o=new MenuItem("Open");
[Link](o);
MenuItem s=new MenuItem("Save");
[Link](s);
MenuItem cut=new MenuItem("Cut");
[Link](cut);
MenuItem copy=new MenuItem("Copy");
[Link](copy);
MenuItem paste=new MenuItem("Paste");
[Link](paste);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](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=[Link]();
if(s=="New")
[Link]("New is clicked");
if(s=="Open")
[Link]("Open is clicked");
if(s=="Save")
[Link]("Save is clicked");
if(s=="Cut")
[Link]("Cut is clicked");
if(s=="Copy")
[Link]("Copy is clicked");
if(s=="Paste")
[Link]("Paste is clicked");
public static void main(String args[])
{
MenuDemo somya=new MenuDemo();
[Link](true);
}
}
41
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114
OOPS Lab(ECS-353)
Program 37: : IO console
// [Link] - Reads and prints lines from/to console.
import [Link].*;
class ConsoleIO {
public static void main(String[] args) throws IOException {
// In order to read a line at a time from [Link],
// 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([Link]);
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 = [Link]()) != null) {
[Link](line); // process each line
}
}
}
42
J.S.S.A.T.E Somya Arya
III SEM,IT-2
0909113114