Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

LABORATORY FILE - Java Programming (5th Sem) !

The document outlines a laboratory file for a Java programming course, detailing various experiments and practical assignments for students. Each experiment includes specific tasks such as performing arithmetic operations, calculating areas, converting units, and working with arrays and strings. The document also contains sample code and expected outputs for some of the practical exercises.

Uploaded by

sahil gupta.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

LABORATORY FILE - Java Programming (5th Sem) !

The document outlines a laboratory file for a Java programming course, detailing various experiments and practical assignments for students. Each experiment includes specific tasks such as performing arithmetic operations, calculating areas, converting units, and working with arrays and strings. The document also contains sample code and expected outputs for some of the practical exercises.

Uploaded by

sahil gupta.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

LABORATORY FILE : Programming in Java

SUBJECT CODE: UGCA: 1938

BACHELOR OF COMPUTER APPLICATIONS

SUBMITTED BY: TEACHER’S /MAM’:


Sahil Kumar Er. Hardeep Kaur

COLLEGE ROLL NO: 226617

UNIVERSITY ROLL NO: 2200315

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

BABA BANDA SINGH BAHADUR ENGINEERING

COLLEGE FATEGARH SAHIB


1

INDEX
Experiment
No. Title Date : Teacher’s Sign \
Remarks

01 Write a program to perform following operations


on two numbers input by the user:
1) Addition 2) subtraction 3) multiplication 4)
division

02 Write a Java program to print the result of the


following operations.
1. -15 +58 * 45
2. (35+8) % 6
3. 24 + -5*3 / 7
4. 15 + 18 / 3 * 2 - 9 % 3

03 Write a Java program to compute area of:


1) Circle2) rectangle 3) triangle 4) square

04 Write a program to convert temperature from


Fahrenheit to Celsius degree using Java.

05 Write a program through Java that reads a


number in inches, converts it to metres .

06 Write a program to convert minutes into a


number of years and days.

07 Write a Java program that prints current time in


GMT.

08 Design a program in Java to solve quadratic


equations using if, if else .

09 Write a Java program to determine the greatest


number of three numbers .

10 Write a program that gets a number from the user


and generates an integer between 1 and 7
subsequently should display the name of the
weekday as per that number.

11 Construct a Java program to find the number of


days in a month .

12 Write a program to sum values of a Single


Dimensional array.

13 Design & execute a program in Java to sort a


numeric array and a string array.

14 Calculate the average value of array elements


through Java Program .
2

15 Write a Java program to test if an array contains a


specific value.

16 Find the index of an array element by writing a


program in Java.

17 Write a Java program to remove a specific


element from an array .

18 Design a program to copy an array by iterating


the array.

19 Write a Java program to insert an element (on a


specific position) into
Multidimensional array.

20 Write a program to perform following operations


on strings:
1) Compare two strings.
2) Count string length.
3) Convert upper case to lower case & vice versa.
4) Concatenate two strings.
5) Print a substring.

21 Developed Program & design a method to find


the smallest number among three numbers.

22 Compute the average of three numbers through a


Java Program.

23 Write a Program & design a method to count all


vowels in a string.

24 Write a Java method to count all words in a


string.

25 Write a method in a Java program to count all


words in a string.
3

Practical No. ➖ 01
Write a program to perform following operations on two numbers input by the
user ➖
1) Addition 2) subtraction 3) multiplication 4) division

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_1 {


public static void main(String[] args)
{
float a,b,sum;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the values of a :");
a = obj.nextFloat();
System.out.println("Enter the values of b :");
b = obj.nextFloat();
sum = a+b;
System.out.println("Sum of a and b = "+ sum);
System.out.println("Subtraction = "+ (a-b));
System.out.println("Multiplications = "+ (a*b));
System.out.println("Division = "+ (a/b));

}
}
4

OUTPUT ➖
Enter the values of a : 19
Enter the values of b : 4

Sum of a and b = 23.0


Subtraction = 15.0
Multiplications = 76.0
Division = 4.75

Process finished with exit code 0


5

Practical No. ➖ 02
Write a Java program to print the result of the following operations.

1. -15 +58 * 45

2. (35+8) % 6

3. 24 + -5*3 / 7

