ChatGpt java
ChatGpt java
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;
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;
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;
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;
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;
scanner.close();
}
}
b) Write a program to sort HashMap by keys and display the details before sorting and after
sorting.
import java.util.*;
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;
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());
}
}