BCSL 43 Java Lab
BCSL 43 Java Lab
UNIVERSITY
IGNOU
FOR
BCA COURSE
(BACHELOR OF COMPUTER APPLICATIONS)
IGNOU
NAME:
Address:
Phone No:
Enrolment No:
Program Title:
Course Title:
Semester:
FOUR
Course Code:
BCSL-043
Study Centre:
External Examiner
Internal in Charge
IGNOU
CONTENTS
SL.
No
PROGRAM DESCRIPTION
PAGE
No.
4
6
7
8
10
12
13
15
16
18
19
20
22
23
26
29
35
Write a Java Program to define a class, describe its constructor, overload the
Constructors and instantiate its object :
import java.lang.*;
class studentdemo
{
public static void main(String arg[])
{
student s1=new student();
student s2=new student("JOHN",34266,58,96,84);
IGNOU
}
class student
{
String name;
int regno;
int marks1,marks2,marks3;
// null constructor
student()
{
name="RAJU";
regno=12345;
marks1=56;
marks2=47;
marks3=78;
}
// parameterized constructor
student(String n,int r,int m1,int m2,int m3)
{
name=n;
regno=r;
marks1=m1;
marks2=m2;
marks3=m3;
}
// copy constructor
student(student s)
{
name=s.name;
regno=s.regno;
marks1=s.marks1;
marks2=s.marks2;
marks3=s.marks3;
}
}
IGNOU
void display()
{
System.out.println(name + "\t" +regno+ "\t" +marks1+ "\t" +marks2+ "\t" +
marks3);
}
OUTPUT
Program: 2
Write a Java Program to define a class, define instance methods and overload them and
use them for dynamic method invocation.
import java.lang.*;
class add_demo
{
public static void main(String arg[])
{
add obj=new add();
IGNOU
obj.display(10,20);
obj.display(10.2,20.2);
}
}
class add
{
void display(int a,int b)
{
int c=a+b;
System.out.println("THE SUM OF " + a + " & " + b + " IS " + c);
}
void display(double a,double b)
{
double c=a+b;
System.out.println("THE SUM OF " + a + " & " + b + " IS " + c);
}
}
OUTPUT
Program: 3
Write a Java Program to demonstrate use of nested class.
import java.lang.*
class outer
{
int m=10;
class inner
{
int n=20;
IGNOU
void display()
{
System.out.println("m = "+m);
System.out.println("n = "+n);
}
}
}
class nesteddemo
{
public static void main(String arg[])
{
outer outobj=new outer();
outer.inner inobj=outobj.new inner();
inobj.display();
}
}
OUTPUT
Program: 4
Write a Java program to demonstrate usage of String class and its methods.
import java.lang.String;
class stringdemo
{
public static void main(String arg[])
{
String s1=new String("sacret heart college");
String s2="SACRET HEART COLLEGE";
IGNOU
OUTPUT
IGNOU
Program: 5
IGNOU
OUTPUT
IGNOU
10
Program: 6
IGNOU
11
Program: 7
IGNOU
12
13
catch(Exception e) { }
}
void putmarks()
{
System.out.println("Mark of Subject 1 = "+m1);
System.out.println("Mark of Subject 2 = "+m2);
System.out.println("Mark of Subject 3 = "+m3);
}
}
class Result extends Marks
{
private float total;
void compute_display()
{
total=m1+m2+m3;
System.out.println("TOTAL MARKS :" +total);
}
}
OUTPUT
Program: 8
IGNOU
14
Program: 9
IGNOU
15
16
Program: 10
IGNOU
17
Program: 11
IGNOU
18
Program: 12
IGNOU
19
20
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
OUTPUT
Program: 13
IGNOU
21
Program: 14
IGNOU
22
Write a Java applet program to create a simple calculator. This calculator should
perform +, , *, /. You need to take care of exceptions handling properly in this
program.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
public class Cal 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);
add(mul);
add(div);
IGNOU
23
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);
24
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("");
}
}
OUTPUT
Program: 15
IGNOU
25
26
System.out.print("\t"+diff[i][j]);
}
System.out.println(" ");
}
System.out.println("3. Transpose");
int trans[][]=new int[3][3];
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
trans[i][j]=matrix1[j][i];
System.out.print("\t"+trans[i][j]);
}
System.out.println(" ");
}
System.out.println("4. Multiplication: ");
int prod[][]=new int[3][3];
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
prod[i][j]=0;
{
for(k=0; k<3; k++)
prod[i][j]=prod[i][j]+matrix1[i][k]*matrix2[k][j];
}
System.out.print("\t"+prod[i][j]);
}
System.out.println(" ");
}
OUTPUT
IGNOU
27
Program: 16
IGNOU
28
29
{
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void setDrawMode(int mode)
{
switch (mode)
{
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}
public void mouseDragged(MouseEvent e)
{
e.consume();
switch (mode)
{
case LINES:
x2 = e.getX();
y2 = e.getY();
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x1 = e.getX();
y1 = e.getY();
break;
}
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
e.consume();
switch (mode)
{
case LINES:
x1 = e.getX();
y1 = e.getY();
x2 =-1;
break;
case POINTS:
IGNOU
30
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(e.getX(), e.getY(),-1,-1));
x1 = e.getX();
y1 = e.getY();
repaint();
break;
}
}
public void mouseReleased(MouseEvent e)
{
e.consume();
switch (mode)
{
case LINES:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 =-1;
break;
case POINTS:
default:
break;
}
repaint();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void paint (Graphics g)
{
int np = lines.size();
/* draw the current lines */
g.setColor(getForeground());
for (int i=0; i < np; i++)
{
Rectangle p = (Rectangle)lines.elementAt(i);
g.setColor((Color)colors.elementAt(i));
if (p.width !=-1)
{
g.drawLine(p.x, p.y, p.width, p.height);
}
else
{
g.drawLine(p.x, p.y, p.x, p.y);
IGNOU
31
}
}
if (mode == LINES)
{
g.setColor(getForeground());
if (x2 !=-1)
{
g.drawLine(x1, y1, x2, y2);
}
}
}
class DrawControls
extends Panel implements ItemListener
{
DrawPanel target;
public DrawControls(DrawPanel target)
{
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();
Checkbox b;
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.red);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.green);
add(b = new Checkbox(null,group, false));
b.addItemListener(this);
b.setForeground(Color.blue);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.pink);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.orange);
add(b = new Checkbox(null, group, true));
b.addItemListener(this);
b.setForeground(Color.black);
target.setForeground(b.getForeground());
Choice shapes = new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.setBackground(Color.lightGray);
add(shapes);
IGNOU
32
}
public void paint(Graphics g)
{
Rectangle r = getBounds();
g.setColor(Color.lightGray);
g.draw3DRect(0, 0, r.width, r.height, false);
int n = getComponentCount();
for(int i=0; i<n; i++)
{
Component comp = getComponent(i);
if (comp instanceof Checkbox)
{
Point loc = comp.getLocation();
Dimension d = comp.getSize();
g.setColor(comp.getForeground());
g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
}
}
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() instanceof Checkbox)
{
target.setForeground(((Component)e.getSource()).getForeground());
}
else if (e.getSource() instanceof Choice)
{
String choice = (String) e.getItem();
if (choice.equals("Lines"))
{
target.setDrawMode(DrawPanel.LINES);
}
else if (choice.equals("Points"))
{
target.setDrawMode(DrawPanel.POINTS);
}
}
}
OUTPUT
IGNOU
33
Program 17
IGNOU
34
Write a program in Java to create a Player class. Inherit the classes Cricket_Player,
Football_Player and Hockey_Player from Player class.
class main_player
{
public static void main (String args[])
{
cricket_player c=new cricket_player("AMEER","CRICKET",25);
football_player f=new football_player("ARUN", "FOOTBALL", 28);
hockey_player h=new hockey_player("RAM","HOCKEY",24);
c.show();
f.show();
h.show();
}
}
class player
{
String name;
int age;
player(String n, int a)
{
name=n;
age=a;
}
void show()
{
System.out.println("\n");
System.out.println("Player name: "+name);
System.out.println("Age :"+age);
}
}
class cricket_player extends player
{
String type;
cricket_player (String n, String t, int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type :"+type);
}
}
class football_player extends player
IGNOU
35
String type;
football_player (String n, String t, int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
OUTPUT
IGNOU
36
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
IGNOU
37