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.
Program:-
import [Link];
import [Link];
public class CityList {
public static void main(String[] args) {
// Create a Scanner object to take input
Scanner scanner = new Scanner([Link]);
// Ask for the number of cities
[Link]("Enter the number of cities: ");
int n = [Link]();
[Link](); // Consume the newline character left by nextInt()
// Create an ArrayList to store city names
ArrayList<String> cities = new ArrayList<>();
// Input the names of the cities
[Link]("Enter the names of " + n + " cities:");
for (int i = 0; i < n; i++) {
String city = [Link]();
[Link](city);
}
// Display the contents of the ArrayList
[Link]("\nCities in the list:");
for (String city : cities) {
[Link](city);
}
// Remove all elements from the ArrayList
[Link]();
// Display the ArrayList after removal
[Link]("\nAfter removing all elements, the list is now empty: " +
cities);
// Close the scanner
[Link]();
}
}
Output:-
b) Write a java program to read 'n' names of your friends, store it into
linked list, also display contents of the same.
Program:-
import [Link];
import [Link];
public class FriendsList {
public static void main(String[] args) {
// Create a Scanner object to take input
Scanner scanner = new Scanner([Link]);
// Ask for the number of friends
[Link]("Enter the number of friends: ");
int n = [Link]();
[Link](); // Consume the newline character left by nextInt()
// Create a LinkedList to store the names of friends
LinkedList<String> friends = new LinkedList<>();
// Input the names of friends
[Link]("Enter the names of " + n + " friends:");
for (int i = 0; i < n; i++) {
String friend = [Link]();
[Link](friend);
}
// Display the contents of the LinkedList
[Link]("\nFriends in the list:");
for (String friend : friends) {
[Link](friend);
}
// Close the scanner
[Link]();
}
}
Output:-
c) Write a program to create a new tree set, add some colors (string) and
print out the tree set.
Program:-
import [Link];
public class ColorTreeSet {
public static void main(String[] args) {
// Create a TreeSet to store color names
TreeSet<String> colors = new TreeSet<>();
// Add some color names to the TreeSet
[Link]("Red");
[Link]("Green");
[Link]("Blue");
[Link]("Yellow");
[Link]("Purple");
[Link]("Orange");
// Display the contents of the TreeSet
[Link]("Colors in the TreeSet:");
for (String color : colors) {
[Link](color);
}
}
}
Output:-
d) Create the hash table that will maintain the mobile number and
student name. Display the contact list.
Program:-
import [Link];
import [Link];
public class ContactList {
public static void main(String[] args) {
// Create a Hashtable to store mobile numbers and student names
Hashtable<String, String> contactList = new Hashtable<>();
// Create a Scanner object for input
Scanner scanner = new Scanner([Link]);
// Asking the user how many contacts they want to add
[Link]("Enter the number of contacts you want to add: ");
int numContacts = [Link]();
[Link](); // Consume the leftover newline
// Collect contact information from the user
for (int i = 0; i < numContacts; i++) {
[Link]("Enter mobile number for contact " + (i + 1) + ": ");
String mobileNumber = [Link]();
[Link]("Enter student name for contact " + (i + 1) + ": ");
String studentName = [Link]();
// Add the contact information to the Hashtable
[Link](mobileNumber, studentName);
}
// Display the contact list (mobile number and student name)
[Link]("\nContact List:");
for (var entry : [Link]()) {
String mobileNumber = [Link]();
String studentName = [Link]();
[Link]("Mobile Number: " + mobileNumber + " | Student Name: " +
studentName);
}
// Close the scanner
[Link]();
}
}
Output:-
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.
Program:-
import [Link];
import [Link];
public class SortedIntegers {
public static void main(String[] args) {
// Create a Scanner object to take input from the user
Scanner scanner = new Scanner([Link]);
// Ask for the number of integers to be entered
[Link]("Enter the number of integers: ");
int n = [Link]();
// Create a TreeSet to store integers (it will automatically sort and remove
duplicates)
TreeSet<Integer> numbers = new TreeSet<>();
// Accept 'n' integers from the user
[Link]("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
int number = [Link]();
[Link](number); // Add to the TreeSet (duplicates will be ignored)
}
// Display the integers in sorted order (TreeSet does this automatically)
[Link]("\nSorted integers (without duplicates):");
for (int num : numbers) {
[Link](num);
}
// Close the scanner
[Link]();
}
}
Output:-
b) Write a program to sort HashMap by keys and display the details
before sorting and after sorting.
Program:-
import [Link];
import [Link];
import [Link];
import [Link];
public class SortHashMapByKeys {
public static void main(String[] args) {
// Create a HashMap to store student IDs and their names
HashMap<Integer, String> studentGrades = new HashMap<>();
// Create a Scanner object to read input
Scanner scanner = new Scanner([Link]);
// Ask the user how many student entries they want to add
[Link]("Enter the number of students: ");
int numStudents = [Link]();
[Link](); // Consume the newline character left by nextInt
// Collect student data from the user
for (int i = 0; i < numStudents; i++) {
[Link]("Enter student ID (integer): ");
int studentID = [Link]();
[Link](); // Consume the newline character
[Link]("Enter student name: ");
String studentName = [Link]();
// Add the student ID and name to the HashMap
[Link](studentID, studentName);
}
// Display the HashMap before sorting
[Link]("\nHashMap before sorting by keys:");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]("Key: " + [Link]() + ", Value: " + [Link]());
}
// Sort the HashMap by keys (student IDs) using TreeMap
TreeMap<Integer, String> sortedStudentGrades = new
TreeMap<>(studentGrades);
// Display the HashMap after sorting by keys
[Link]("\nHashMap after sorting by keys:");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]("Key: " + [Link]() + ", Value: " + [Link]());
}
// Close the scanner
[Link]();
}
}
Output:-
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)
Program:-
import [Link].*;
import [Link];
import [Link];
public class PhoneBook {
public static void main(String[] args) {
// Initialize the Hashtable to store name-phone number pairs
Hashtable<String, String> nameToPhone = new Hashtable<>();
Hashtable<String, String> phoneToName = new Hashtable<>();
// File path where the data is stored
String filePath = "[Link]"; // Modify with your file path
// Load data from the text file into the hash tables
loadDataFromFile(filePath, nameToPhone, phoneToName);
// Create Scanner object for user input
Scanner scanner = new Scanner([Link]);
// Prompt the user to input a name or a phone number
[Link]("Enter a name or phone number: ");
String input = [Link]();
// Check if the input is a name or phone number
if ([Link](input)) {
// Input is a name, fetch the corresponding phone number
[Link]("Phone number: " + [Link](input));
} else if ([Link](input)) {
// Input is a phone number, fetch the corresponding name
[Link]("Name: " + [Link](input));
} else {
// Input is not found in the hash tables
[Link]("No record found for the input: " + input);
}
// Close the scanner
[Link]();
}
// Method to load data from the file into the hash tables
private static void loadDataFromFile(String filePath, Hashtable<String, String>
nameToPhone, Hashtable<String, String> phoneToName) {
try {
// Create BufferedReader to read the file
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
// Read each line from the file
while ((line = [Link]()) != null) {
// Split the line by tab character (\t)
String[] parts = [Link]("\t");
// Ensure we have exactly two parts (name and phone number)
if ([Link] == 2) {
String name = parts[0].trim();
String phoneNumber = parts[1].trim();
// Add the name-phone number pair to the hash tables
[Link](name, phoneNumber);
[Link](phoneNumber, name);
}
}
// Close the file reader
[Link]();
} catch (IOException e) {
[Link]("Error reading the file: " + [Link]());
}
}
}
Output:-