Java Programming Lab Manual
PRACTICAL 1
1. Write a JAVA Program to make a calculator using switch case. You
can have 5 operations +,-,/,*,%.
Source Code:
import [Link].*;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first number : ");
double num1 = [Link]();
[Link]("Enter the second number : ");
double num2 = [Link]();
[Link]("Choose an operation (+, -, *, /, %) : ");
char operator = [Link]().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
[Link]("Error! Division by zero is not allowed.");
return;
}
result = num1 / num2;
break;
case '%':
result = num1 % num2;
break;
default:
[Link]("Error! Invalid operator.");
return;
}
[Link]("Result: " + result);
}
}
OUTPUT:
Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
2. Write a JAVA Program to print Floyd's Triangle.
Priyanshu Pandey /BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
Source Code:
import [Link];
public class FloydTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of rows for Floyd's Triangle:");
int rows = [Link]();
int number = 1;
[Link]("Floyd's Triangle:");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
[Link](number + " ");
number++;
}
[Link]();
}
}
}
OUTPUT:
Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
PRACTICAL 2
3. Given a list of marks ranging from 0 to 100. Write a JAVA Program to compute and
print the number of students who have obtained marks
a. in the range of 81-100
b. in the range of 61-80
c. in the range of 41-60
d. in the range of 0-40.
The program should use a minimum number of if statements.
Source Code:
import [Link].*;
public class MarksRange{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int i,s;
[Link]("Enter the number of students: ");
s=[Link]();
int[] marks=new int[s];
for(i=0;i<s;i++)
{
[Link]("Enter marks for student " + (i + 1) + ": ");
marks[i]=[Link]();
}
int[] range=new int[4];
for (int mark : marks)
{
if(mark>=81 && mark<=100)
{
range[0]++;
}
else if(mark>=61 && mark<=80)
{
range[1]++;
}
else if(mark>=41 && mark<=60)
{
range[2]++;
}
else if(mark>=0 && mark<=40)
{
range[3]++;
Priyanshu Pandey /BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
}
}
[Link]("Number of students in the range 81-100: " + range[0]);
[Link]("Number of students in the range 61-80: " + range[1]);
[Link]("Number of students in the range 41-60: " + range[2]);
[Link]("Number of students in the range 0-40: " + range[3]);
[Link]();
}
}
OUTPUT:
4. Write a JAVA Program to implement various String class functions such as length(),
concat(), replace(), replaceAll(), trim() etc.
Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
Source Code:
import [Link].*;
public class stringFunctions {
public static void main(String args[])
{
String sample=" Hello, World! ";
[Link]("Length of the string: " + [Link]());
[Link]("Trimmed string: \"" + [Link]() + "\"");
[Link]("Uppercase string: " + [Link]());
[Link]("Lowercase string: " + [Link]());
[Link]("Substring from index 7 to 12: \"" + [Link](7,12) + "\"");
[Link]("String after replacing 'l' with 'z': \"" + [Link]('l','z') + "\"");
[Link]("String after replacing all occurrences of 'World' with 'Universe': \"" +
[Link]("World","Universe") + "\"");
[Link]("Concatenated string: \"" + [Link]("Welcome!") + "\"");
[Link]("Index of ',' in the string: " + [Link](','));
[Link]("String starts with 'Hello': " + [Link]("Hello"));
[Link]("String ends with space: " + [Link](" "));
}
}
OUTPUT:
PRACTICAL 3
5. Write a Java program to add two matrices.
Priyanshu Pandey /BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
Source Code:
import [Link];
public class sumMatrix {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input for the dimensions of the matrices
[Link]("Enter the number of rows and columns of the matrices:");
int rows = [Link]();
int cols = [Link]();
// Creating matrices
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];
// Input for the elements of the first matrix
[Link]("Enter the elements of the first matrix:");
inputMatrixElements(scanner, matrix1);
// Input for the elements of the second matrix
[Link]("Enter the elements of the second matrix:");
inputMatrixElements(scanner, matrix2);
// Adding matrices
int[][] sumMatrix = addMatrices(matrix1, matrix2);
// Displaying the result
[Link]("The sum of the matrices is:");
displayMatrix(sumMatrix);
[Link]();
}
// Method to input elements of a matrix
private static void inputMatrixElements(Scanner scanner, int[][] matrix) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = [Link]();
}
}
}
// Method to add two matrices
private static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {
int rows = [Link];
Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
int cols = matrix1[0].length;
int[][] sumMatrix = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return sumMatrix;
}
// Method to display a matrix
private static void displayMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int num : row) {
[Link](num + " ");
}
[Link]();
}
}
}
OUTPUT:
6. Write a Java program to compute the sum of diagonal elements in a m*n matrix.
Source Code:
import [Link];
public class DiagonalSum {
public static void main(String[] args) {
Priyanshu Pandey /BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
Scanner scanner = new Scanner([Link]);
// Input for the dimensions of the matrix
[Link]("Enter the number of rows and columns of the matrix:");
int rows = [Link]();
int cols = [Link]();
// Creating the matrix
int[][] matrix = new int[rows][cols];
// Input for the elements of the matrix
[Link]("Enter the elements of the matrix:");
inputMatrixElements(scanner, matrix);
// Computing the sum of diagonal elements
int sum = diagonalSum(matrix);
// Displaying the sum
[Link]("The sum of diagonal elements in the matrix is: " + sum);
[Link]();
}
// Method to input elements of a matrix
private static void inputMatrixElements(Scanner scanner, int[][] matrix) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = [Link]();
}
}
}
// Method to compute the sum of diagonal elements
private static int diagonalSum(int[][] matrix) {
int sum = 0;
int rows = [Link];
int cols = matrix[0].length;
// Assuming it's a square matrix (rows == cols)
for (int i = 0; i < rows; i++) {
sum += matrix[i][i];
}
return sum;
}
}
Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
OUTPUT:
7. Design a class to represent a bank account. Include the following members-
Data Members-
*Name of the Depositor
*Type of account
Priyanshu Pandey /BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
*Account Number
*Balance amount in the account
Methods-
*To assign initial values.
*To deposit an amount
*To withdraw an amount after checking balance.
*To display the name and balance.
Source Code:
import [Link];
public class BankAccount
{
private String name,accnt,accntNo;
private double bal;
public BankAccount(String name,String accnt,String accntNo,double bal)
{
[Link]=name;
[Link]=accnt;
[Link]=accntNo;
[Link]=bal;
}
public void deposit(double amt)
{
bal+=amt;
[Link](amt + " deposited successfully.");
}
public void withdraw(double amt)
{
if(bal>=amt)
{
bal-=amt;
Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
[Link](amt + " withdrawn successfully.");
}
else
{
[Link]("Insufficient balance. Withdrawal failed.");
}
}
public void display()
{
[Link]("Depositor Name: " + name);
[Link]("Account Balance: " + bal);
}
public static void main(String[] args)
{
String name,type,accntNo;
double bal;
int dep,withdrawl;
Scanner sc=new Scanner([Link]);
[Link]("Enter depositor name: ");
name=[Link]();
[Link]("Enter account type: ");
type=[Link]();
[Link]("Enter account number: ");
accntNo=[Link]();
[Link]("Enter initial balance: ");
bal=[Link]();
BankAccount ob=new BankAccount(name,type,accntNo,bal);
[Link]("Enter amount to be deposited :");
dep=[Link]();
[Link]("Enter amount to withdraw :");
withdrawl=[Link]();
[Link](dep);
[Link](withdrawl);
Priyanshu Pandey /BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)
Java Programming Lab Manual
[Link]();
[Link]();
}
}
OUTPUT:
Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)