VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES
TECHNICAL CAMPUS
VIVEKANANDA SCHOOL OF INFORMATION TECHNOLOGY
BACHELOR OF COMPUTER APPLICATIONS
Practical- VIII Java Lab
Subject Code – BCA 272
GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY
SUBMITTED TO: SUBMITTED BY:
Dr. Neha Verma Malhotra Utsav Pahwa
Associate Professor 12817702022
VSIT-VIPS BCA – 4C
Ques1. Write a Java program to print all odd numbers between 1 to 10.
CODE
public class odd {
public static void main(String args[]) {
int n = 10;
int i;
[Link]("odd numbers are: ");
for (i = 1; i <= n; i++) {
if (i % 2 != 0) {
[Link](i + " ");
}
}
}
}
OUTPUT
Ques2. Write a Java program to find out factorial of a number through
recursion.
CODE
public class fact {
static int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
public static void main(String args[]) {
int ans = factorial(5);
[Link]("factorial of a number is :" + ans);
}
}
OUTPUT
Ques3. Write a Java program to accept command line arguments & print
them.
CODE
public class cmd {
public static void main(String args[]) {
[Link]("No. of command line arguments " +
[Link]); for (int i = 0; i < [Link]; i++) {
[Link](args[i]); }
}
}
OUTPUT
Ques4. Write a Java program to print fibonacci series.
CODE
public class fibonacci {
static void fiboci(int n) {
int num1 = 0;
int num2 = 1;
for (int i = 0; i < n; i++) {
[Link](num1 + "");
int num3 = num1 + num2; num1 = num2;
num2 = num3;
}
}
public static void main(String args[]) {
fiboci(13);
}
}
OUTPUT
Ques5. Write a Java program that creates a class accounts with following details:
Instance variables: ac_no., name, ac_name, balance
Methods: withdrawal (), deposit (), display ().use constructors to initialize
members
CODE
class accounts {
int acc_no;
String name;
String ac_name;
double balance;
accounts(int a, String n, String an, double b){
acc_no=a; name=n; ac_name=an; balance=b;
}
void display(){
[Link]("Account number: "+acc_no);
[Link]("Account holder: "+name);
[Link]("Account name: "+ac_name);
[Link]("Account balance: "+balance);
}
double withdraw(){
balance=balance-5000;
return balance;
}
double deposit(){ balance=balance+8000; return balance;
}
}
public class account_demo{
public static void main(String args[]) {
accounts a=new accounts(123456789,"soniya Gupta","Current
Account",50000.00);
[Link]();
double wd=[Link]();
double dp=[Link]();
[Link]("Account balance after withdrawal: "+wd);
[Link]("Account balance after deposit: "+dp);
}
}
OUTPUT
Ques6. Write a Java program to implement constructor overloading.
CODE
class Box {
double length, breath, height;
Box() {
length = breath = height = 20;
}
Box(double w, double b, double h) {
length = w;
breath = b;
height = h;
}
double volume() {
return (length * breath * height);
}
}
class construct_demo {
public static void main(String args[]) {
Box obj1 = new Box();
Box obj2 = new Box(2, 5, 7);
double vol;
vol = [Link]();
[Link]("volume is :" + vol);
vol = [Link]();
[Link]("volume is :" + vol);
}
}
OUTPUT
Ques7. Write a Java program to count the no. of objects created in a program.
CODE
class object_counter {
static int count = 0;
object_counter() {
count++;
}
static int get_count() {
return count;
}
}
class object_demo {
public static void main(String args[]) {
object_counter obj1 = new object_counter();
object_counter obj2 = new object_counter();
object_counter obj3 = new object_counter();
object_counter obj4 = new object_counter();
[Link]("no. of object:" + object_counter.get_count());
}
}
OUTPUT
Ques8. Write a Java program to show call by value & call by reference.
CODE
class MyClass {
int value;
MyClass(int value) {
[Link] = value;
}
}
public class qestion9 {
public static void main(String[] args) {
int x = 10;
MyClass obj = new MyClass(20);
[Link]("Before callByValue - x: " +
x); callByValue(x);
[Link]("After callByValue - x: " + x);
[Link]("Before callByReference - [Link]: " + [Link]);
callByReference(obj);
[Link]("After callByReference - [Link]: " + [Link]);
}
public static void callByValue(int a)
{ a = 100;
}
public static void callByReference(MyClass obj) {
[Link] = 200;
}
}
OUTPUT
Ques9. Write a Java program to implement method over ridding & method
overloading.
CODE
//method overloading
class Method {
static void add(int a, int b) {
int c = a + b;
[Link]("the sum is" + c);
}
static void add(double a, double b) {
double c = a + b;
[Link]("the sum is" + c);
}
static void add(double a) {
double c = a + 50.2;
[Link]("the sum is" + c);
}
public static void main(String args[]) {
[Link](4, 5);
[Link](4.5, 6.7);
[Link](7.5);
}
}
// method overriding
class Bike {
void run() {
[Link]("Bike is running");
}
}
class Hero extends Bike {
void run() {
[Link]("Hero bike is running");
}
}
public class Runtime {
public static void main(String[] args) {
Bike h = new Hero();
[Link]();
}
}
OUTPUT
Ques10. Create a class box having height, width, depth as the instance variables
& calculate its volume. Implement constructor overloading in it. Create a
subclass named box_new that has weight as an instance variable. Use super in
the box_new class to initialize members of the base class.
CODE
class box {
int height;
int width;
int depth;
box() {
[Link]("The defaukt constructor has been called");
}
box(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
void volume() {
int v = height * depth * width;
[Link]("Volume of the box:" + v);
}
}
class box_new extends box {
int weight;
box_new(int w, int h, int d, int we)
{ super(we, h, d);
weight = we;
}
void display() {
[Link]("Weight:" + weight);
}
}
public class Ques10 {
public static void main(String[] args) {
box_new b = new box_new(23, 62, 20, 10);
[Link]();
[Link]();
}
}
OUTPUT
Ques11. Write a Java program to implement run time
polymorphism. CODE
class A {
void show_me() {
[Link]("show inside A");
}
}
class B extends A {
void show_me() {
[Link]("show inside B");
}
}
class C extends B {
void show_me() {
[Link]("show inside C");
}
}
class abc {
public static void main(String args[])
{ A a = new A();
B b = new B();
C c = new C();
A r;
r = a;
r.show_me();
r = b;
r.show_me();
r = c;
r.show_me();
}
}
OUTPUT
Ques12. Write a Java program to implement interface. Create an interface
named shape having area () & perimeter () as its methods. Create three classes
circle, rectangle & square that implement this interface.
CODE
interface Shape {
void area();
void perimeter();
}
class Circle implements Shape {
int r = 5;
public void area() {
double area = (3.14) * r * r;
[Link]("Area of circle:" + area);
}
public void perimeter() {
double per = 2 * 3.14 * r;
[Link]("Perimter of circle:" + per);
}
}
class Square implements Shape {
int a = 10;
public void area() {
int area = a * a;
[Link]("Area of sqaure:" + area);
}
public void perimeter() {
int per = 4 * a;
[Link]("Perimeter of square" + per);
}
}
class Rectangle implements Shape {
int l = 10;
int b = 5;
public void area() {
int area = l * b;
[Link]("Area of rectangle:" + area);
}
public void perimeter() {
int per = 2 * (l + b);
[Link]("Perimeter of rectangle:" + per);
}
}
public class ques12 {
public static void main(String[] args) {
Circle c = new Circle();
Rectangle r = new Rectangle();
Square s = new Square();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT
Ques13. Write a Java program to show multiple inheritance.
CODE
interface Vehicle {
void exists();
}
interface car {
void wheels();
}
class Transport implements Vehicle, car
{ public void exists() {
[Link]("It is existing");
}
public void wheels() {
[Link]("It has four wheels");
}
}
public class mulinher {
public static void main(String[] args) {
Transport t1 = new Transport();
[Link]();
[Link]();
}
}
OUTPUT
Ques14. Write a Java program to implement exception handling. Use try, catch
& finally.
CODE
class exceptiondemo {
public static void main(String args[]) {
int a[] = { 2, 6 };
int b = 5;
try {
int x = a[2] / b - a[1];
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("array index error");
} finally {
[Link]("finally always executed");
}
}
}
OUTPUT
Ques15. Write a Java program to implement matrix multiplication by 2d array.
CODE
class matrices {
public static void main(String args[]){
int[][] m1 = { {2,1,4}, {4,6,1}, {3,8,7} };
int[][] m2 = { {1,2,3}, {0,6,5}, {5,3,9} };
int rows = [Link];
int cols1 = m1[0].length;
int cols2 = m2[0].length;
int[][] result = new int[rows][cols2];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += m1[i][k] * m2[k][j];
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols2; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}
OUTPUT
Ques16. Write a Java program to implement vector [use:
addelement(), elementat(), removeelement(), size().]
CODE
import [Link].*;
class vec {
public static void main(String args[]){
Vector<String> v = new Vector<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
String element = [Link](1);
[Link]("Element at index 1: " + element);
[Link]("Banana");
int s = [Link]();
[Link]("Size of vector: " + s);
[Link](v);
}
}
OUTPUT
Ques17. Create a user defined exception named “nomatchexception” that is fired
when the string entered by the user is not “india”
CODE
class NomatchException extends Exception {
public NomatchException() {
super("String does not match 'india'");
}
}
public class ex {
public static void main(String[] args) {
try {
String userInput = "example";
if () {
throw new NomatchException();
}
[Link]("String matches 'india'");
} catch (NomatchException e) {
[Link]([Link]());
}
}
}
OUTPUT
Ques18. Write a Java program to show even & odd numbers by thread.
CODE
public class EvenOddThread {
public static void main(String[] args) {
Printer printer = new Printer();
Thread evenThread = new Thread(new EvenRunnable(printer,
10), "EvenThread");
Thread oddThread = new Thread(new OddRunnable(printer, 10), "OddThread");
[Link]();
[Link]();
}
static class Printer {
private volatile boolean isEven = true;
public synchronized void print(int number) {
while ((number % 2 == 0 && !isEven) || (number % 2 != 0 && isEven)) {
try {
wait();
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
[Link]([Link]().getName() + ": " + number);
isEven = !isEven;
notifyAll();
}
}
static class EvenRunnable implements Runnable {
private final Printer printer;
private final int max;
public EvenRunnable(Printer printer, int max)
{ [Link] = printer;
[Link] = max;
}
public void run() {
for (int i = 2; i <= max; i += 2) {
[Link](i);
}
}
}
static class OddRunnable implements Runnable {
private final Printer printer;
private final int max;
public OddRunnable(Printer printer, int max)
{ [Link] = printer;
[Link] = max;
}
public void run() {
for (int i = 1; i <= max; i += 2) {
[Link](i);
}
}
}
}
OUTPUT
Ques19. Write a Java program that draws different color shapes on an applet .set
the foreground & background color as red & blue.
CODE
OUTPUT
Ques20. Write a Java program to show moving banner by
applet, CODE
OUTPUT
Ques21. Write a Java program to demonstrate the use of equals(), trim()
,length() , substring(), compareTo() of string
class. CODE
class str {
public static void main(String args[]){
String str1="Hello";
String str2="world";
String str3="Java programming";
[Link]("string 1=" +str1 );
[Link]("string 2=" +str2 );
[Link]("using equals function " + [Link](str2));
[Link]("using trim function " + [Link]());
[Link]("using length function " + [Link]());
[Link]("using substring function " + [Link](1,3));
[Link]("using campareto function " + [Link](str2));
}
}
OUTPUT
Ques22. Write a Java program to demonstrate the use of equals() and == in Java.
CODE
class eq {
public static void main(String args[]){
String str1="Hello";
String str2="world";
String str3="Hello";
[Link]("string 1=" +str1 );
[Link]("string 2=" +str2 );
[Link]("string 3=" +str3 );
[Link]("using equals()= " + [Link](str3));
[Link]("using double" + str1==str3 );
}
}
OUTPUT
Ques23. Write a Java program to implement all mouse events and mouse motion
events.
CODE
import [Link].*;
import [Link].*;
public class MouseEventsDemo extends Frame implements MouseListener,
MouseMotionListener {
public MouseEventsDemo() {
setTitle("Mouse Events Demo");
setSize(400, 300);
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at (" + [Link]() + ", " + [Link]() + ")");
}
public void mousePressed(MouseEvent e) {
[Link]("Mouse Pressed at (" + [Link]() + ", " + [Link]() + ")");
}
public void mouseReleased(MouseEvent e) {
[Link]("Mouse Released at (" + [Link]() + ", " + [Link]() + ")");
}
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered Frame");
}
public void mouseExited(MouseEvent e) {
[Link]("Mouse Exited Frame");
}
public void mouseDragged(MouseEvent e) {
[Link]("Mouse Dragged at (" + [Link]() + ", " + [Link]() + ")");
}
public void mouseMoved(MouseEvent e) {
[Link]("Mouse Moved at (" + [Link]() + ", " + [Link]() + ")");
}
public static void main(String[] args) {
new MouseEventsDemo();
}
}
OUTPUT
Ques24. Write a Java program to implement keyboard
events. CODE
import [Link].*;
import [Link].*;
public class Kkeyevent extends Frame implements KeyListener {
public Kkeyevent() {
setTitle("Keyboard Events
Demo"); setSize(400, 300);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed: " + [Link]([Link]()));
}
public void keyReleased(KeyEvent e) {
[Link]("Key Released: " + [Link]([Link]()));
}
public void keyTyped(KeyEvent e) {
[Link]("Key Typed: " + [Link]());
}
public static void main(String[] args) {
new Kkeyevent();
}
}
OUTPUT
Ques25. Write a Java program using AWT to create a simple calculator.
CODE