4. 15 + 18 / 3 * 2 - 9 % 3

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_2 {


public static void main(String[] args)
{
float result1 = (-15 +58 *45);
System.out.println("First Result = "+ result1);

float result2 = (35+8) % 6 ;


System.out.println("Second Result = "+ result2);

float result3 = (24 + ((-5*3) / 7)) ;


System.out.println("Third Result = "+ result3);

float result4 = (15 + 18 / 3 * 2 - 9 % 3);


System.out.println("Fourth Answers = "+ result4);

}
}
6

OUTPUT ➖
First Result = 2595.0
Second Result = 1.0
Third Result = 22.0
Fourth Answers = 27.0

Process finished with exit code 0


7

Practical No. ➖ 03
3. Write a Java program to compute area of: ➖
1) Circle 2) rectangle 3) triangle 4) square

package com.SyllabusQuestions5thSem;
import com.sun.security.jgss.GSSUtil;
import java.util.Scanner;

public class Experiment_3 {


public static void main(String[] args)
{
// 1. Area of Circle : ➖
double r , CircleArea;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the radius :");
r= obj.nextDouble();
CircleArea = Math.PI*(r*r);
System.out.println("Area of Circle = "+ CircleArea);

// rectangle ➖
double l,w,RectangleArea;
System.out.println("Enter the length of rectangle = ");
l = obj.nextDouble();
System.out.println("Enter the width of rectangle = ");
w = obj.nextDouble();
RectangleArea = l * w;
System.out.println("Area of Rectangle = "+ RectangleArea);

8
// triangle

double b,h,triangleArea;
System.out.println("Enter the base of triangle = ");
b=obj.nextDouble();
System.out.println("Enter the height of triangle = ");
h=obj.nextDouble();
triangleArea = (0.5)*b*h;
System.out.println("Area of Triangle = "+ triangleArea);

// square ➖
double side ,squareArea;
System.out.println("Enter the side of square =");
side = obj.nextDouble();
squareArea = side * side;
System.out.println("Area of Square = "+ squareArea);

}
}
9

OUTPUT ➖
Enter the radius : 5
Area of Circle = 78.53981633974483

Enter the length of rectangle = 5


Enter the width of rectangle = 9
Area of Rectangle = 45.0

Enter the base of triangle = 4


Enter the height of triangle = 6
Area of Triangle = 12.0

Enter the side of square = 5


Area of Square = 25.0

Process finished with exit code 0


10

