[Go to site: main page, start]

0% found this document useful (0 votes)
6 views19 pages

Java Programming Lab Manual

The document contains a series of Java programming exercises including implementations for calculating square roots, checking even or odd numbers, swapping integers, calculating factorials, and more. Each exercise includes source code, console outputs, and explanations of different approaches. The exercises cover various programming concepts such as recursion, iteration, pattern display, and method overloading.

Uploaded by

vishwasgupta568
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)
6 views19 pages

Java Programming Lab Manual

The document contains a series of Java programming exercises including implementations for calculating square roots, checking even or odd numbers, swapping integers, calculating factorials, and more. Each exercise includes source code, console outputs, and explanations of different approaches. The exercises cover various programming concepts such as recursion, iteration, pattern display, and method overloading.

Uploaded by

vishwasgupta568
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

JAVA PROGRAMMING LABORATORY FILE

Verified Source Code & Execution Outputs

1. Command Line Integer Square Root Calculator

public class SquareRootCalculator {


public static void main(String[] args) {
if ([Link] != 1) {
[Link]("Usage: java SquareRootCalculator <integer>");
return;
}

try {
int number = [Link](args[0]);
if (number < 0) {
[Link]("Error: Cannot calculate the square root of a
negative number.");
} else {
double squareRoot = [Link](number);
[Link]("The square root of " + number + " is: " +
squareRoot);
}
} catch (NumberFormatException e) {
[Link]("Invalid input. Please enter a valid integer.");
}
}
}

CONSOLE OUTPUT
The square root of 25 is: 5.0

Java Programming Laboratory Manual Page 1


2. Even or Odd Number Checker (Scanner Class)

import [Link];

public class EvenOddChecker {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter an integer: ");
int number = [Link]();

if (number % 2 == 0) {
[Link](number + " is even.");
} else {
[Link](number + " is odd.");
}
[Link]();
}
}

CONSOLE OUTPUT
Enter an integer: 4
4 is even.

Enter an integer: 7
7 is odd.

Java Programming Laboratory Manual Page 2


3. Swap Two Integer Values (With & Without a Third Variable)

Approach I: Using a Third Variable

import [Link];

public class SwapWithThirdVariable {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first integer: ");
int a = [Link]();
[Link]("Enter the second integer: ");
int b = [Link]();

[Link]("Before swapping: a = " + a + ", b = " + b);

// Swapping using a third variable


int temp = a;
a = b;
b = temp;

[Link]("After swapping: a = " + a + ", b = " + b);


[Link]();
}
}

Approach II: Without Using a Third Variable

import [Link];

public class SwapWithoutThirdVariable {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first integer: ");
int a = [Link]();
[Link]("Enter the second integer: ");
int b = [Link]();

[Link]("Before swapping: a = " + a + ", b = " + b);

// Swapping without using a third variable


a = a + b;
b = a - b;
a = a - b;

[Link]("After swapping: a = " + a + ", b = " + b);

Java Programming Laboratory Manual Page 3


[Link]();
}
}

Java Programming Laboratory Manual Page 4


4. Factorial of a Number (Recursion & Iteration)

Approach I: Using Recursion

import [Link];

public class FactorialRecursion {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a non-negative integer: ");
int number = [Link]();

if (number < 0) {
[Link]("Factorial is not defined for negative numbers.");
} else {
long result = factorial(number);
[Link]("The factorial of " + number + " is: " + result);
}
[Link]();
}

public static long factorial(int n) {


if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
}

Approach II: Using Iteration

import [Link];

public class FactorialIteration {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a non-negative integer: ");
int number = [Link]();

if (number < 0) {
[Link]("Factorial is not defined for negative numbers.");
} else {
long result = factorial(number);
[Link]("The factorial of " + number + " is: " + result);
}

Java Programming Laboratory Manual Page 5


[Link]();
}

public static long factorial(int n) {


long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}

