0% found this document useful (0 votes)
9 views12 pages

collectionp

The document contains multiple Java assignments completed by a student named Borkar Pradnya Ramesh. Each assignment involves creating Java programs that utilize different data structures such as ArrayList, LinkedList, TreeSet, and HashMap to manage and display collections of data. The assignments cover tasks like accepting user input, sorting data, and managing contacts with a simple user interface.

Uploaded by

priyankawalunj52
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)
9 views12 pages

collectionp

The document contains multiple Java assignments completed by a student named Borkar Pradnya Ramesh. Each assignment involves creating Java programs that utilize different data structures such as ArrayList, LinkedList, TreeSet, and HashMap to manage and display collections of data. The assignments cover tasks like accepting user input, sorting data, and managing contacts with a simple user interface.

Uploaded by

priyankawalunj52
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/ 12

/**********************************************************************************

Student Name:Borkar Pradnya Ramesh Roll No:8428


Class:T.Y.BSC(com.sci) Batch:B
Asiignment No:1(A1)
Assignment Name: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.
**********************************************************************************/
//Cities.java

import java.util.ArrayList;
import java.util.Scanner;

public class Cities


{
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();
for (int i = 0; i < n; i++)
{
System.out.print("Enter city " + (i + 1) + ": ");
String city = scanner.nextLine();
cityList.add(city);
}
System.out.println("\nCity List:");
for (String city : cityList) {
System.out.println(city);
}
cityList.clear();
System.out.println("\nArrayList after clearing:");
if (cityList.isEmpty())
{
System.out.println("ArrayList is empty");
} else
{
System.out.println("ArrayList is not empty");
}
}
}
/*********************************OUTPUT*****************************************
[root@192 8428]# javac Cities.java
[root@192 8428]# java Cities
Enter the number of cities: 3
Enter city 1: Pune
Enter city 2: Shirur
Enter city 3: Latur
City List:
Pune
Shirur
Latur

ArrayList after clearing:


ArrayList is empty

**********************************************************************************/
/**********************************************************************************
Student Name:Borkar Pradnya Ramesh Roll No:8428
Class:T.Y.BSC(com.sci) Batch:B
Asiignment No:1(A2)
Assignment Name:write a java program to read ‘n’names of your friends ,store it into linked list,also
display contents of the same.
**********************************************************************************/
//Friends.java

import java.util.LinkedList;
import java.util.Scanner;
public class Friends
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
LinkedList<String> friendsList = new LinkedList<>();

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


int n = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
// Read 'n' names of friends and store in LinkedList
for (int i = 0; i < n; i++)
{
System.out.print("Enter friend " + (i + 1) + "'s name: ");
String friendName = scanner.nextLine();
friendsList.add(friendName);
}
// Display contents of LinkedList
System.out.println("\nFriends List:");
for (String friendName : friendsList)
{
System.out.println(friendName);
}
// Display contents of LinkedList using get() method
System.out.println("\nFriends List using get() method:");
for (int i = 0; i < friendsList.size(); i++)
{
System.out.println("Friend " + (i + 1) + ": " + friendsList.get(i));
}
}
}

/**************************************OUTPUT************************************
[root@192 8428]# vim Friends.java
[root@192 8428]# javac Friends.java
[root@192 8428]# java Friends
Enter the number of friends: 4
Enter friend 1's name: Payal
Enter friend 2's name: Hindavi
Enter friend 3's name: Tanvi
Enter friend 4's name: Gayatri

Friends List:
Payal
Hindavi
Tanvi
Gayatri

Friends List using get() method:


Friend 1: Payal
Friend 2: Hindavi
Friend 3: Tanvi
Friend 4: Gayatri

**********************************************************************************/
/**********************************************************************************
Student Name:Borkar Pradnya Ramesh Roll No:8428
Class:T.Y.BSC(com.sci) Batch:B
Asiignment No:1(A3)
Assignment Name:write a program to create a new tree set,add some colors(string)and print out the tree
set.
**********************************************************************************/
//Treset.java

import java.util.TreeSet;

public class Treset


{
public static void main(String[] args)
{

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

colorSet.add("White");
colorSet.add("Green");
colorSet.add("Orange");
colorSet.add("Black");
colorSet.add("Grey");
colorSet.add("Blue");

System.out.println("TreeSet of Colors:");
System.out.println(colorSet);
}
}
/***********************************OUTPUT***************************************
Output:-
[root@192 8428]# java Treset
TreeSet of Colors:
[Black, Blue, Green, Grey, Orange, White]
**********************************************************************************/
/**********************************************************************************
Student Name:Borkar Pradnya Ramesh Roll No:8428
Class:T.Y.BSC(com.sci) Batch:B
Asiignment No:1(A4)
Assignment Name:Create the hash table that will maintain the mobile number and student name.
Display the contact list.
**********************************************************************************/
//HashTable.java

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

public class HashTable