Practical No. ➖ 04
Write a program to convert temperature from Fahrenheit to Celsius degree using
Java .

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_4 {


public static void main(String[] args){
double fahrenheit, celsius;
Scanner obj = new Scanner(System.in);
System.out.print("Enter the temperature in fahrenheit :");
fahrenheit = obj.nextDouble();

celsius = (fahrenheit - 32) * 5/9;


System.out.println("After converting into fahrenheit to celsius , the temperature in celsius =
"+ celsius + " C");

}
}
11

OUTPUT ➖
Enter the temperature in fahrenheit :788
After converting into fahrenheit to celsius , the temperature in celsius = 420.0 C

Process finished with exit code 0


12

Practical No. ➖ 05
Write a program through Java that reads a number in inches, converts it to
metres.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_5 {


public static void main(String[] args)
{
double inches, metre;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the number in inches :");
inches = obj.nextDouble();

metre = inches * 0.0254;


System.out.println("After converting, the number in metre = "+ metre);
}
}
13

OUTPUT ➖
Enter the number in inches : 98939
After converting, the number in metre = 2513.0506

Process finished with exit code 0


14

Practical No. ➖ 06
Write a program to convert minutes into a number of years and days.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_6 {


public static void main(String[] args)
{
long minutes;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the total number of minutes :");
minutes=obj.nextLong();

final int minutes_in_hour = 60;


final int hour_in_a_day = 24;
final int day_in_a_year = 365;

long FindTotalDay, FindTotalYear, FindRemainDay;


FindTotalDay = minutes / (minutes_in_hour * hour_in_a_day);
FindTotalYear = FindTotalDay / day_in_a_year;
FindRemainDay = FindTotalDay % day_in_a_year;

System.out.println("After converting, the total number of years and days are : = "+
FindTotalYear + " Years and " + FindRemainDay + " Days.");

}
}
15

OUTPUT ➖
Enter the total number of minutes : 898945894
After converting, the total number of years and days are : = 1710 Years and 117 Days.

Process finished with exit code 0


16

Practical No. ➖ 07
Write a Java program that prints current time in GMT.

package com.SyllabusQuestions5thSem;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;

public class Experiment_7 {


public static void main(String[] args)
{

ZonedDateTime gmtTime = ZonedDateTime.now(ZoneId.of("GMT"));


DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
System.out.println("Current time in GMT : = "+ gmtTime.format(formatter));

}
}
17

OUTPUT ➖
Current time in GMT : = 2024-08-24 07:08:10 GMT

Process finished with exit code 0


18

Practical No. ➖ 08
Design a program in Java to solve quadratic equations using if, if else .

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_8 {


public static void main(String[] args)
{
double b,a,c,d,solution1 , solution2;
// d= (b**2)-(4*a*c);
// x = + - (sqrt d) / 2*a;

Scanner obj = new Scanner(System.in);


System.out.print("Enter the coefficient or values of a :");
a=obj.nextDouble();

System.out.print("Enter the coefficient or values of b :");


b = obj.nextDouble();

System.out.print("Enter the coefficient or values of c :");


c = obj.nextDouble();

d= (Math.sqrt(b*b)) - 4*a*c;
System.out.println("Discriminant values = "+ d);

if(a==0 && b==0 && c==0){


System.out.println("These values can not maintain operations !! ");
}
else if(a>=1 && b>=1 && c>=1)
{
solution1= (-b +d)/(2*a) ;
solution2= (-b -d)/(2*a);
19
System.out.println("First Solutions = "+ solution1);
System.out.println("Second Solutions = "+ solution2);
}

else
{
System.out.println("Input Error...");
}
}
}
20

OUTPUT ➖
Enter the coefficient or values of a :78
Enter the coefficient or values of b :34
Enter the coefficient or values of c :21

Discriminant values = -6518.0

First Solutions = -42.0


Second Solutions = 41.56410256410256

Process finished with exit code 0


21

Practical No. ➖ 09
Write a Java program to determine the greatest number of three numbers.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_9 {


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

int a,b,c;
System.out.print("Enter the first values :");
a=obj.nextInt();

System.out.print("Enter the second values :");


b = obj.nextInt();

System.out.print("Enter the third values : ");


c=obj.nextInt();

if(a>b && a>c)


{
System.out.println("Largest values = "+ a);
}
else if (b>a && b>c)
{
System.out.println("Largest values = "+ b);
}
else if(c>a && c>b)
{
System.out.println("Largest values = "+ c);
}
22
else if(a==b && b==c && a==c)
{
System.out.println("All these 3 values are equals !! ");
}
else {
System.out.println("Invalid input..");
}

}
}
23

OUTPUT ➖
Enter the first values :67
Enter the second values :32
Enter the third values : 11

Largest values = 67

Process finished with exit code 0


24

Practical No. ➖ 10
Write program that gets a number from the user and generates an integer
between 1 and 7 subsequently should display the name of the weekday as per
that number.

package com.SyllabusQuestions5thSem;
import java.util.Random;
import java.util.Scanner;

public class Experiment_10 {


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

int randomNumber;
String day;

System.out.println("Enter any number :");


obj.nextInt();

randomNumber = random.nextInt(7);
// randomNumber = obj.nextInt();

switch (randomNumber) {
case 1:
day = "Monday !";
break;
case 2:
day = "Tuesday !";
break;
case 3:
day = "Wednesday !";
break;
25
case 4:
day = "Thursday !";
break;
case 5:
day = "Friday !";
break;
case 6:
day = "Saturday !";
break;
case 7:
day = "Sunday !";
break;
default:
day = "Invalid input..";
break;
}
System.out.println("Random Generated Number = " + randomNumber);
System.out.println("Week-Day is : " + day);
}
}
26

OUTPUT ➖
Enter any number : 878

Random Generated Number = 3

Week-Day is : Wednesday !

Process finished with exit code 0


27

