0% found this document useful (0 votes)
23 views24 pages

BCA II Java Lab Manual

The document outlines a curriculum for Object Oriented Programming using Java for the BCA II Semester, detailing various programming tasks divided into Part A and Part B. Part A includes programs for basic operations, class demonstrations, inheritance, and array manipulations, while Part B focuses on exception handling, GUI creation, and mouse event handling. Each section provides example code and expected outputs for the tasks listed.

Uploaded by

smithauk12122020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views24 pages

BCA II Java Lab Manual

The document outlines a curriculum for Object Oriented Programming using Java for the BCA II Semester, detailing various programming tasks divided into Part A and Part B. Part A includes programs for basic operations, class demonstrations, inheritance, and array manipulations, while Part B focuses on exception handling, GUI creation, and mouse event handling. Each section provides example code and expected outputs for the tasks listed.

Uploaded by

smithauk12122020
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Programming Using Java (BCA II Semester - SEP)

Part A:
1. Program to find whether the given number is Positive, Negative or Zero.
2. Program to list the factorial of the numbers 1 to 10.
3. Program to demonstrate classes & objects.
4. Program to demonstrate method overloading.
5. Program to demonstrate single inheritance (simple calculator – base class, Advanced
Calculator – derived class).
6. Program to find Maximum & Minimum element in one-dimensional array of numbers.
7. Program to check whether the given string is palindrome or not.
8. Program to create a ‘Student’ class with [Link]., name and marks of 3 subjects.
Calculate the total marks of 3 subjects and create an array of 3 student objects & display
the results.

Part B:
1. Program to generate negative array size exception
2. Program to generate NullPointer Exception.
3. Program that reads two integer numbers for the variables a and b. The program should
catch NumberFormatException & display the error message.
4. Program to create AWT window with 4 buttons M/A/E/Close. Display M for Good
Morning, A for Afternoon, E for evening and Close button to exit the window.
5. Program to demonstrate the various mouse handling events.
6. Program to read and write Binary I/O file.
7. Program to create window with three buttons father, mother and close. Display the
respective details of father and mother as name, age and designation using AWT
controls.
8. Program to create menu bar and pull-down menus.

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)

Part A

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 1. Program to find whether the given number is Positive, Negative or Zero. */

import [Link];

class Number
{
public static void main(String args[])
{
Scanner s = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
if(n > 0)
[Link]("Positive Number");
else if(n < 0)
[Link]("Negative Number");
else
[Link]("Zero");
}
}

Output 1:

D:\Java>javac [Link]

D:\Java>java Number
Enter a number:
10
Positive Number

Output 2:

D:\Java>javac [Link]

D:\Java>java Number
Enter a number:
-10
Negative Number

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 2. Program to list the factorial of the numbers 1 to 10. */

class Fact
{
public static void main(String args[])
{
int i, f = 1;
for (i = 1; i <= 10; i++)
{
f=f*i;
[Link]("Factorial of "+i+" is "+f);
}
}
}

Output:

D:\Java>javac [Link]

D:\Java>java Fact

Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
Factorial of 6 is 720
Factorial of 7 is 5040
Factorial of 8 is 40320
Factorial of 9 is 362880
Factorial of 10 is 3628800

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 3. Program to demonstrate classes & objects. */

class ClassObject
{
int x, y;

void getData(int a, int b)


{
x = a;
y = b;
}

void putData()
{
[Link]("x = "+x+" y = "+y);
}

public static void main(String args[])


{
ClassObject c1 = new ClassObject();
ClassObject c2 = new ClassObject();

[Link](10, 20);
[Link](30, 40);

[Link]();
[Link]();
}
}

Output:

D:\Java>javac [Link]

D:\Java>java ClassObject
x = 10 y = 20
x = 30 y = 40

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 4. Program to demonstrate method overloading. */

class Method
{
int add(int a, int b)
{
return a+b;
}
float add(float x, float y)
{
return x+y;
}
public static void main(String args[])
{
Method m = new Method();
[Link]([Link](10,20));
[Link]([Link](10.3f, 50.5f));
}
}

Output:

D:\Java>javac [Link]

D:\Java>java Method

30
60.8

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 5. Program to demonstrate single inheritance (simple calculator – base class, Advanced
Calculator – derived class). */

class SimpleCalculator
{
int sum(int a, int b)
{
return a+b;
}
}

class AdvancedCalculator extends SimpleCalculator


{
int product(int a, int b)
{
return a*b;
}
}

class Calculator
{
public static void main(String args[])
{
AdvancedCalculator a = new AdvancedCalculator();
[Link]([Link](10, 20));
[Link]([Link](10, 20));
}
}

Output:

D:\Java>javac [Link]

D:\Java>java Calculator