{
private Map<String, String> contacts;
public HashTable() {
this.contacts = new HashMap<>();
}
public void addContact(String mobileNumber, String studentName) {
this.contacts.put(mobileNumber, studentName);
System.out.println("Contact added: " + mobileNumber + " - " + studentName);
}
public void displayContacts() {
System.out.println("\nContacts:");
for (Map.Entry<String, String> entry : this.contacts.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}
public void searchContact(String mobileNumber) {
if (this.contacts.containsKey(mobileNumber)) {
System.out.println("Contact found: " + mobileNumber + " - " +
this.contacts.get(mobileNumber));
} else {
System.out.println("Contact not found: " + mobileNumber);
}
}
public static void main(String[] args) {
HashTable contactBook = new HashTable();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n1. Add contact");
System.out.println("2. Display contacts");
System.out.println("3. Search contact");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
switch (choice) {
case 1:
System.out.print("Enter mobile number: ");
String mobileNumber = scanner.nextLine();
System.out.print("Enter student name: ");
String studentName = scanner.nextLine();
contactBook.addContact(mobileNumber, studentName);
break;
case 2:
contactBook.displayContacts();
break;
case 3:
System.out.print("Enter mobile number to search: ");
mobileNumber = scanner.nextLine();
contactBook.searchContact(mobileNumber);
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}

/*****************************************OUTPUT*********************************
[root@192 8428]# java HashTable

1. Add contact
2. Display contacts
3. Search contact
4. Exit
Enter your choice: 1
Enter mobile number: 8010775431
Enter student name: Payal
Contact added: 8010775431 - Payal

1. Add contact
2. Display contacts
3. Search contact
4. Exit
Enter your choice: 1
Enter mobile number: 3465785510
Enter student name: Karan
Contact added: 3465785510 - Karan

1. Add contact
2. Display contacts
3. Search contact
4. Exit
Enter your choice: 1
Enter mobile number: 5466752907
Enter student name: Hindavi
Contact added: 5466752907 - Hindavi

1. Add contact
2. Display contacts
3. Search contact
4. Exit
Enter your choice: 1
Enter mobile number: 9985463674
Enter student name: Pradnya
Contact added: 9985463674 - Pradnya

1. Add contact
2. Display contacts
3. Search contact
4. Exit
Enter your choice: 2

Contacts:
8010775431 - Payal
9985463674 - Pradnya
3465785510 - Karan
5466752907 - Hindavi

1. Add contact
2. Display contacts
3. Search contact
4. Exit
Enter your choice: 3
Enter mobile number to search: 9985463674
Contact found: 9985463674 - Pradnya

1. Add contact
2. Display contacts
3. Search contact
4. Exit
Enter your choice: 4
[root@192 8428]#
**********************************************************************************/
/**********************************************************************************
Student Name:Borkar Pradnya Ramesh Roll No:8428
Class:T.Y.BSC(com.sci) Batch:B
Assignment No:1(B1)
Assignment Name:Accept ‘n’ integers from the user. Store and display integers in sorted order having
proper collection class. The collection should not accept duplicate elements.
**********************************************************************************/
//SortInt.java

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

public class SortInt


{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
TreeSet<Integer> uniqueSortedIntegers = 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 integer = scanner.nextInt();
uniqueSortedIntegers.add(integer);
}
System.out.println("\nSorted unique integers:");
for (Integer integer : uniqueSortedIntegers) {
System.out.println(integer);
}
}
}
/***********************************OUTPUT***************************************
[root@192 8428]# javac SortInt.java
[root@192 8428]# java SortInt
Enter the number of integers: 5
Enter 5 integers:
22
3
66
78
10

Sorted unique integers:


3
10
22
66
78
**********************************************************************************/
/**********************************************************************************
Student Name:Borkar Pradnya Ramesh Roll No:8428
Class:T.Y.BSC(com.sci) Batch:B
Asiignment No:1(B2)
Assignment Name:Write a program to sort HashMap by keys and display the details before sorting and
after sorting.
**********************************************************************************/
//Hash_Map.java

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Hash_Map


{
public static void main(String[] args)
{
Map<String, String> unsortedMap = new HashMap<>();

unsortedMap.put("Z", "Zebra");
unsortedMap.put("A", "Apple");
unsortedMap.put("K", "Kite");
unsortedMap.put("D", "Dog");
unsortedMap.put("E", "Elephant");

System.out.println("Unsorted HashMap:");
for (Map.Entry<String, String> entry : unsortedMap.entrySet())
{
System.out.println(entry.getKey() + ": " + entry.getValue());
}

Map<String, String> sortedMap = new TreeMap<>(unsortedMap);


System.out.println("\nSorted HashMap:");
for (Map.Entry<String, String> entry : sortedMap.entrySet())
{
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
/************************************OUTPUT**************************************
[root@192 8428]# javac Hash_Map.java
[root@192 8428]# java Hash_Map
Unsorted HashMap:
A: Apple
D: Dog
E: Elephant
Z: Zebra
K: Kite
Sorted HashMap:
A: Apple
D: Dog
E: Elephant
K: Kite
Z: Zebra
[root@192 8428]#
**********************************************************************************/
/**********************************************************************************
Student Name:Borkar Pradnya Ramesh Roll No:8428
Class:T.Y.BSC(com.sci) Batch:B
Asiignment No:1(B3)
Assignment Name: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)
**********************************************************************************/
//PhoneNo.java

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class Hash_Map
{
public static void main(String[] args)
{
Map<String, String> unsortedMap = new HashMap<>();
unsortedMap.put("Z", "Zebra");
unsortedMap.put("A", "Apple");
unsortedMap.put("K", "Kite");
unsortedMap.put("D", "Dog");
unsortedMap.put("E", "Elephant");
System.out.println("Unsorted HashMap:");
for (Map.Entry<String, String> entry : unsortedMap.entrySet())
{
System.out.println(entry.getKey() + ": " + entry.getValue());
}
Map<String, String> sortedMap = new TreeMap<>(unsortedMap);
System.out.println("\nSorted HashMap:");
for (Map.Entry<String, String> entry : sortedMap.entrySet())
{
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
//phone.txt
Payal:8010112345
Gayatri:478890435
Ram:456732896
/*******************************************OUTPUT*******************************
[root@192 8428]# javac PhoneNo.java
[root@192 8428]# java PhoneNo
Enter Name :
Payal
8010112345
**********************************************************************************/

You might also like