Practical No. ➖ 11
Construct a Java program to find the number of days in a month.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_11 {


public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
int month, year,days;
System.out.println("Carefully ! Enter under the month (1-12) :");
month = obj.nextInt();
// System.out.println("Enter the years :");
// year = obj.nextInt();

switch (month){
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = 28;
break;
default:
days =31;
break;
}
System.out.println("Number of day in month "+ month + "of year : "+ days);
}
}
28

OUTPUT ➖
Carefully ! Enter under the month (1-12) : 3

Number of day in month 3 of year : 31

Process finished with exit code 0


29

Practical No. ➖ 12
Write a program to sum values of a Single Dimensional array.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_12 {


public static void main(String[] args)
{
int i,size;
int arr[]= new int[10];
Scanner obj = new Scanner(System.in);
System.out.print("Enter the size of 1-D array :");
size = obj.nextInt();
System.out.println("Enter the elements of 1-D Array :");
for(i=0;i<size;i++)
{
arr[i]=obj.nextInt();
}
System.out.print("1-D Array = ");
for(i=0;i<size;i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
int sum=0;
for(i=0;i<size;i++)
{
sum += arr[i] ;
}
System.out.println("Sum of the elements of the array = "+ sum);
}
}
30

OUTPUT ➖
Enter the size of 1-D array : 5
Enter the elements of 1-D Array :
3
4
3
6
7

1-D Array = 3 4 3 6 7
Sum of the elements of the array = 23

Process finished with exit code 0


31

Practical No. ➖ 13
Design & execute a program in Java to sort a numeric array and a string array.

package com.SyllabusQuestions5thSem;
import java.util.Arrays;
import java.util.Scanner;

public class Experiment_13 {


public static void main(String[] args)
{
int i,j,temp=0,size;
int arr[ ] = new int[10];
Scanner obj = new Scanner(System.in);
System.out.print("Enter the size of 1-D array :");
size = obj.nextInt();

System.out.println("Enter the elements of 1-D array : ");


for(i=0;i<size;i++)
{
arr[i] = obj.nextInt();
}
System.out.print("1-D Array = ");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println( );
System.out.print("After sorting in ascending order , new array = ");
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
32
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println();
for(i=0;i<size;i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();

// STRING SORTING CONCEPTS AND SOLUTIONS : !! ;

// String[] stringArrayIs = {"Sahil","Rohit","Vikki","Yuvraj","Rauniyar" };

String[] stringArrayIs = {"zyx","pqr","ghi","def","abc"};


System.out.print("Before Sorting The String Array is : "+ Arrays.toString(stringArrayIs));
System.out.println();

Arrays.sort(stringArrayIs);
System.out.println("After The Sorting String , And The New String is : "+
Arrays.toString(stringArrayIs));

}
}
33

OUTPUT ➖
Enter the size of 1-D array : 4
Enter the elements of 1-D array :
2
5
4
1

1-D Array = 2 5 4 1
After sorting in ascending order , new array = 1 2 4 5

Before Sorting The String Array is : [zyx, pqr, ghi, def, abc]

After The Sorting String , And The New String is : [abc, def, ghi, pqr, zyx]

Process finished with exit code 0


34

Practical No. ➖ 14
Calculate the average value of array elements through Java Program.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_14 {


public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
int i;
float size,sum=0,avg;
int arr[] = new int[10];
System.out.print("Enter the size of 1-D array :");
size = obj.nextInt();
System.out.println("Enter the elements of 1-D array :");
for(i=0;i<size;i++)
{
arr[i] = obj.nextInt();
}
System.out.print("1-D Array = ");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+ " ");
}
for(i=0;i<size;i++) {
sum+=arr[i];
}
System.out.println();
System.out.println("Sum of the array elements = "+ sum);
avg=sum/size;
System.out.println("Average of the array elements = "+ avg);
} }
35

OUTPUT ➖
Enter the size of 1-D array : 4
Enter the elements of 1-D array :
2
4
3
1

1-D Array = 2 4 3 1

Sum of the array elements = 10.0


Average of the array elements = 2.5

Process finished with exit code 0


36

