You are on page 1of 11

Slip 2

1. Write a java program to read ‘N’ names of your friends, store it into HashSet and display
them in ascending order.

Program:
import java.util.*;

public class FriendNames {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of friends: ");
int n = input.nextInt();

Set<String> friends = new HashSet<>();

// Reading 'n' names from user


for (int i = 1; i <= n; i++) {
System.out.print("Enter name " + i + ": ");
String name = input.next();
friends.add(name);
}

// Sorting and displaying the names in ascending order


List<String> sortedFriends = new ArrayList<>(friends);
Collections.sort(sortedFriends);
System.out.println("\nList of friends in ascending order:");
for (String friend : sortedFriends) {
System.out.println(friend);
}
}
}

Explanation:
1. The program starts by importing the necessary classes - java.util.*.
2. The user is prompted to enter the number of friends to store in the HashSet.
3. A HashSet friends is created to store the names of friends.
4. Using a for loop, the program reads 'n' names of friends from the user and adds them
to the HashSet.
5. To display the names in ascending order, the HashSet is converted to an ArrayList
sortedFriends, which can be sorted using the Collections.sort() method.
6. Finally, the sorted names are displayed using a for-each loop.

Note: HashSet does not maintain the order of elements, hence we have used an ArrayList to
sort the elements.

Slip 3
2. Write a Java program to create LinkedList of String objects and perform the following:
i. Add element at the end of the list.
ii. Delete first element of the list.
iii. Display the contents of list in reverse order.

Program:
import java.util.LinkedList;

public class LinkedListExample {


public static void main(String[] args) {
// Create a LinkedList of String objects
LinkedList<String> list = new LinkedList<>();

// Add elements to the end of the list


list.add("A");
list.add("B");
list.add("C");
list.add("D");
System.out.println("Original list: " + list);

// Delete the first element of the list


list.removeFirst();
System.out.println("List after deleting first element: " + list);

// Display the contents of the list in reverse order


System.out.print("Reverse order of the list: ");
for (int i = list.size() - 1; i >= 0; i--) {
System.out.print(list.get(i) + " ");
}
}
}

Slip 4
2. Write a Java program to store city names and their STD codes using an appropriate
collection and perform following operations:
i. Add a new city and its code (No duplicates).
ii. Remove a city from the collection.
iii. Search for a city name and display the code.
Program:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CityCodesExample {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Create a HashMap to store city names and STD codes


Map<String, String> cityCodes = new HashMap<>();
cityCodes.put("Mumbai", "022");
cityCodes.put("Delhi", "011");
cityCodes.put("Kolkata", "033");
cityCodes.put("Chennai", "044");

// Print the original HashMap


System.out.println("Original HashMap: " + cityCodes);

// Add a new city and its code


System.out.print("Enter a city name to add: ");
String city = sc.nextLine();
if (cityCodes.containsKey(city)) {
System.out.println("City already exists in the HashMap.");
} else {
System.out.print("Enter the STD code for " + city + ": ");
String code = sc.nextLine();
cityCodes.put(city, code);
System.out.println("City " + city + " added to the HashMap with code " + code + ".");
System.out.println("Updated HashMap: " + cityCodes);
}

// Remove a city from the collection


System.out.print("Enter a city name to remove: ");
city = sc.nextLine();
if (cityCodes.containsKey(city)) {
cityCodes.remove(city);
System.out.println("City " + city + " removed from the HashMap.");
System.out.println("Updated HashMap: " + cityCodes);
} else {
System.out.println("City not found in the HashMap.");
}

// Search for a city name and display the code


System.out.print("Enter a city name to search: ");
city = sc.nextLine();
if (cityCodes.containsKey(city)) {
String code = cityCodes.get(city);
System.out.println("STD code for " + city + " is " + code + ".");
} else {
System.out.println("City not found in the HashMap.");
}
}
}

Slip 5
1. Write a Java Program to create the hash table that will maintain the mobile number and
student name. Display the details of student using Enumeration interface.

Program:
import java.util.*;

public class Student {


public static void main(String args[]) {
Hashtable<String, Long> hashtable = new Hashtable<String, Long>();
hashtable.put("A",12345L);
hashtable.put("B",67890L);
hashtable.put("C",54454L);
hashtable.put("D",84362L);
hashtable.put("E",84849L);

System.out.println(hashtable);

Enumeration<String> e = hashtable.keys();
while (e.hasMoreElements()) {
String studentName = e.nextElement();
Long mobileNumber = hashtable.get(studentName);
System.out.println("Student Name: " + studentName);
System.out.println("Mobile Number: " + mobileNumber);
System.out.println();
}
}
}