30
200

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 6. Program to find Maximum & Minimum element in one dimensional array of
numbers.*/

import [Link];
class MaxMin
{
public static void main(String args[])
{
int i, min, max;
Scanner s = new Scanner([Link]);
[Link]("Enter the size of the array: ");
int n = [Link]();
int a[] = new int[n];
[Link]("Enter the array elements: ");
for(i=0;i<n;i++)
a[i]=[Link]();

min = max = a[0];


for(i=0;i<n;i++)
{
if(min > a[i])
min = a[i];
if(max < a[i])
max = a[i];
}
[Link]("Smallest Number = "+min);
[Link]("Largest Number = "+max);
}
}

Output:
D:\Java>javac [Link]

D:\Java>java MaxMin
Enter the size of the array:
5
Enter the array elements:
1
2
3
4
5
Smallest Number = 1
Largest Number = 5

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 7. Program to check whether the given string is palindrome or not. */

class Palindrome
{
public static void main(String[] args)
{

String str=args[0];
String revStr = "";

int len = [Link]();

for (int i = len-1; i >= 0; i--)


{
revStr = revStr + [Link](i);
}

if ([Link]().equals([Link]()))
{
[Link](str + " is Palindrome");
}
else
{
[Link](str + " is not Palindrome");
}
}
}

Output 1:
D:\Java>javac [Link]

D:\Java>java Palindrome Level


Level is Palindrome

Output 2:
D:\Java>javac [Link]

D:\Java>java Palindrome Hassan


Hassan is not Palindrome

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 8. Program to create a ‘Student’ class with [Link]., name and marks of 3 subjects.
Calculate the total marks of 3 subjects and create an array of 3 student objects &
display the results. */

class Student
{
int regno, m1, m2, m3;
String name;

Student(int rno, String n, int sub1, int sub2, int sub3)


{
regno = rno;
name = n;
m1 = sub1;
m2 = sub2;
m3 = sub3;
}
int calculate()
{
return m1 + m2 + m3;
}
void display()
{
[Link]("\[Link]: " + regno);
[Link]("Name: " + name);
[Link]("Marks1: " + m1);
[Link]("Marks2: " + m2);
[Link]("Marks3: " + m3);
[Link]("Total Marks: " + calculate());
}
}
class StudentDetails
{
public static void main(String[] args)
{
Student[] s = new Student[3];

s[0] = new Student(101, "Ramesh", 85, 90, 78);


s[1] = new Student(102, "Rajesh", 76, 88, 92);
s[2] = new Student(103, "Rakesh", 92, 80, 85);

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


s[i].display();
}
}

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
Output:
D:\Java>javac [Link]

D:\Java>java StudentDetails

[Link]: 101
Name: Ramesh
Marks1: 85
Marks2: 90
Marks3: 78
Total Marks: 253

[Link]: 102
Name: Rajesh
Marks1: 76
Marks2: 88
Marks3: 92
Total Marks: 256

[Link]: 103
Name: Rakesh
Marks1: 92
Marks2: 80
Marks3: 85
Total Marks: 257

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)

Part B

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 1. Program to generate negative array size exception. */

import [Link];

class ArraySize
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

try
{
[Link]("Enter array size: ");
int size = [Link]();

int arr[] = new int[size];

[Link]("Array created successfully");


}
catch (NegativeArraySizeException e)
{
[Link]("Array size cannot be negative");
}
}
}

Output 1:
D:\Java>javac [Link]

D:\Java>java ArraySize
Enter array size: 5
Array created successfully

Output 2:
D:\Java>javac [Link]

D:\Java>java ArraySize
Enter array size: -3
Array size cannot be negative

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 2. Program to generate NullPointer Exception. */

class NullPointer
{
public static void main(String[] args)
{
try
{
String str = null;

[Link]("Length: " + [Link]());


}
catch (NullPointerException e)
{
[Link]("Error: String object is null");
}
}
}

Output:
D:\Java>javac [Link]

D:\Java>java NullPointer

Error: String object is null.

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 3. Program that reads two integer numbers for the variables a and b. The program
should catch NumberFormatException & display the error message. */

import [Link];

class NumberFormat
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);

try
{
[Link]("Enter first integer: ");
String input1 = [Link]();
int a = [Link](input1);

[Link]("Enter second integer: ");


String input2 = [Link]();
int b = [Link](input2);

int sum = a + b;
[Link]("Sum = " + sum);
}
catch (NumberFormatException e)
{
[Link]("Error: Invalid Number");
}

}
}

Output 1:
D:\Java>javac [Link]

D:\Java>java NumberFormat
Enter first integer: 10
Enter second integer: 20
Sum = 30

Output 2:
D:\Java>javac [Link]