Practical No. ➖ 15
Write a Java program to test if an array contains a specific value.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_15 {


public static void main(String[] args)
{
int size,i;
int arr[] = new int[10];
Scanner obj = new Scanner(System.in);
System.out.print("Enter the size of array :");
size = obj.nextInt();
System.out.println("Enter the elements of array :");
for(i=0;i<size;i++)
{
arr[i] = obj.nextInt();
}
System.out.print("1-D Array = ");
for (i=0;i<size;i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();

int valuesCheck;
boolean found = false;

System.out.print("Enter an value to check that is available in the array or not ! : ");


valuesCheck = obj.nextInt();
for(int array : arr)
{
37
if(array == valuesCheck)
{
found = true;
break;
}
}

if(found)
{
System.out.println("Yes ! Array Contain this : "+ valuesCheck + " values !!");
}
else{
System.out.println("No ! Does not contains this : "+ valuesCheck + " values !!");
}

}
}
38

OUTPUT ➖
Enter the size of array : 4
Enter the elements of array :
2
3
5
1

1-D Array = 2 3 5 1

Enter a value to check that is available in the array or not ! : 3


Yes ! Array Contain this : 3 values !!

Process finished with exit code 0


39

Practical No. ➖ 16
Find the index of an array element by writing a program in Java.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_16 {


public static void main(String[] args)
{
int size, i;
int arr[] = new int[10];
Scanner obj = new Scanner(System.in);

System.out.print("Enter the size of array :");


size = obj.nextInt();

System.out.println("Enter the elements of 1-D array :");


for(i=0;i<size;i++)
{
arr[i] = obj.nextInt();
}
System.out.print("1-D Array = ");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+ " ");
}
System.out.println();

int SearchPositions, index = -1;


System.out.print("Enter a elements to check their positions :");
SearchPositions = obj.nextInt();
40
for(i=0;i<size;i++)
{
if(arr[i] == SearchPositions)
{
index = i;
break;
}
}
if(index != -1)
{
System.out.println(arr[i] + " Elements Positions is : "+ i + " th");
}
else{
System.out.println("Element are not in array : !! ");
}
}

}
41

OUTPUT ➖
Enter the size of array : 5
Enter the elements of 1-D array :
3
5
3
7
1

1-D Array = 3 5 3 7 1

Enter a elements to check their positions :7


7 Elements Positions is : 3 th

Process finished with exit code 0


42

Practical No. ➖ 17
Write a Java program to remove a specific element from an array.

package com.SyllabusQuestions5thSem;
import java.util.Arrays;
import java.util.Scanner;

public class Experiment_17 {


public static void main(String[] args)
{
int size, i,pos;
Scanner obj = new Scanner(System.in);
System.out.print("Enter the size of array : ");
size = obj.nextInt();

int arr[] = new int[size];


System.out.println("Enter the elements of 1-D array :");
for(i=0;i<size;i++)
{
arr[i]=obj.nextInt();
}
System.out.println("1-D Array is : ");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}

System.out.println();
System.out.println("Enter the pos of element to remove that : ");
pos = obj.nextInt();
43
// 1st way;
for(i=pos;i<size-1;i++)
{
arr[i] = arr[i+1];
}
size--;

System.out.println("After removing , New Array is : ");


for(i=0;i<size;i++)
{
System.out.print(arr[i]+ " ");
}
}
}
44

OUTPUT ➖
Enter the size of array : 4
Enter the elements of 1-D array :
2
5
8
3

1-D Array is :
2 5 8 3

Enter the pos of element to remove that :


1

After removing , New Array is :


2 8 3
Process finished with exit code 0
45

Practical No. ➖ 18
Design a program to copy an array by iterating the array.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_18 {


public static void main(String[] args){
int i,size, ele;
Scanner obj = new Scanner(System.in);
System.out.print("Enter the size of array : ");
size = obj.nextInt();
int OriginalArr[] = new int[size];
System.out.println();
System.out.println("Enter the elements of array : ");
for(i=0;i<size;i++)
{
OriginalArr[i] = obj.nextInt();
}
System.out.println("Original Array is : ");
for(i=0;i<size;i++)
{
System.out.print(OriginalArr[i]+" ");
}
System.out.println();
System.out.println("Copied Array is: ");
int CopiedArr[] = new int[size];
for(i=0;i<size;i++)
{
CopiedArr[i] = OriginalArr[i] ;
System.out.print(CopiedArr[i]+" ");
}
} }
46