Explanation:

1. The program creates a Hashtable that maps student names to mobile numbers. The
keys of the Hashtable are student names and the values are mobile numbers.
2. The program adds some sample student details to the Hashtable. Note that the mobile
numbers are now being stored as integers.
3. The Enumeration interface is used to iterate over the keys of the Hashtable using the
keys() method, which returns an enumeration of the keys in the Hashtable.
4. The hasMoreElements() method of Enumeration is used to check if there are more
keys in the enumeration, and the nextElement() method is used to retrieve the next
key.
5. For each student name, the program retrieves the corresponding mobile number using
the get() method of the Hashtable and displays the details of the student.

Slip 6
1. Write a Java program to accept ‘n’ integers from the user and store them in a
collection. Display them in the sorted order. The collection should not accept
duplicate elements. (Use a suitable collection). Search for a particular element using
predefined search method in the Collection framework.

Program:

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class IntegerSet {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
// Create a set to store integers
Set<Integer> numbers = new TreeSet<>();

// Accept n integers from the user and add them to the set
System.out.print("Enter the number of integers: ");
int n = sc.nextInt();
System.out.print("Enter " + n + " integers: ");
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
numbers.add(num);
}

// Display the sorted integers


System.out.println("Sorted Integers: " + numbers);

// Search for a particular element


System.out.print("Enter a number to search for: ");
int searchNum = sc.nextInt();
if (numbers.contains(searchNum)) {
System.out.println(searchNum + " is present in the set.");
} else {
System.out.println(searchNum + " is not present in the set.");
}
}
}
Slip 16
1. Write a java program to create a TreeSet, add some colors (String) and print out the
content of TreeSet in ascending order.

Program:

import java.util.*;

public class TreeSetExample {


public static void main(String[] args) {
TreeSet<String> colors = new TreeSet<String>();
colors.add("red");
colors.add("green");
colors.add("blue");
colors.add("yellow");
colors.add("orange");

System.out.println("Colors in ascending order: " + colors);


}
}

Explanation:

1.The program creates a TreeSet of strings called colors.


2.The program adds some sample colors to the TreeSet using the add() method.
3.The TreeSet automatically sorts the colors in ascending order.
4.The program prints out the content of the TreeSet in ascending order using the
println() method.

Slip 17
1. Write a java program to accept ‘N’ integers from a user. Store and display integers in
sorted order having proper collection class. The collection should not accept duplicate
elements.

Program :

import java.util.*;

public class SortIntegers {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();

SortedSet<Integer> set = new TreeSet<>();

for (int i = 0; i < n; i++) {


System.out.print("Enter integer " + (i+1) + ": ");
int num = scanner.nextInt();
set.add(num);
}

System.out.println("Sorted integers:");
for (int num : set) {
System.out.println(num);
}
}
}

Slip 19
1. Write a java program to accept ‘N’ Integers from a user store them into LinkedList
Collection and display only negative integers.
Program:

import java.util.*;

public class NegativeIntegersInLinkedList {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();

LinkedList<Integer> list = new LinkedList<>();

for (int i = 0; i < n; i++) {


System.out.print("Enter integer " + (i+1) + ": ");
int num = scanner.nextInt();
list.add(num);
}

System.out.println("Negative integers:");
for (int num : list) {
if (num < 0) {
System.out.println(num);
}
}
}
}

Slip 21
1. Write a java program to accept ‘N’ Subject Names from a user store them into
LinkedList Collection and Display them by using Iterator interface.

Program:
import java.util.*;

public class SubjectNamesInLinkedList {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the number of subjects: ");
int n = scanner.nextInt();

LinkedList<String> list = new LinkedList<>();

for (int i = 0; i < n; i++) {


System.out.print("Enter subject " + (i+1) + ": ");
String subject = scanner.next();
list.add(subject);
}

System.out.println("Subject names:");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

Slip 23

1. Write a java program to accept ‘N’ student names through command line, store
them into the appropriate Collection and display them by using Iterator and
ListIterator interface.

Program:

You might also like