CONSOLE OUTPUT
Enter a non-negative integer: 7
The factorial of 7 is: 5040

Java Programming Laboratory Manual Page 6


5. Reverse a Number and Find the Sum of its Digits

import [Link];

public class NumberReverseAndSum {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();

int reversedNumber = reverseNumber(number);


int sumOfDigits = sumOfDigits(number);

[Link]("Reversed number: " + reversedNumber);


[Link]("Sum of digits: " + sumOfDigits);
[Link]();
}

public static int reverseNumber(int n) {


int reversedNumber = 0;
while (n != 0) {
int digit = n % 10;
reversedNumber = reversedNumber * 10 + digit;
n /= 10;
}
return reversedNumber;
}

public static int sumOfDigits(int n) {


int sum = 0;
while (n != 0) {
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
}

CONSOLE OUTPUT
Enter a number: 12345
Reversed number: 54321
Sum of digits: 15

Java Programming Laboratory Manual Page 7


6. Sum of Series (1 + x + x² + x³ + ... + xⁿ)

import [Link];

public class SeriesSum {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the value of x: ");
double x = [Link]();
[Link]("Enter the value of n: ");
int n = [Link]();

double sum = calculateSeriesSum(x, n);


[Link]("Sum of the series is: " + sum);
[Link]();
}

public static double calculateSeriesSum(double x, int n) {


double sum = 0;
for (int i = 0; i <= n; i++) {
sum += [Link](x, i);
}
return sum;
}
}

CONSOLE OUTPUT
Enter the value of x: 2
Enter the value of n: 3
Sum of the series is: 15.0

Java Programming Laboratory Manual Page 8


7. Number Right-Triangle Pattern Display

public class NumberPattern {


public static void main(String[] args) {
int n = 4; // Change this value to adjust the number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link](j + " ");
}
[Link]();
}
}
}

VISUAL OUTPUT
1
1 2
1 2 3
1 2 3 4

Java Programming Laboratory Manual Page 9


8. Character / Star Right-Triangle Pattern

import [Link].*;

public class GeeksForGeeks {


public static void printPattern(int n) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}

public static void main(String args[]) {


int n = 6;
printPattern(n);
}
}

VISUAL OUTPUT
*
**
***
****
*****
******

Java Programming Laboratory Manual Page 10


9. Centered Inverted Pyramid Pattern

public class PatternDisplay {


public static void main(String[] args) {
int rows = 5; // Change this value to adjust the number of rows
for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = 1; j <= rows - i; j++) {
[Link](" ");
}
// Print stars
for (int k = 1; k <= 2 * i - 1; k++) {
[Link]("*");
}
[Link]();
}
}
}

VISUAL OUTPUT
*********
*******
*****
***
*

Java Programming Laboratory Manual Page 11


10. Simple Arithmetic Calculator (Switch-Case)

import [Link];

public class SimpleCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Welcome to Simple Calculator!");
[Link]("Available operations:");
[Link]("1. Addition (+)");
[Link]("2. Subtraction (-)");
[Link]("3. Multiplication (*)");
[Link]("4. Division (/)");
[Link]("Enter the operation number: ");
int choice = [Link]();

[Link]("Enter the first number: ");


double num1 = [Link]();
[Link]("Enter the second number: ");
double num2 = [Link]();

double result = 0;
switch (choice) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
} else {
[Link]("Error: Division by zero is not allowed.");
return;
}
break;
default:
[Link]("Error: Invalid operation number.");
return;
}
[Link]("Result: " + result);
[Link]();

Java Programming Laboratory Manual Page 12


}
}

CONSOLE OUTPUT
Welcome to Simple Calculator!
Available operations:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
Enter the operation number: 3
Enter the first number: 5
Enter the second number: 3
Result: 15.0

Java Programming Laboratory Manual Page 13


11. Linear Search in a One-Dimensional Array

import [Link];