OUTPUT ➖
Enter the size of array : 5

Enter the elements of array :


3
6
8
4
1

Original Array is :
3 6 8 4 1

Copied Array is:


3 6 8 4 1
Process finished with exit code 0
47

Practical No. ➖ 19
Write a Java program to insert an element (on a specific position) into
Multidimensional array.

package com.SyllabusQuestions5thSem;

import org.w3c.dom.ls.LSOutput;

import java.util.Scanner;

// 19 Write a Java program to insert an element (on a specific position) into


// Multidimensional array.
public class Experiment_19 {
public static void main(String[] args){
Scanner obj = new Scanner(System.in);
int r,c,i,j;
System.out.print("Enter the size of row : ");
r = obj.nextInt();
System.out.print("Enter the size of columns : ");
c = obj.nextInt();
int matrix1[][]=new int[r][c];
System.out.println();
System.out.println("Enter the elements of matrix : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
matrix1[i][j] = obj.nextInt();
}
}
System.out.println("Original Matrix is : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
48
{
System.out.print(matrix1[i][j] + " ");
}
System.out.println();
}
int RowIndex, ColIndex, NewElement;
System.out.println("Enter the specific positions of row index : ");
RowIndex= obj.nextInt();
System.out.println("Enter the specific positions of col index : ");
ColIndex=obj.nextInt();
System.out.println("Enter elements which you want to insert : ");
NewElement = obj.nextInt();
if(RowIndex >= 0 && RowIndex < r && ColIndex >= 0 && ColIndex < c ) {
matrix1[RowIndex][ColIndex] = NewElement;

System.out.println("After inserting new element , Updated Matrix is : ");


for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
System.out.print(matrix1[i][j] + " ");
}
System.out.println();
}
}
else{
System.out.println("Something Error..");
}
obj.close();
}
}
49

OUTPUT ➖
Enter the size of row : 2
Enter the size of columns : 3

Enter the elements of matrix :


5
3
6
7
3
1

Original Matrix is :
5 3 6
7 3 1

Enter the specific positions of row index :


1
Enter the specific positions of col index :
2
Enter elements which you want to insert :
10

After inserting new element , Updated Matrix is :


5 3 6
7 3 10

Process finished with exit code 0


50

Practical No. ➖ 20
Write a program to perform following operations on strings:

1) Compare two strings.


2) Count string length.
3) Convert upper case to lower case & vice versa.
4) Concatenate two strings.
5) Print a substring.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_20 {


public static void main(String[] args){
String str1, str2;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the first string : ");
str1 = obj.nextLine();
System.out.println("Enter the second string : ");
str2 = obj.nextLine();
if(str1.equals(str2)){
System.out.println("Both string are equal ! ");
}
else{
System.out.println("Both string are different ! ");
}

System.out.println("First String Length is :"+ str1.length());


System.out.println("Second String Length is : "+ str2.length());

System.out.println("First string convert lower into the upper-case : "+ str1.toUpperCase());


System.out.println("First string convert upper into lower case : "+ str1.toLowerCase());
System.out.println("Second string convert lower into upper-case : "+ str2.toUpperCase());
System.out.println("Second string convert upper into lower case : " + str2.toLowerCase());
51

String concat;
concat = str1 + str2;
System.out.println("After Concatenate first and second string , New String is : "+ concat);

int startIndex , endIndex;


System.out.println("Enter the starting index of any one string : ");
startIndex = obj.nextInt();
System.out.println("Enter the ending index of any one string : ");
endIndex = obj.nextInt();
if(startIndex >= 0 && startIndex <= str1.length() && startIndex <= endIndex){
System.out.println("Substring of first string is : "+ str1.substring(startIndex,endIndex));
}
else{
System.out.println("Error Occurring to finding the sub-string !");
}
}
}
52

OUTPUT ➖
Enter the first string :
SahIL_rAuNiYaR
Enter the second string :
ViKKI_

Both string are different !

First String Length is :14


Second String Length is : 6

First string convert lower into the upper-case : SAHIL_RAUNIYAR


First string convert upper into lower case : sahil_rauniyar

Second string convert lower into upper-case : VIKKI_


Second string convert upper into lower case : vikki_

After Concatenate first and second string , New String is : SahIL_rAuNiYaRViKKI_

