6Java Problem Solving questions (OOPS,Collections)
Day 1
1. Write a Java program to create a class called "Student" with a name, grade, and
courses attributes, and methods to add and remove courses.
2. Write a Java program to create a class with methods to search for flights and
hotels, and to book and cancel reservations.
Day 2:
3. Write a Java program to create a class called "BankAccount" with attributes for
account number, account holder's name, and balance. Include methods for depositing
and withdrawing money, as well as checking the balance. Create a subclass called
"SavingsAccount" that adds an interest rate attribute and a method to apply interest.
4. Write a Java program to create a class called "Person" with a name and age
attribute. Create two instances of the "Person" class, set their attributes using the
constructor, and print their name and age.
Day 3:
5. Write a Java program to create a class called "Shape" with abstract methods for
calculating area and perimeter, and subclasses for "Rectangle", "Circle", and
"Triangle".
6. Write a simple program for arithmetic addition,subtraction using operator
overloading
Day 4:
7. Write a simple abstract class program Student and Staff class relationship
8. Perform simple multi level inheritance program for Infrastructure domain
organization
Day 5:
9. Write a simple interface program for different types of RDBS servers
10. Write a program to find the hierarchical inheriance for hospital management
system
Day 6 :
11. Write a Java program to create a class called "Inventory" with a collection of
products and methods to add and remove products, and to check for low inventory.
12. Create a simple method overloading for Person class
Day 7:
13. How to Perform Method overriding concept in Student and Staff class relationship.
Explain with simple code
14. Write a Java program to create a class called "Customer" with attributes for name,
email, and purchase history. Implement methods to add purchases to the history and
calculate total expenditure.
Also, Create a subclass "RegularCustomer" that adds a discount rate attribute and a
method to apply the discount 10%.
Day 8:
15. Write a simple to achieve encapsulation technique.
16. Write a Java program to create a class Employee with a method called
calculateSalary(). Create two subclasses Manager and Programmer. In each subclass,
override the calculateSalary() method to calculate and return the salary based on their
specific roles.
Day 9 :
17. Write a Java program to create a vehicle class hierarchy. The base class should be
Vehicle, with subclasses Truck, Car and Motorcycle. Each subclass should have
properties such as make, model, year, and fuel type. Implement methods for
calculating fuel efficiency, distance traveled, and maximum speed.
18. Write a Java program to create an abstract class Shape3D with abstract methods
calculateVolume() and calculateSurfaceArea(). Create subclasses Sphere and Cube
that extend the Shape3D class and implement the respective methods to calculate the
volume and surface area of each shape.
Day 10:
19. Write a Java program to create an abstract class Bird with abstract methods fly()
and makeSound(). Create subclasses Eagle and Hawk that extend the Bird class and
implement the respective methods to describe how each bird flies and makes a
sound.
20. Write The Program to Find [Link] Words , [Link] Chars with space and Without
space in given String.
Input : This is Hello
Output:
[Link] Words = 3
[Link] Chars (ws) = 13
[Link] Chars (wos)= 11
Day 11
2. Write The Program for The following
Input : This is Hello
Output : sihT is olleH
3. Write The Program for The Following
Input : Hello
Output :
H = 1 = 72
e = 2 = 101
l = 3 = 108
l = 4 = 108
o = 5 = 111
Day 12
1 Find Missing Numbers from 1 to N
Input: N = 5, nums = [2, 3, 1, 5]
Output: [4]
2. Write The Program to find the [Link] Occurrence of The given char with index in
String.
Input 1: Hello
Input 2: l
Output : 2
2,3
Day 13
1. Write The program to find [Link] occurrence of all characters in given String.
Input : Hello
Output : l – 2, H – 1, e – 1, o- 1
2. Given a string S(input consisting) of vowels(aeiou) and consonants. The length
of the string is variable. The task is to find the minimum number of vowels or
consonants to make it a valid string. The string is considered valid if the number of
vowels and consonants are equal. The vowels and consonants can be at any position
in the string.
Note : The output will be a strong or weak string based on number of vowels and
consonants in the input string.
vowels > consonants = "Strong"
consonants > vowels = "Weak"
vowels = consonants = "Average"
Sample Input:
Hello -> Value of S
Sample Output :
Weak (number of vowels are less than consonants)
Sample Input :
apes -> Value of S
Sample Output :
Average ( numbers of vowels and consonants are equal)
Day 14
1. Write the program to concatenate the two strings with index.
Sample Input:
input1 = "apple"
input2 = "grape"
Sample Output:
agprpalpee (index of 0 of first string concatenate with index of 0 with string2)
Sample Input :
Input 1 = “abc”
Input 2 = “p”
Output = “apbc”
2. Write The program for the following scenario
Input 1 : ABCD
Input 2 : 2
Output : CDEF
Input 1 : XYZ9
Input 2: 2
Output : ZAB1
Day 15
1. Write The program for removing the vowels in given String.
Input 1: Hello
Output : Hll
2. Write The Program for Following
Input : Hello
Output :
Hello
ell
l
ell
Hello
Input : besant
Output : Not possible because length of string is even , possible only odd
Day 16
1. Write a Java program to divide a string into n equal parts.
Sample Output:
The given string is: abcdefghijklmnopqrstuvwxy
n=5
abcde
fghij
klmno
pqrst
uvwxy
2. Write a Java program to create a method that takes an integer as a parameter
and throws an exception if the number is odd.
Day 17
1. Write a Java program to create a method that takes a string as input and
throws an exception if the string does not contain vowels.
2. Write The program to display the characters of given string with 2 secs delay
with each characters.
Day 18
1. Write The program to call the exception while the length of the string is more
than 10.
Sample Input:
input1 = "abcdefghijkl"
Sample Output:
Exception in String length is out of control (length of given input is >10)
2. int a[]=new int[5];
int b=a[5]/0;
Find The Exception in this code and Handle it with corresponding Exception
Day 19
1. Write a Java Program to remove every second letter of the given string
Sample Output:
The given string is: Hello
After remove all every second letter : Hlo
2. Given a username and password as input string from user and verify the string
from the text file line1 and line2 as respectively. If the strings are correct then print
“Login successfully”. Else “Login failed”.
Day 20
1. Write a Program to Find The [Link] Occurrence of a given word in a Text File.
2. Write a Java program to find the first non-repeating character in a string.
Sample Output:
The given string is: gibblegabbler
The first non repeated character in String is: i
Day 21:
1. Create your own class and achieve Is-A and Has-A relationship.
2. Count Occurrences in ArrayList Input:
[1, 2, 2, 3, 3, 3]
Output: 1 -> 1 2 -> 2 3 -> 3
Day 22:
[Link] Duplicates from ArrayList
Input: [1, 2, 2, 3, 3, 3]
Output: [1, 2, 3]
[Link] an ArrayList without using sort method
Input: [1, 2, 3, 4]
Output: [4, 3, 2, 1]
Day 23:
[Link] Max Element in ArrayList
Input: [10, 30, 20, 40]
Output: 40
[Link] if ArrayList is Palindrome
Input: [1, 2, 3, 2, 1]
Output: true
Day 24
[Link] Two ArrayLists into same arraylist without pushing into new arraylist
Input: [1, 2], [3, 4] Output: [1, 2, 3, 4]
[Link] Common Elements in Two ArrayLists
Input: [1, 2, 3], [2, 3, 4]
Output: [2, 3]
Day 25
[Link] Frequency of Characters in a String using HashMap
Input: "aabbcc"
Output: a -> 2 b -> 2 c -> 2
[Link] if Two Strings are Anagrams using HashMap
Input: "listen", "silent"
Output: true
Day 26
[Link] First Non-Repeating Character
Input: "aabbcde"
Output: c
[Link] Words by Length using HashMap
Input: ["apple", "bat", "car"]
Output: 3 -> ["bat", "car"] 5 -> ["apple"]
Day 27
[Link] Map by Values
Input: {a=3, b=1, c=2}
Output: {b=1, c=2, a=3}
[Link] for Duplicates using HashMap
Input: [1, 2, 3, 4, 2]
Output: true
Day 28
[Link] Most Frequent Word in List
Input: ["apple", "banana", "apple"]
Output: apple
[Link] Unique Elements in List using HashSet
Input: [1, 2, 2, 3]
Output: [1, 2, 3]
Day 29
[Link] if Set Contains Element
Input: ["apple", "banana"], element = "banana"
Output: true
[Link] Intersection of Two Sets
Input: [1, 2, 3], [2, 3, 4]
Output: [2, 3]
Day 30
[Link] for Duplicates using HashSet
Input: [1, 2, 3, 4, 1]
Output: true
[Link] of Two Sets
Input: [1, 2], [2, 3]
Output: [1, 2, 3]