Assignment no : 1
Set A
a) Write a java program to accept names of ‘n’ cities, insert same into array list collection
and display the contents of same array list, also remove all these elements
import [Link];
import [Link];
public class CityListManager {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
ArrayList<String> cityList = new ArrayList<>();
[Link]("Enter the number of cities: ");
int n = [Link]();
[Link](); // Consume the newline character
for (int i = 0; i < n; i++) {
[Link]("Enter city name: ");
String city = [Link]();
[Link](city);
}
[Link]("Cities in the ArrayList: " + cityList);
[Link]();
[Link]("ArrayList after removing all cities: " + cityList);
[Link]();
}
}
b) Write a java program to read ‘n’ names of your friends, store it into linked list, also
display contents of the same.
import [Link];
import [Link];
public class FriendList {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
LinkedList<String> friendList = new LinkedList<>();
[Link]("Enter the number of friends: ");
int n = [Link]();
[Link](); // Consume the newline character
for (int i = 0; i < n; i++) {
[Link]("Enter friend's name: ");
String name = [Link]();
[Link](name);
}
[Link]("Friends in the LinkedList: " + friendList);
[Link]();
}
}
c) Write a program to create a new tree set, add some colors (string) and print out the tree set.
import [Link];
public class ColorTreeSet {
public static void main(String[] args) {
TreeSet<String> colors = new TreeSet<>();
// Adding some colors to the TreeSet
[Link]("Red");
[Link]("Blue");
[Link]("Green");
[Link]("Yellow");
[Link]("White");
// Printing out the TreeSet
[Link]("Colors in the TreeSet: " + colors);
}
}
d) Create the hash table that will maintain the mobile number and student name. Display the
contact list.
import [Link];
import [Link];
public class ContactList {
public static void main(String[] args) {
Hashtable<String, String> contacts = new Hashtable<>();
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of contacts: ");
int n = [Link]();
[Link](); // Consume the newline character
for (int i = 0; i < n; i++) {
[Link]("Enter student name: ");
String name = [Link]();
[Link]("Enter mobile number: ");
String mobile = [Link]();
[Link](mobile, name);
}
[Link]("\nContact List:");
for (String mobile : [Link]()) {
[Link]("Mobile: " + mobile + ", Name: " + [Link](mobile));
}
[Link]();
}
}
Set B
a) Accept ‘n’ integers from the user. Store and display integers in sorted order having proper
collection class. The collection should not accept duplicate elements.
import [Link];
import [Link];
public class SortedIntegers {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
TreeSet<Integer> numbers = new TreeSet<>();
[Link]("Enter the number of integers: ");
int n = [Link]();
[Link]("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
int number = [Link]();
[Link](number); // TreeSet ensures no duplicates and sorts automatically
}
[Link]("Sorted integers without duplicates: " + numbers);
[Link]();
}
}
b) Write a program to sort HashMap by keys and display the details before sorting and after
sorting.
import [Link].*;
public class SortHashMapByKey {
public static void main(String[] args) {
// Creating a HashMap
HashMap<Integer, String> map = new HashMap<>();
[Link](3, "Alice");
[Link](1, "Charlie");
[Link](2, "Bob");
// Displaying the HashMap before sorting
[Link]("HashMap before sorting: " + map);
// Sorting the HashMap by keys using a TreeMap
TreeMap<Integer, String> sortedMap = new TreeMap<>(map);
// Displaying the sorted HashMap
[Link]("HashMap after sorting by keys: " + sortedMap);
}
}
c) Write a program that loads names and phone numbers from a text file where the data is
organized as one line per record and each field in a record are separated by a tab (\t).it takes a
name or phone number as input and prints the corresponding other value from the hash table
(hint: use hash tables)
import [Link].*;
import [Link];
import [Link];
public class PhoneBook {
public static void main(String[] args) {
Hashtable<String, String> phoneBook = new Hashtable<>();
Scanner scanner = new Scanner([Link]);
// Load data from a text file
try (BufferedReader br = new BufferedReader(new FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null) {
String[] parts = [Link]("\t"); // Split by tab character
if ([Link] == 2) {
[Link](parts[0], parts[1]); // Name as key, phone as value
[Link](parts[1], parts[0]); // Phone as key, name as value
}
}
} catch (IOException e) {
[Link]("Error reading file: " + [Link]());
return;
}
// Accept input for searching
[Link]("Enter a name or phone number to search: ");
String query = [Link]();
// Search and display the corresponding value
if ([Link](query)) {
[Link]("Result: " + [Link](query));
} else {
[Link]("No match found!");
}
[Link]();
}
}
Set C
b) Write a program to create link list of integer objects. Do the following:
i. add element at first position
ii. delete last element
iii. display the size of link list
import [Link].*;
class linkedlist2
{
public static void main(String args[])
{
LinkedList<Integer> al=new LinkedList<Integer>();
int n,i;
Scanner sc=new Scanner([Link]);
[Link]("Enter the no of integers ");
n=[Link]();
[Link]("Enter the integers you want");
for(i=0;i<n;i++)
{
int c=[Link]();
[Link](c);
[Link](al);
[Link](100);
[Link](al);
[Link](90);
[Link]("Adding element at first position list is"+al);
[Link]();
[Link]("Deleting the last element"+al);
[Link]("Size of the element is"+[Link]());
}
}