0% found this document useful (0 votes)
26 views16 pages

Aditya Bankar Assignment No 1

The document contains multiple Java programming assignments that involve using various collection classes such as ArrayList, LinkedList, TreeSet, and Hashtable. Each assignment includes a program to perform specific tasks like storing city names, friends' names, colors, and contact information, as well as sorting integers and HashMaps. Additionally, it features a program to load data from a text file into a Hashtable for easy lookup of names and phone numbers.

Uploaded by

sonyabhongale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views16 pages

Aditya Bankar Assignment No 1

The document contains multiple Java programming assignments that involve using various collection classes such as ArrayList, LinkedList, TreeSet, and Hashtable. Each assignment includes a program to perform specific tasks like storing city names, friends' names, colors, and contact information, as well as sorting integers and HashMaps. Additionally, it features a program to load data from a text file into a Hashtable for easy lookup of names and phone numbers.

Uploaded by

sonyabhongale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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 java.util.ArrayList; import

java.util.Scanner;

public class CityList { public static void

main(String[] args){

// Createa

Scanner object to take input

Scanner scanner = new Scanner(System.in);

// Ask for the number of cities

System.out.print("Enter the number of cities:");scanner.nextint();

character left by nextlnt()

int n=

scanner.nextLine(); // Consume the newline

// Create an ArrayList to store city names

ArrayList<String> cities = new ArrayList<>();

// Input the names of the cities

System.out.println("Enter the names of"+n +"cities:");for (int i=0;i<n; i++){

O) Scanned with OKEN Scanner

String city = scanner.nextLine();

cities.add(city);

// Display the contents of the ArrayList System.out.println("\nCities in the list:");

(String city : cities){


System.out.printin(city);

// Remove all elements from the ArrayList

cities.clear();

/I Display the ArrayList after removal

System.out.println("\nAfter removing all elements, the list is now empty:"+cities);

// Close the scanner scanner.close();

OUTPUT:-

C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> javac City.java

C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> java City.java

Enter the number of cities: 3

Enter the names of 3 cities:

Baramati

Phaltan

Pune

Cities in the list:

Baramati

Phaltan

Pune

After removing all elements, the list is now empty: []

PS C:\Users\Aditya Bankar\OneDrive\Documents\Assignemnt1>
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 java.util.LinkedList; import

java.util.Scanner;

public class FriendsList { public static void

main(Stringl] args){ // Create a

Scanner object to take input

Scanner scanner = new Scanner(System.in);

// Ask for the number of friends

int n=scanner.nextLine(); // Consume the newline

System.out.print("Enter the number of friends: ");scanner.nextlnt();

Ol Scanned with OKEN Scanner

character left by nextlnt() // Create a LinkedList to store the names of

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

// Input the names of friends

System.out.printin("Enter the names of "+n +"friends:");

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

String friend = scanner.nextLine();

friends.add(friend);

// Display the contents of the LinkedList System.out.println("\nFriends in the list:"); for

(String friend :friends) {

System.out.println(friend);

}
// Close the scanner scanner.close();

}
}

OUTPUT:-

C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> java friendlist.java

Enter the number of friends: 5

Enter the names of 5 friends:

Rohan

Aditya

Omkar

Suyash

Ritesh

friends in the list:

Rohan

Aditya

Omkar

Suyash

Ritesh
c) Write a program to create a new tree set, add some colors (string) andprint out the tree set.
Program:-

import java.util.TreeSet;

public class ColorTreeSet{ public static void

main(Stringl] args) {

to store color names

// Create a TreeSet

TreeSet<String> colors = new TreeSet<>();

// Add some color names to the

TreeSet colors.add("Indigo");

colors.add("Violet");

colors.add("Green");

colors.add("Yellow");

colors.add("Red");

(I Display the contents of the TreeSet

System.out.println("Colors in the TreeSet:"); for

(String color : colors){

System.out-printin(color);

}
C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> java ColorTreeset.java

Colors in the Tree:

Indigo

Violet

Green

Yellow

Red

C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1>

d) Create the hash table that will maintain the mobile number and

student name. Display the contact list. Program:-

import java.util.Hashtable; import

java.util.Scanner;

public class ContactList { public static

void main(Stringl] 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(System.in);

// Asking the user how many contacts they want to add

System.out.print("Enter the number of contacts you want to add:

"); int numContacts = scanner.nextint(); scanner.nextLine(); //

Consume the leftover newline

// Collect contact information from the user for (inti=0; i< numContacts; i++){

System.out,print("Enter mobile number for contact "+(i +1)+":");


String mobileNumber = scanner.nextLine();

System.out.print("Enter student name for contact "+(i +1)+":");

String studentName = scanner.nextLine();

// Add the contact information to the Hashtable

contactList.put(mobileNumber, studentName);

// Display the contact list (mobile number and student name)System.out.println("\nContact


List:"); for (var entry:contactList.entrySet()){

entry.getKey();

String mobileNumber =

String studentName = entry.getValue();

System.out.println("Mobile Number:"+ mobileNumber+" | Student Name:"+

studentName);

// Close the scanner scanner.close();

}
C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> java contactlist.java

Enter the number of contacts you want to add: 2

Enter mobile number for contact 1: 9882657833

Enter student name for contact 1: rohan

Enter mobile number for contact 2: 8976305764

Enter student name for contact 2: aditya

Contact List:

Mobile Number: 9882657833 | Student Name: rohan

Mobile Number: 8976305764 | Student Name: aditya


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 java.util.Scanner; import

java.util.TreeSet;

public class Sortedintegers {

public static void main(Stringl] args) {

// Create a Scanner object to take input from the user

Scanner scanner = new Scanner(System.in);

Int

// Ask for the number of integers to be entered System.out.print("Enter the number of


integers:");I1 =scanner.nextlnt();

// Create a TreeSet to store integers (it will automatically sort and remove duplicates)

TreeSet<Integer> numbers = new TreeSet<>();

// Accept 'n' integers from the user

System.out.println("Enter "+ n +"integers:"); for (int i= 0; i< n; i++){int number =


scanner.nextint();

TreeSet (duplicates will be ignored)

numbers.add(number);

// Add to the Treeset(Duplicates will be ignored)

// Display the integers in sorted order (TreeSet does this

automatically)

duplicates):");
System.out.println("\nSorted integers (without

for (int num :numbers){

System.out.println(num);

// Close the scanner scanner.close();

}
Output:-

C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> java sortedint.java

Enter number of integers: 10

Sorted Integers (Without Duplicates)

9
b)Write a java program to sort Hashmap by keys and display the details before sorting and
after sorting. Program:-

import java.util.*;

public class SortHashMapByKey {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

HashMap<String, Integer> map = new HashMap<>();

System.out.println("Enter the number of entries you want to add:");

int n = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

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

System.out.println("Enter key " + (i + 1) + ":");

String key = scanner.nextLine();

System.out.println("Enter value for key " + (i + 1) + ":");

int value = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

map.put(key, value);

// Display the HashMap before sorting

System.out.println("\nHashMap before sorting:");

displayMap(map);
// Create a TreeMap from the HashMap to sort it by keys

TreeMap<String, Integer> sorted = new TreeMap<>(map);

// Display the sorted HashMap

System.out.println("\nHashMap after sorting by keys:");

displayMap(sorted);

scanner.close();

public static void displayMap(Map<String, Integer> map) {

for (Map.Entry<String, Integer> entry : map.entrySet()) {

System.out.println("Key: " + entry.getKey() + " - Value: " + entry.getValue());

OUTPUT:-
C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> java sorthash.java
ENTER THE NUMBER OF ENTRIES YOU WANT TO ADD:
4
ENTER KEY 1:
BANANA
ENTER VALUE FOR KEY 1:
1
ENTER KEY 2:
APPLE
ENTER VALUE FOR KEY 2:
2
ENTER KEY 3:
ORANGE
ENTER VALUE FOR KEY 3:
3
ENTER KEY 4:
MANGO
ENTER VALUE FOR KEY 4:
4

HASHMAP BEFORE SORTING:


KEY: BANANA - VALUE: 1
KEY: APPLE - VALUE: 2
KEY: ORANGE - VALUE: 3
KEY: MANGO - VALUE: 4

HASHMAP AFTER SORTING BY KEYS:


KEY: APPLE - VALUE: 2
KEY: BANANA - VALUE: 1
KEY: MANGO - VALUE: 4
KEY: ORANGE - VALUE: 3
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 java.io.*;

import java.util.*;

public class PhoneBook {

public static void main(String[] args) {

Hashtable<String, String> phoneBook = new Hashtable<>();

String fileName = "phonebook.txt";

// Load names and phone numbers from the file

try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

String line;

while ((line = br.readLine()) != null) {

String[] parts = line.split("\t");

if (parts.length == 2) {

phoneBook.put(parts[0], parts[1]);

phoneBook.put(parts[1], parts[0]); // For reverse lookup

} catch (IOException e) {

e.printStackTrace();

}
Scanner scanner = new Scanner(System.in);

System.out.println("Enter a name or phone number to search:");

String input = scanner.nextLine();

String result = phoneBook.get(input);

if (result != null) {

System.out.println("Corresponding value: " + result);

} else {

System.out.println("No matching entry found.");

scanner.close();

OUTPUT:-

C:\Users\Aditya Bankar\OneDrive\Documents\Assignment1> java phonenu.java

Enter phone number or name: john doe

Record found for the input: 123-456-789

Enter phone number or name: bob brown

No records found for input: bob brown

You might also like