Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
MATU RAM INSTITUTE OF ENGINEERING
AND MANAGEMENT
Rohtak, Haryana
PRACTICAL FILE
of
JAVA PROGRAMMING
Submitted To: Department of Computer Science
MRIEM, Rohtak
Submitted By: Himanshu
Course: Bachelor of Computer Applications (BCA)
Semester: 4th Semester
Subject: Java Programming
Session: 2024 – 2025
Matu Ram Institute of Engineering and ManagementPage 1
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
INDEX
Java Programming – BCA 4th Semester
[Link]. Program Name Page
No.
1 Introduction to Java 4
2 Installation of Java 5
3 Compile and Run Java Program 6
4 First Program in Java – Hello World 7
5 Find Largest of Two Numbers 8
6 Palindrome Program 9
7 Even or Odd Number 10
8 Leap Year Program 11
9 Swap Two Numbers Without Third Variable 12
10 Swap Two Numbers Using Third Variable 13
11 Factorial of a Number 14
12 Multiplication Table 15
13 Factorial Using Recursion 16
14 Reverse of a Number 17
15 Prime Numbers from 1 to 100 18
16 Fibonacci Series 19
17 Right Triangle Star Pattern 20
18 Left Triangle Star Pattern 21
19 Pyramid Star Pattern 22
20 Triangle Star Pattern 23
21 Number Pattern 24
22 Print 2-D Array 25
23 Print 3-D Array 26
24 Constructor in Java 27
25 Single Inheritance 28
26 Hierarchical Inheritance 29
27 Method Overriding 31
28 Abstract Class and Method 32
29 Java Interface 33
30 this Keyword in Java 34
31 super Keyword in Java 35
32 final Keyword in Java 36
Matu Ram Institute of Engineering and ManagementPage 2
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
1. Introduction to Java
Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3
billion devices run Java.
It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
Java programming language is named JAVA. The name was chosen by James Gosling while having
coffee near his office. Java is the name of an island in Indonesia where the first coffee was produced.
Note that Java is just a name, not an acronym.
Matu Ram Institute of Engineering and ManagementPage 3
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
2. Installation of Java
The following steps can be followed in order to download and install Java on Windows 10:
Step 1: Open [Link] in the browser to go to the official Oracle Java
downloads page.
Step 2: Scroll to the version of Java you want to download and click on JDK Download option.
Step 3: Click on the download button suitable for your Operating System. For a 64-bit machine, choose the
file ending with x64.
Step 4: Accept the Oracle Technology Network License Agreement to start the download.
Step 5: Login to your Oracle account – the download will start automatically after login.
Step 6: Run the downloaded installer and click Next. You can change the installation directory if needed.
Step 7: The installation will begin automatically.
Step 8: It will also ask for JRE installation directory – continue with the default.
Step 9: Java installation will be completed successfully.
Step 10: Click Close on the confirmation window.
Step 11: To verify, open Command Prompt and type: java -version
Matu Ram Institute of Engineering and ManagementPage 4
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
3. How to Compile and Run a Java Program
Follow these steps to compile and run a Java program using Command Prompt:
Step 1: Write your Java program in Notepad and save it with the .java extension (e.g., [Link]).
Step 2: Open Command Prompt.
Step 3: Navigate to the folder where the .java file is saved using the cd command.
Step 4: To compile the program, type the following command:
javac [Link]
Step 5: To run the compiled program, type:
java HelloWorld
A .class file is generated during compilation. If there are any errors, they will be shown at the compile
step itself.
Matu Ram Institute of Engineering and ManagementPage 5
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 1: Hello World – First Program in Java
Objective: To print 'Hello World!' on the screen using Java.
// The main method must be in a class named 'Main'.
class Main {
public static void main(String[] args)
{
[Link]("Hello world!");
}
}
Output: Hello world!
Matu Ram Institute of Engineering and ManagementPage 6
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 2: Find Largest in Two Numbers
Objective: To compare two numbers and find out which one is greater.
public class Program1 {
public static void main(String[] args)
{
int num1 = 30, num2 = 24;
if (num1 == num2) {
[Link]("both are equal");
}
else if (num1 > num2) {
[Link](num1 + " is greater");
}
else {
[Link](num2 + " is greater");
}
}
}
Output: 30 is greater
Matu Ram Institute of Engineering and ManagementPage 7
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 3: Palindrome Program
Objective: To check whether a given number is a palindrome or not.
public class Palindrome {
public static void main(String args[]) {
int r, sum = 0, temp;
int n = 2332; // number to check for palindrome
temp = n;
while (n > 0) {
r = n % 10; // getting remainder
sum = (sum * 10) + r;
n = n / 10;
}
if (temp == sum)
[Link]("palindrome number");
else
[Link]("not palindrome");
}
}
Output: palindrome number
Matu Ram Institute of Engineering and ManagementPage 8
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 4: Even or Odd Number
Objective: To check whether a given number is Even or Odd.
public class Even_Odd {
public static void main(String[] args)
{
// Declaring and initializing integer variable
int num = 24;
// Checking if number is even or odd via remainder
if (num % 2 == 0) {
// If remainder is zero then number is even
[Link]("Entered Number is Even");
}
else {
// If remainder is not zero then number is odd
[Link]("Entered Number is Odd");
}
}
}
Output: Entered Number is Even
Matu Ram Institute of Engineering and ManagementPage 9
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 5: Leap Year Program
Objective: To check whether a given year is a Leap Year or not.
public class Leap_year {
// Method to check leap year
public static void isLeapYear(int year)
{
boolean is_leap_year = false;
// If year is divisible by 4
if (year % 4 == 0) {
is_leap_year = true;
// To check if it is a century year
if (year % 100 == 0) {
if (year % 400 == 0)
is_leap_year = true;
else
is_leap_year = false;
}
}
else {
is_leap_year = false;
}
if (!is_leap_year)
[Link](year + ": Non Leap-year");
else
[Link](year + ": Leap-year");
}
public static void main(String[] args) {
isLeapYear(2004);
isLeapYear(2002);
}
}
2004: Leap-year
Output: 2002: Non Leap-year
Matu Ram Institute of Engineering and ManagementPage 10
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 6: Swap Two Numbers Without Third Variable
Objective: To swap two numbers without using a third (temporary) variable.
import [Link].*;
class Swapping
{
public static void main(String a[])
{
[Link]("Enter the value of x and y");
Scanner sc = new Scanner([Link]);
int x = [Link]();
int y = [Link]();
[Link]("before swapping numbers: " + x + " " + y);
// Swapping without third variable
x = x + y;
y = x - y;
x = x - y;
[Link]("After swapping: " + x + " " + y);
}
}
Enter the value of x and y
24 30
Output: before swapping numbers: 24 30
After swapping: 30 24
Matu Ram Institute of Engineering and ManagementPage 11
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 7: Swap Two Numbers Using Third Variable
Objective: To swap two numbers using a temporary (third) variable.
public class Swap {
// main function
public static void main(String[] args)
{
int x = 400, y = 350;
[Link]("Before Swap");
[Link]("x = " + x);
[Link]("y = " + y);
// Swapping using three variables
int temp = x;
x = y;
y = temp;
[Link]("After swap");
[Link]("x = " + x);
[Link]("y = " + y);
}
}
Before Swap
x = 400
y = 350
Output: After swap
x = 350
y = 400
Matu Ram Institute of Engineering and ManagementPage 12
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 8: Factorial of a Number
Objective: To find the factorial of a number using a for loop.
public class Factorial {
// Method to find factorial of given number
static int factorial(int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
// main method
public static void main(String[] args)
{
int num = 7;
[Link]("Factorial of " + num + " is " + factorial(7));
}
}
Output: Factorial of 7 is 5040
Matu Ram Institute of Engineering and ManagementPage 13
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 9: Multiplication Table
Objective: To print the multiplication table of a given number using a for loop.
public class Table {
public static void main(String[] args)
{
// number for which we have to print the multiplication table
int N = 8;
// looping from 1 to 10
for (int i = 1; i <= 10; i++) {
[Link](N + " * " + i + " = " + N * i);
}
}
}
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
Output: 8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
Matu Ram Institute of Engineering and ManagementPage 14
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 10: Factorial Using Recursion
Objective: To find the factorial of a number using recursive function call.
public class Factorial_Recursion {
public static void main(String[] args) {
int num = 6;
long factorial = multiplyNumbers(num);
[Link]("Factorial of " + num + " = " + factorial);
}
public static long multiplyNumbers(int num)
{
if (num >= 1)
return num * multiplyNumbers(num - 1);
else
return 1;
}
}
Output: Factorial of 6 = 720
Matu Ram Institute of Engineering and ManagementPage 15
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 11: Reverse of a Number
Objective: To find and print the reverse of a given number.
public class Reverse {
// Function to reverse the number
static int reverse(int n)
{
int rev = 0; // reversed number
int rem; // remainder
while (n > 0) {
rem = n % 10;
rev = (rev * 10) + rem;
n = n / 10;
}
return rev;
}
// Driver Function
public static void main(String[] args)
{
int n = 8437;
[Link]("Reversed Number is " + reverse(n));
}
}
Output: Reversed Number is 7348
Matu Ram Institute of Engineering and ManagementPage 16
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 12: Prime Numbers from 1 to 100
Objective: To print all prime numbers between 1 and 100.
public class Prime_num {
public static void main(String[] args) {
int a = 1, b = 100;
for (int i = a; i <= b; i++) {
if (checkPrime(i))
[Link](i + " ");
}
}
public static boolean checkPrime(int num) {
// 0, 1 and negative numbers are not prime
if (num < 2) { return false; }
else {
int x = num / 2;
for (int i = 2; i < x; i++) {
if (num % i == 0) { return false; }
}
}
return true;
}
}
2 3 4 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Output: 83 89 97
Matu Ram Institute of Engineering and ManagementPage 17
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 13: Fibonacci Series
Objective: To print the Fibonacci series up to N terms using recursion.
public class Fibonacci {
// Function to print the fibonacci series
static int fib(int n)
{
// Base Case
if (n <= 1)
return n;
// Recursive call
return fib(n - 1) + fib(n - 2);
}
// Driver Code
public static void main(String args[])
{
// Given Number N
int N = 10;
// Print the first N numbers
for (int i = 0; i < N; i++) {
[Link](fib(i) + " ");
}
}
}
Output: 0 1 1 2 3 5 8 13 21 34
Matu Ram Institute of Engineering and ManagementPage 18
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 14: Right Triangle Star Pattern
Objective: To print a Right Triangle star pattern using nested for loops.
public class Pattern1 {
public static void StarRightTriangle(int n)
{
int a, b;
for (a = 0; a < n; a++) {
for (b = 0; b <= a; b++) {
[Link]("* ");
}
[Link]();
}
}
public static void main(String args[])
{
int k = 5;
StarRightTriangle(k);
}
}
*
* *
Output: * * *
* * * *
* * * * *
Matu Ram Institute of Engineering and ManagementPage 19
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 15: Constructor in Java
Objective: To demonstrate the use of a parameterized constructor in Java.
public class Constructor {
// data members of the class
String name;
int id;
Constructor(String name, int id)
{
[Link] = name;
[Link] = id;
}
}
class Const {
public static void main(String[] args)
{
// Invoking the parameterized constructor
Constructor name1 = new Constructor("Saloni", 24);
[Link]("Name: " + [Link] + " and Id: " + [Link]);
}
}
Output: Name: Saloni and Id: 24
Matu Ram Institute of Engineering and ManagementPage 20
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 16: Single Inheritance in Java
Objective: To demonstrate Single Inheritance where one class inherits from another class.
import [Link].*;
// Base or Super Class
class Employee {
int salary = 80000;
}
// Inherited or Sub Class
class Engineer extends Employee {
int benefits = 20000;
}
// Driver Class
class Abc {
public static void main(String args[])
{
Engineer E1 = new Engineer();
[Link]("Salary: " + [Link]);
[Link]("Benefits: " + [Link]);
}
}
Salary: 80000
Output: Benefits: 20000
Matu Ram Institute of Engineering and ManagementPage 21
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 17: Method Overriding in Java
Objective: To demonstrate Method Overriding and Runtime Polymorphism in Java.
// Base Class
class Parent {
void show() { [Link]("Parent's show()"); }
}
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
@Override
void show()
{
[Link]("Child's show()");
}
}
class Xyz {
public static void main(String[] args)
{
// Parent reference to Parent object
Parent obj1 = new Parent();
[Link]();
// Parent reference to Child object – RUN TIME POLYMORPHISM
Parent obj2 = new Child();
[Link]();
}
}
Parent's show()
Output: Child's show()
Matu Ram Institute of Engineering and ManagementPage 22
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 18: Abstract Class and Method
Objective: To demonstrate the use of abstract class and abstract method in Java.
abstract class Animal {
abstract void makeSound();
public void eat() {
[Link]("I can eat.");
}
}
class Dog extends Animal {
// provide implementation of abstract method
public void makeSound() {
[Link]("Bark bark");
}
}
class Main {
public static void main(String[] args) {
// create an object of Dog class
Dog d1 = new Dog();
[Link]();
[Link]();
}
}
Bark bark
Output: I can eat.
Matu Ram Institute of Engineering and ManagementPage 23
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 19: Java Interface
Objective: To demonstrate the use of Interface in Java.
public interface Polygon {
void getArea(int length, int breadth);
}
// implement the Polygon interface
class Rectangle implements Polygon {
// implementation of abstract method
public void getArea(int length, int breadth) {
[Link]("The area of the rectangle is " + (length *
breadth));
}
}
class Main {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
[Link](5, 6);
}
}
Output: The area of the rectangle is 30
Matu Ram Institute of Engineering and ManagementPage 24
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 20: this and super Keyword in Java
Objective: To demonstrate the use of 'this' and 'super' keyword in Java.
this keyword:
public class Main {
int age;
Main(int age) {
[Link] = age; // 'this' refers to current object
}
public static void main(String[] args) {
Main obj = new Main(8);
[Link]("[Link] = " + [Link]);
}
}
Output: [Link] = 8
super keyword:
class Vehicle {
int maxSpeed = 120;
}
class Car extends Vehicle {
int maxSpeed = 180;
void display() {
// print maxSpeed of base class (Vehicle)
[Link]("Maximum Speed: " + [Link]);
}
}
class Test {
public static void main(String[] args) {
Car small = new Car();
[Link]();
}
}
Output: Maximum Speed: 120
Matu Ram Institute of Engineering and ManagementPage 25
Practical File – Java Programming | BCA 4th SemesterSubmitted by: Himanshu
Program 21: final Keyword in Java
Objective: To demonstrate the use of 'final' keyword in Java.
The final keyword is used to declare a constant variable whose value cannot be changed once
assigned.
// Java program to demonstrate local final variable
public class ABC {
// Main driver method
public static void main(String args[])
{
// Declaring local final variable
final int i;
// Now initializing it with integer value
i = 20;
// Printing the value on console
[Link](i);
}
}
Output: 20
*** End of Practical File ***
Himanshu | BCA 4th Semester | Java Programming | MRIEM
Matu Ram Institute of Engineering and ManagementPage 26