0% found this document useful (0 votes)
30 views

Abd Java

java programs

Uploaded by

Kriti Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Abd Java

java programs

Uploaded by

Kriti Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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 java.util.*;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number : ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number : ");
double num2 = scanner.nextDouble();
System.out.print("Choose an operation (+, -, *, /, %) : ");
char operator = scanner.next().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) {
System.out.println("Error! Division by zero is not allowed.");
return;
}
result = num1 / num2;
break;
case '%':
result = num1 % num2;
break;
default:
System.out.println("Error! Invalid operator.");
return;
}
System.out.println("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 java.util.Scanner;

public class FloydTriangle {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows for Floyd's Triangle:");
int rows = scanner.nextInt();
int number = 1;
System.out.println("Floyd's Triangle:");
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
number++;
}
System.out.println();
}
}
}

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 java.util.*;
public class MarksRange{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,s;
System.out.print("Enter the number of students: ");
s=sc.nextInt();
int[] marks=new int[s];
for(i=0;i<s;i++)
{
System.out.print("Enter marks for student " + (i + 1) + ": ");
marks[i]=sc.nextInt();
}
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

}
}
System.out.println("Number of students in the range 81-100: " + range[0]);
System.out.println("Number of students in the range 61-80: " + range[1]);
System.out.println("Number of students in the range 41-60: " + range[2]);
System.out.println("Number of students in the range 0-40: " + range[3]);
sc.close();
}
}

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 java.util.*;

public class stringFunctions {


public static void main(String args[])
{
String sample=" Hello, World! ";
System.out.println("Length of the string: " + sample.length());
System.out.println("Trimmed string: \"" + sample.trim() + "\"");
System.out.println("Uppercase string: " + sample.toUpperCase());
System.out.println("Lowercase string: " + sample.toLowerCase());
System.out.println("Substring from index 7 to 12: \"" + sample.substring(7,12) + "\"");
System.out.println("String after replacing 'l' with 'z': \"" + sample.replace('l','z') + "\"");
System.out.println("String after replacing all occurrences of 'World' with 'Universe': \"" +
sample.replaceAll("World","Universe") + "\"");
System.out.println("Concatenated string: \"" + sample.concat("Welcome!") + "\"");
System.out.println("Index of ',' in the string: " + sample.indexOf(','));
System.out.println("String starts with 'Hello': " + sample.startsWith("Hello"));
System.out.println("String ends with space: " + sample.endsWith(" "));
}
}

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 java.util.Scanner;

public class sumMatrix {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input for the dimensions of the matrices


System.out.println("Enter the number of rows and columns of the matrices:");
int rows = scanner.nextInt();
int cols = scanner.nextInt();

// Creating matrices
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];

// Input for the elements of the first matrix


System.out.println("Enter the elements of the first matrix:");
inputMatrixElements(scanner, matrix1);

// Input for the elements of the second matrix


System.out.println("Enter the elements of the second matrix:");
inputMatrixElements(scanner, matrix2);

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

// Displaying the result


System.out.println("The sum of the matrices is:");
displayMatrix(sumMatrix);

scanner.close();
}

// Method to input elements of a matrix


private static void inputMatrixElements(Scanner scanner, int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = scanner.nextInt();
}
}
}

// Method to add two matrices


private static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {
int rows = matrix1.length;

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) {
System.out.print(num + " ");
}
System.out.println();
}
}
}

OUTPUT:

6. Write a Java program to compute the sum of diagonal elements in a m*n matrix.
Source Code:

import java.util.Scanner;

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(System.in);

// Input for the dimensions of the matrix


System.out.println("Enter the number of rows and columns of the matrix:");
int rows = scanner.nextInt();
int cols = scanner.nextInt();

// Creating the matrix


int[][] matrix = new int[rows][cols];

// Input for the elements of the matrix


System.out.println("Enter the elements of the matrix:");
inputMatrixElements(scanner, matrix);

// Computing the sum of diagonal elements


int sum = diagonalSum(matrix);

// Displaying the sum


System.out.println("The sum of diagonal elements in the matrix is: " + sum);

scanner.close();
}

// Method to input elements of a matrix


private static void inputMatrixElements(Scanner scanner, int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = scanner.nextInt();
}
}
}

// Method to compute the sum of diagonal elements


private static int diagonalSum(int[][] matrix) {
int sum = 0;
int rows = matrix.length;
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 java.util.Scanner;
public class BankAccount
{
private String name,accnt,accntNo;
private double bal;
public BankAccount(String name,String accnt,String accntNo,double bal)
{
this.name=name;
this.accnt=accnt;
this.accntNo=accntNo;
this.bal=bal;
}
public void deposit(double amt)
{
bal+=amt;
System.out.println(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

System.out.println(amt + " withdrawn successfully.");


}
else
{
System.out.println("Insufficient balance. Withdrawal failed.");
}
}
public void display()
{
System.out.println("Depositor Name: " + name);
System.out.println("Account Balance: " + bal);
}
public static void main(String[] args)
{
String name,type,accntNo;
double bal;
int dep,withdrawl;
Scanner sc=new Scanner(System.in);
System.out.print("Enter depositor name: ");
name=sc.nextLine();
System.out.print("Enter account type: ");
type=sc.nextLine();
System.out.print("Enter account number: ");
accntNo=sc.nextLine();
System.out.print("Enter initial balance: ");
bal=sc.nextDouble();
BankAccount ob=new BankAccount(name,type,accntNo,bal);
System.out.println("Enter amount to be deposited :");
dep=sc.nextInt();
System.out.println("Enter amount to withdraw :");
withdrawl=sc.nextInt();
ob.deposit(dep);
ob.withdraw(withdrawl);

Priyanshu Pandey /BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)


Java Programming Lab Manual

ob.display();
sc.close();
}
}

OUTPUT:

Priyanshu Pandey/BTECH CSE/UNIVERSITY ROLL NO – 2219338 /SEM-IV/SECTION- B2 (51)

You might also like