public class ArraySearch {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
[Link]("Enter the element to search: ");
int target = [Link]();

boolean found = false;


int index = -1;

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


if (array[i] == target) {
found = true;
index = i;
break;
}
}

if (found) {
[Link]("Element " + target + " found at index " + index);
} else {
[Link]("Element " + target + " not found in the array");
}
[Link]();
}
}

CONSOLE OUTPUT
Enter the element to search: 5
Element 5 found at index 4

Java Programming Laboratory Manual Page 14


12. Matrix Addition (3x3 Ordered Matrices)

import [Link];

public class MatrixAddition {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];

[Link]("Enter elements of first matrix:");


inputMatrix(scanner, matrix1);

[Link]("Enter elements of second matrix:");


inputMatrix(scanner, matrix2);

int[][] sumMatrix = addMatrices(matrix1, matrix2);


[Link]("Sum of the matrices:");
displayMatrix(sumMatrix);
[Link]();
}

public static void inputMatrix(Scanner scanner, int[][] matrix) {


for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = [Link]();
}
}
}

public static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {


int[][] sumMatrix = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return sumMatrix;
}

public static void displayMatrix(int[][] matrix) {


for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();

Java Programming Laboratory Manual Page 15


}
}
}

CONSOLE OUTPUT
Enter elements of first matrix:
1 2 3
4 5 6
7 8 9
Enter elements of second matrix:
9 8 7
6 5 4
3 2 1
Sum of the matrices:
10 10 10
10 10 10
10 10 10

Java Programming Laboratory Manual Page 16


13. Student Class Initialization (Reference, Method, and Constructor)

public class Student {


String name;
int rollNo;
double marks;

// Default constructor added for reference/method initialization allocation


public Student() {}

// Initialization using a method


void initializeUsingMethod(String name, int rollNo, double marks) {
[Link] = name;
[Link] = rollNo;
[Link] = marks;
}

// Initialization using a parameterised constructor


Student(String name, int rollNo, double marks) {
[Link] = name;
[Link] = rollNo;
[Link] = marks;
}

void show() {
[Link]("Name: " + name);
[Link]("Roll Number: " + rollNo);
[Link]("Marks: " + marks);
}

public static void main(String[] args) {


// 1. Initializing directly via object fields (Reference approach mapped to
initialization)
Student student1 = new Student();
[Link] = "John";
[Link] = 101;
[Link] = 85.5;
[Link]("Using reference:");
[Link]();
[Link]();

// 2. Initializing via constructor


Student student2 = new Student("Alice", 102, 90.0);
[Link]("Using constructor:");
[Link]();
[Link]();

Java Programming Laboratory Manual Page 17


// 3. Initializing via custom method
Student student3 = new Student();
[Link]("Bob", 103, 78.0);
[Link]("Using method:");
[Link]();
}
}

CONSOLE OUTPUT
Using reference:
Name: John
Roll Number: 101
Marks: 85.5

Using constructor:
Name: Alice
Roll Number: 102
Marks: 90.0

Using method:
Name: Bob
Roll Number: 103
Marks: 78.0

Java Programming Laboratory Manual Page 18


14. Compile-Time Polymorphism (Method Overloading)

public class MethodOverloadingExample {


static int add(int a) {
return a + 10;
}

static int add(int a, int b) {


return a + b;
}

static int add(int a, int b, int c) {


return a + b + c;
}

static double add(double a, double b) {


return a + b;
}

public static void main(String[] args) {


[Link]("Method with one parameter: " + add(5));
[Link]("Method with two parameters: " + add(5, 10));
[Link]("Method with three parameters: " + add(5, 10, 15));
[Link]("Method with double parameters: " + add(5.5, 10.5));
}
}

CONSOLE OUTPUT
Method with one parameter: 15
Method with two parameters: 15
Method with three parameters: 30
Method with double parameters: 16.0

Java Programming Laboratory Manual Page 19

You might also like