LABORATORY FILE - Java Programming (5th Sem) !
LABORATORY FILE - Java Programming (5th Sem) !
INDEX
Experiment
No. Title Date : Teacher’s Sign \
Remarks
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;
}
}
4
OUTPUT ➖
Enter the values of a : 19
Enter the values of b : 4
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;
}
}
6
OUTPUT ➖
First Result = 2595.0
Second Result = 1.0
Third Result = 22.0
Fourth Answers = 27.0
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;
// 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
Practical No. ➖ 04
Write a program to convert temperature from Fahrenheit to Celsius degree using
Java .
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
}
}
11
OUTPUT ➖
Enter the temperature in fahrenheit :788
After converting into fahrenheit to celsius , the temperature in celsius = 420.0 C
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;
OUTPUT ➖
Enter the number in inches : 98939
After converting, the number in metre = 2513.0506
Practical No. ➖ 06
Write a program to convert minutes into a number of years and days.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
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.
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;
}
}
17
OUTPUT ➖
Current time in GMT : = 2024-08-24 07:08:10 GMT
Practical No. ➖ 08
Design a program in Java to solve quadratic equations using if, if else .
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
d= (Math.sqrt(b*b)) - 4*a*c;
System.out.println("Discriminant values = "+ d);
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
Practical No. ➖ 09
Write a Java program to determine the greatest number of three numbers.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
int a,b,c;
System.out.print("Enter the first values :");
a=obj.nextInt();
}
}
23
OUTPUT ➖
Enter the first values :67
Enter the second values :32
Enter the third values : 11
Largest values = 67
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;
int randomNumber;
String day;
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
Week-Day is : Wednesday !
Practical No. ➖ 11
Construct a Java program to find the number of days in a month.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
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
Practical No. ➖ 12
Write a program to sum values of a Single Dimensional array.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
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
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;
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]
Practical No. ➖ 14
Calculate the average value of array elements through Java Program.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
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
Practical No. ➖ 15
Write a Java program to test if an array contains a specific value.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
int valuesCheck;
boolean found = false;
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
Practical No. ➖ 16
Find the index of an array element by writing a program in Java.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
}
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
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;
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--;
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
Practical No. ➖ 18
Design a program to copy an array by iterating the array.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
OUTPUT ➖
Enter the size of array : 5
Original Array is :
3 6 8 4 1
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;
OUTPUT ➖
Enter the size of row : 2
Enter the size of columns : 3
Original Matrix is :
5 3 6
7 3 1
Practical No. ➖ 20
Write a program to perform following operations on strings:
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
String concat;
concat = str1 + str2;
System.out.println("After Concatenate first and second string , New String is : "+ concat);
OUTPUT ➖
Enter the first string :
SahIL_rAuNiYaR
Enter the second string :
ViKKI_
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
Practical No. ➖ 22
Compute the average of three numbers through a Java Program.
package com.SyllabusQuestions5thSem;
import java.awt.*;
import java.util.Scanner;
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
Practical No. ➖ 23
Write a Program & design a method to count all vowels in a string.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
}
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
Practical No. ➖ 24
Write a Java method to count all words in a string.
package com.SyllabusQuestions5thSem;
import java.util.Scanner;
}
}
60
OUTPUT ➖
Word count: 12
Practical No. ➖ 25
Write a method in a Java program to count all words in a string.
package com.SyllabusQuestions5thSem;
OUTPUT ➖
Original String is : Hello Sahil Rauniyar , Please count all the string of these sentences !
Total String count is : 13