D:\Java>java NumberFormat
Enter first integer: 10
Enter second integer: abc
Error: Invalid number

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 4. Program to create AWT window with 4 buttons M/A/E/Close. Display M for Good Morning,
A for Afternoon, E for evening and Close button to exit the window. */

import [Link].*;
import [Link].*;

class AWT
{
public static void main(String[] args)
{

Frame f = new Frame("Greeting Window");


[Link](new FlowLayout());

Button b1 = new Button("M");


Button b2 = new Button("A");
Button b3 = new Button("E");
Button b4 = new Button("Close");

final Label l1 = new Label("Click a button");

[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](l1);

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Good Morning");
}
});

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Good Afternoon");
}
});

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Good Evening");
}
});

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link](0);
}
});

[Link](300, 200);
[Link](true);
}
}

Output:
D:\Java>javac [Link]

D:\Java>java AWT

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 5. Program to demonstrate the various mouse handling events. */

import [Link].*;
import [Link].*;

public class MouseDemo extends Frame implements MouseListener


{

Label d = new Label();

MouseDemo()
{

setLayout(null);

[Link](20, 50, 200, 30);


add(d);

addMouseListener(this);

setSize(400, 300);
setTitle("Mouse Event Demo");
setVisible(true);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}

public void mouseClicked(MouseEvent e)


{
[Link]("Mouse Clicked");
setTitle([Link]() + " , " + [Link]());
}

public void mousePressed(MouseEvent e)


{
[Link]("Mouse Pressed");
setTitle([Link]() + " , " + [Link]());
}

public void mouseReleased(MouseEvent e)


{
[Link]("Mouse Released");
setTitle([Link]() + " , " + [Link]());
}

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
public void mouseEntered(MouseEvent e)
{
[Link]("Mouse Entered");
setTitle([Link]() + " , " + [Link]());
}

public void mouseExited(MouseEvent e)


{
[Link]("Mouse Exited");
setTitle([Link]() + " , " + [Link]());
}

public static void main(String[] args)


{
new MouseDemo();
}
}

Output:

D:\Java>javac [Link]

D:\Java>java MouseDemo

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 6. Program to read and write Binary I/O file. */

import [Link].*;

public class BinaryArray


{
public static void main(String[] args)
{
try
{
int arr[] = {10, 20, 30, 40, 50};

FileOutputStream fout = new FileOutputStream("[Link]");

for (int i = 0; i < [Link]; i++)


{
[Link](arr[i]);
}

[Link]();
[Link]("Array written successfully.");

FileInputStream fin = new FileInputStream("[Link]");

[Link]("Reading from file:");

int value;
while ((value = [Link]()) != -1)
{
[Link](value);
}

[Link]();
}
catch (IOException e)
{
[Link]("File Error");
}
}
}

Output:
D:\Java>javac [Link]

D:\Java>java BinaryArray
Array written successfully.
Reading from file:
10
20
30
40
50

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 7. Program to create window with three buttons father, mother and close. Display the
respective details of father and mother as name, age and designation using AWT controls. */

import [Link].*;
import [Link].*;
public class FamilyDetails
{
public static void main(String[] args)
{
Frame f = new Frame("Family Details");
[Link](new FlowLayout());
Button father = new Button("Father");
Button mother = new Button("Mother");
Button close = new Button("Close");
final Label info = new Label("");
[Link](new Dimension(350, 30));
[Link](father);
[Link](mother);
[Link](close);
[Link](info);
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Father:John, Age:50, Designation:Manager");
}
});

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Mother:Mary, Age:45, Designation:Teacher");
}
});

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link](0);
}
});

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
[Link](new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});

[Link](600, 200);
[Link](true);
}
}

Output:
D:\Java>javac [Link]

D:\Java>java FamilyDetails

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
/* 8. Program to create menu bar and pull-down menus. */

import [Link].*;
import [Link].*;

public class MenuDemo


{
public static void main(String[] args)
{
Frame f = new Frame("Menu Bar Example");
[Link](new FlowLayout());

final Label status = new Label("Select a menu item");


[Link](status);

MenuBar mb = new MenuBar();

Menu file = new Menu("File");

MenuItem open = new MenuItem("Open");


MenuItem save = new MenuItem("Save");
MenuItem exit = new MenuItem("Exit");

[Link](open);
[Link](save);
[Link](exit);

[Link](file);

[Link](mb);

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Open selected");
}
});

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Save selected");
}
});

Government Science College Hassan (Autonomous) – 573 201


Object Oriented Programming Using Java (BCA II Semester - SEP)
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link](0);
}
});

[Link](new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});

[Link](400, 300);
[Link](true);
}
}

Output:
D:\Java>javac [Link]

D:\Java>java MenuDemo

Government Science College Hassan (Autonomous) – 573 201

You might also like