Lab Programs
Lab Programs
Regular Programs:
1. Demonstrate creation of multiple objects of the class.
2. Demonstrate constructor over loading in JAVA.
3. Demonstrate method over loading in Java.
4. Demonstrate this- keyword.
5. Demonstrate type casting in java.
6. Write a Java program that takes an array of integers as input and finds the largest and smallest
elements in the array.
7. Write a Java program that calculates the sum and average of all elements in an array of integers.
8. Write a Java program that reverses the elements of an array. For example, if the input is [1, 2,
3, 4, 5], the output should be [5, 4, 3, 2, 1].
9. Write a Java program that searches for a specific element in an array and prints its position. If
the element is not found, display a message indicating so.
10. Write a Java program that merges two arrays into a single array and displays the combined
array.
11. Write a Java program that finds the second largest element in an array of integers.
12. Write a Java program to find and print the duplicate elements in an array.
Usage:
n=sc.nextInt();
import java.util.Scanner;
// Reading an integer
// Reading a double
// Reading a boolean
sc.close();
}
Advanced Programs:
1. Program1
Implement a Class called Circle with two instance variable radius of type
integer , area of type of double and three instance methods namely setRadius(),
getRadius(), caluculateArea().
SetRadius() method will set the value for the variable radius ,
caluculateArea() calculates the area of the circle and prints the area.
Note: the radius of the circle should be a positive integer. if any other radius is provided print -1.
case = 1
input = 2
output = 12.56
case = 2
input = 3
output = 28.26
2. Program 2
Create a class called Employee with instance variables namely eno, ename, basicsal ,da, hra
calculateTotalSal() should calculate the total salary of the employee based on basicsal &
Create an executable class called Test in which create an instance of Employee class ,
initialize the members of Employee class with the help of Constructors &
Read the input in the following manner :- First Eno, followed by Ename followed by basicsal
Input
101
Abc
10000
Output
12500
3. Program 3
Write a program to check the given two strings are anagrams or not ? if they are anagrams print 1 else
print -1.
An anagram is a word or phrase that's formed by rearranging the letters of another word or phrase.
case = 1
input =
god
dog
output =
case = 2
input =
124
411
ouput =
-1
4. Program 4
Write a program to print the following pattern for any given postive integer 'n'
input = 4
output =
1234
2341
3412
4123
input = 5
output =
12345
23451
34512
45123
51234
input = 2
output =
12
21
input = 3
output =
123
231
312
5. Program 5
Given two integer A and B, find the count of common factors of two numbers where factors are prime.
Examples:
input = 6,12
output = 2
Explanation :-
Among them 2 and 3 are the only common prime divisors of 6 and 12 whose count is 2
input = 4,8
output = 1
6. Program 6
Among them 2 is the only commin prime divisor of 4 and 8 whose count is 1
write a java program to read a value n from the user and print the following pattern
input = 5
output =
54321
4321
321
21
input = 6
output =
654321
54321
4321
321
21
input = 245
output = 7
sum of 2 and 5 is 7
input = 237
output = 12
input = 44
output = 0
7. Program 7
input = 123
321
output = ANAGRAM
input = 121
113