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 java.util.ArrayList;
import java.util.Scanner;
public class CityListManager {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> cityList = new ArrayList<>();
System.out.print("Enter the number of cities: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
for (int i = 0; i < n; i++) {
System.out.print("Enter city name: ");
String city = scanner.nextLine();
cityList.add(city);
}
System.out.println("Cities in the ArrayList: " + cityList);
cityList.clear();
System.out.println("ArrayList after removing all cities: " + cityList);
scanner.close();
}
}
b) Write a java program to read ‘n’ names of your friends, store it into linked list, also
display contents of the same.
import java.util.LinkedList;
import java.util.Scanner;
public class FriendList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LinkedList<String> friendList = new LinkedList<>();
System.out.print("Enter the number of friends: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
for (int i = 0; i < n; i++) {
System.out.print("Enter friend's name: ");
String name = scanner.nextLine();
friendList.add(name);
}
System.out.println("Friends in the LinkedList: " + friendList);
scanner.close();
}
}
c) Write a program to create a new tree set, add some colors (string) and print out the tree set.
import java.util.TreeSet;
public class ColorTreeSet {
public static void main(String[] args) {
TreeSet<String> colors = new TreeSet<>();
// Adding some colors to the TreeSet
colors.add("Red");
colors.add("Blue");
colors.add("Green");
colors.add("Yellow");
colors.add("White");
// Printing out the TreeSet
System.out.println("Colors in the TreeSet: " + colors);
}
}
d) Create the hash table that will maintain the mobile number and student name. Display the
contact list.
import java.util.Hashtable;
import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
Hashtable<String, String> contacts = new Hashtable<>();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of contacts: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
for (int i = 0; i < n; i++) {
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.print("Enter mobile number: ");
String mobile = scanner.nextLine();
contacts.put(mobile, name);
}
System.out.println("\nContact List:");
for (String mobile : contacts.keySet()) {
System.out.println("Mobile: " + mobile + ", Name: " + contacts.get(mobile));
}
scanner.close();
}
}
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 java.util.Scanner;
import java.util.TreeSet;
public class SortedIntegers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TreeSet<Integer> numbers = new TreeSet<>();
System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();
System.out.println("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
int number = scanner.nextInt();
numbers.add(number); // TreeSet ensures no duplicates and sorts automatically
}
System.out.println("Sorted integers without duplicates: " + numbers);
scanner.close();
}
}
b) Write a program to sort HashMap by keys and display the details before sorting and after
sorting.
import java.util.*;
public class SortHashMapByKey {
public static void main(String[] args) {
// Creating a HashMap
HashMap<Integer, String> map = new HashMap<>();
map.put(3, "Alice");
map.put(1, "Charlie");
map.put(2, "Bob");
// Displaying the HashMap before sorting
System.out.println("HashMap before sorting: " + map);
// Sorting the HashMap by keys using a TreeMap
TreeMap<Integer, String> sortedMap = new TreeMap<>(map);
// Displaying the sorted HashMap
System.out.println("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 java.io.*;
import java.util.Hashtable;
import java.util.Scanner;
public class PhoneBook {
public static void main(String[] args) {
Hashtable<String, String> phoneBook = new Hashtable<>();
Scanner scanner = new Scanner(System.in);
// Load data from a text file
try (BufferedReader br = new BufferedReader(new FileReader("phonebook.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split("\t"); // Split by tab character
if (parts.length == 2) {
phoneBook.put(parts[0], parts[1]); // Name as key, phone as value
phoneBook.put(parts[1], parts[0]); // Phone as key, name as value
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
return;
}
// Accept input for searching
System.out.print("Enter a name or phone number to search: ");
String query = scanner.nextLine();
// Search and display the corresponding value
if (phoneBook.containsKey(query)) {
System.out.println("Result: " + phoneBook.get(query));
} else {
System.out.println("No match found!");
}
scanner.close();
}
}
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 java.util.*;
class linkedlist2
{
public static void main(String args[])
{
LinkedList<Integer> al=new LinkedList<Integer>();
int n,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no of integers ");
n=sc.nextInt();
System.out.println("Enter the integers you want");
for(i=0;i<n;i++)
{
int c=sc.nextInt();
al.add(c);
System.out.println(al);
al.add(100);
System.out.println(al);
al.addFirst(90);
System.out.println("Adding element at first position list is"+al);
al.removeLast();
System.out.println("Deleting the last element"+al);
System.out.println("Size of the element is"+al.size());
}
}