Enter the starting index of any one string :


3
Enter the ending index of any one string :
7
Substring of first string is : IL_r

Process finished with exit code 0


53

Practical No. ➖ 21
Developed Program & design a method to find the smallest number among three
numbers.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;
public class Experiment_21 {
public static void FindSmallestNum(int num1, int num2, int num3){
if(num1 < num2 && num1 < num3){
System.out.println("Smallest Number is : "+ num1);
}
else if(num2 < num1 && num2 < num3){
System.out.println("Smallest Number is : "+ num2);
}
else if(num3 < num1 && num3 < num2){
System.out.println("Smallest Number is : "+ num3);
}
else if(num1 == num2 && num2 == num3){
System.out.println("Both numbers are equal ! ");
}
else {
System.out.println("Invalid input..");
} }
public static void main(String[] args){
int a, b, c ;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the first number : ");
a = obj.nextInt();
System.out.println("Enter the second number : ");
b = obj.nextInt();
System.out.println("Enter the third number : ");
c = obj.nextInt();
FindSmallestNum(a,b,c);
} }
54

OUTPUT ➖
Enter the first number :
10
Enter the second number :
6
Enter the third number :
332
Smallest Number is : 6

Process finished with exit code 0


55

Practical No. ➖ 22
Compute the average of three numbers through a Java Program.

package com.SyllabusQuestions5thSem;

import java.awt.*;
import java.util.Scanner;

public class Experiment_22 {


public static void main(String[] args){
int num1, num2 , num3, sum, avg;
Scanner obj = new Scanner(System.in);
System.out.println("Enter the value of first number : ");
num1 = obj.nextInt();

System.out.println("Enter the value of second number : ");


num2 = obj.nextInt();

System.out.println("Enter the value of third number : ");


num3 = obj.nextInt();

sum = (num1 + num2 + num3);


avg = (sum/3);
System.out.println("Average of these three numbers is => "+ avg);
}
}
56

OUTPUT ➖
Enter the value of first number :
80
Enter the value of second number :
78.5
Enter the value of third number :
77
Average of these three numbers is => 78.5

Process finished with exit code 0


57

Practical No. ➖ 23
Write a Program & design a method to count all vowels in a string.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_23 {


public static void CountVowels(String str1){
int count = 0;
for(int i=0; i < str1.length();i++)
{
char ch = str1.charAt(i);
if(ch =='a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' && ch == 'A' ||
ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
count++;
// System.out.println(ch);
}
}
System.out.println(count);

}
public static void main(String[] args){
String str ;
Scanner obj = new Scanner(System.in);
System.out.println("Enter any string to count their vowels : ");
str= obj.nextLine();
// System.out.println("Vowels are : ");
System.out.print("Total number of vowels => ");
CountVowels(str);

}
}
58

OUTPUT ➖
Enter any string to count their vowels :
sahil

Total number of vowels => 2

Process finished with exit code 0


59

Practical No. ➖ 24
Write a Java method to count all words in a string.

package com.SyllabusQuestions5thSem;
import java.util.Scanner;

public class Experiment_24 {

public static int countWords(String str) {


if (str == null || str.isEmpty()) {
return 0;
}

String[] words = str.trim().split("\\s+");


return words.length;
}

public static void main(String[] args) {


String testStr = " Hello Sahil Rauniyar , this is a test string with multiple words. ";
System.out.println("Word count: " + countWords(testStr));

}
}
60

OUTPUT ➖
Word count: 12

Process finished with exit code 0


61

Practical No. ➖ 25
Write a method in a Java program to count all words in a string.

package com.SyllabusQuestions5thSem;

public class Experiment_25 {


public static int countWords(String str) {
if (str == null || str.isEmpty()) {
return 0;
}

String[] words = str.trim().split("\\s+");


return words.length;
}

public static void main(String[] args) {


String testStr = " Hello Sahil Rauniyar , Please count all the string of these sentence ! ";
System.out.println("Original String is : "+ testStr);
System.out.println("Word count: " + countWords(testStr));
}
}
62

OUTPUT ➖
Original String is : Hello Sahil Rauniyar , Please count all the string of these sentences !
Total String count is : 13

Process finished with exit code 0

You might also like