Milestone - 2: Collection
Milestone - 2: Collection
Milestone - 2: Collection
ROLL NO-96
IT- (B)
COLLECTION
List Assignment
1. Write a java program to create an Arraylist , add all months of the
year and print the same.
SOLUTION
package Milestone2;
import java.util.*;
public class printmonths {
}
}
2. Create an application for employee management having
following classes:
SOLUTION
Employee Class
package Assignment2;
public Employee() {}
EmployeeDB Class
package Assignment2;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Iterator<Employee> it = employeeDb.iterator();
while (it.hasNext()) {
Employee emp = it.next();
if (emp.getEmpId() == empId) {
isRemoved = true;
it.remove();
}
}
return isRemoved;
}
return paySlip;
}
public Employee[] listAll() {
Employee[] empArray = new Employee[employeeDb.size()];
for (int i = 0; i < employeeDb.size(); i++)
empArray[i] = employeeDb.get(i);
return empArray;
}}
Main Test
package Assignment2;
import Assignment2.*;
public class MainTest {
empDb.addEmployee(emp1);
empDb.addEmployee(emp2);
empDb.addEmployee(emp3);
empDb.addEmployee(emp4);
System.out.println();
empDb.deleteEmployee(102);
System.out.println();
System.out.println(empDb.showPaySlip(103));
}
}
3.Create an ArrayList which will be able to store only Strings.
Create a printAll method which will print all the elements using
an Iterator.
SOLUTION
import
java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Assignment3 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.add("Item 4");
printAll(list);
}
public static void printAll(List<String> list)
{
Iterator<String> it = list.iterator();
while (it.hasNext())
System.out.println(it.next());
}
}
SOLUTION
package Milestone2;
import java.util.ArrayList;
import java.util.List;
class MyArrayList<E> extends ArrayList<E> {
public boolean add(E e) {
if (e instanceof Integer || e instanceof Float || e
instanceof Double) {
super.add(e);
return true;
} else {
throw new ClassCastException("Only Integer, Float,
and Double are supported.");
}
}
}
public class Assignment4 {
public static void main(String[] args) {
List<Object> list = new MyArrayList<>();
try {
list.add(15);
list.add(1.2F);
list.add(3.1415D);
list.add("Test");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(list);
}
}
SOLUTION
package Milestone2;
import java.util.LinkedList;
public class Assignment5 {
SOLUTION
package Milestone2;
import java.util.*;
public class Assignment6 {
SOLUTION
package Milestone2;
import java.util.Iterator;
import java.util.Vector;
class Employee {
private int id;
private String name;
private String address;
private Double salary;
Iterator<Employee> it = list.iterator();
while (it.hasNext())
System.out.println(it.next());
}
}
SET ASSIGNMENT
1.Develop a java class with a instance variable Country HashSet
(H1) add a method saveCountryNames(String CountryName) ,
the method should add the passed country to a HashSet (H1)
and return the added HashSet(H1). Develop a method
getCountry(String CountryName) which iterates through the
HashSet and returns the country if exist else return null. NOTE:
You can test the methods using a main method
SOLUTION
Country Class
package SET;
import java.util.HashSet;
import java.util.Iterator;
while (it.hasNext()) {
if (it.next().equals(CountryName))
return CountryName;
}
return null;
}
}
Main Class
package SET;
public class Assignment1 {
public static void main(String[] args) {
Country countries = new Country();
countries.saveCountryNames("India");
countries.saveCountryNames("USA");
countries.saveCountryNames("Pakistan");
countries.saveCountryNames("Bangladesh");
countries.saveCountryNames("China");
System.out.println("China: " +
countries.getCountry("China"));
System.out.println("Japan: " +
countries.getCountry("Japan"));
}
}
2. Write a program to store a group of employee names into a
HashSet, retrieve the elements one by one using an Iterator.
SOLUTION
package SET;
import java.util.HashSet;
import java.util.Iterator;
set.add("Bob");
set.add("Alice");
set.add("John");
set.add("Richard");
Iterator<String> it = set.iterator();
while (it.hasNext())
System.out.println(it.next());
}
}
3.Create Collection called TreeSet which is capable of storing
String objects. The Collection should have the following
capabilities a)Reverse the elements of the Collection b)Iterate
the elements of the TreeSet c) Checked if a particular element
exists or not.
SOLUTION
package SET;
import java.util.Iterator;
import java.util.TreeSet;
public class Assignment3 {
Iterator<String> it = set.iterator();
String query = "John";
boolean result = false;
while (it.hasNext()) {
if (it.next().equals(query)) {
result = true;
break;
}
}
if (result) System.out.println(query + " exists");
else System.out.println(query + " doesn't exist");
}
}
SOLUTION
Country Class
import java.util.Iterator;
import java.util.TreeSet;
public class Country1 {
TreeSet<String> H1 = new TreeSet<>();
public TreeSet<String> saveCountryNames(String CountryName) {
H1.add(CountryName);
return H1;
}
return null;
}
}
Main Class
public class Assignment4 {
System.out.println("China: " +
countries.getCountry("China"));
System.out.println("Japan: " +
countries.getCountry("Japan"));
}
}
MAP ASSIGNMENT
1.
1. Develop a java class with a instance variable CountryMap HashMap (M1) add
a method saveCountryCapital(String CountryName, String capital) , the
method should add the passed country and capital as key/value in the map
M1 and return the Map (M1). Key- Country Value - Capital India Delhi Japan
Tokyo
4. Develop a method which iterates through the map M1 and creates another
map M2 with Capital as the key and value as Country and returns the Map M2.
Key – Capital Value – Country Delhi India Tokyo Japan
SOLUTION
COUNTRYMAP CLASS
package MAP;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public CountryMap() {
M1 = new HashMap<String, String>();
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
if (me.getValue().equals(capitalName))
return me.getKey();
}
return null;
}
return M2;
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
list.add(me.getKey());
}
return list;
}
}
MAIN CLASS
import java.util.HashMap;
countryMap.saveCountryCapital("India", "Delhi");
countryMap.saveCountryCapital("Japan", "Tokyo");
countryMap.saveCountryCapital("USA", "Washington, D.C.");
System.out.println(countryMap.getCapital("India"));
System.out.println(countryMap.getCountry("Tokyo"));
System.out.println(countryMap.toArrayList());
}
2.Create a Collection called HashMap which is capable of
storing String objects. The program should have the following
abilities a) Check if a particular key exists or not b) Check if a
particular value exists or not c) Use Iterator to loop through the
map key set.
SOLUTION
package MAP;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Assignment2 {
map.put("India", "Delhi");
map.put("Japan", "Tokyo");
map.put("Bangladesh", "Dhaka");
Set<Entry<String, String>> set = map.entrySet();
Iterator<Entry<String, String>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
if (me.getKey().equals("Japan")) {
System.out.println("Key Japan exists");
break;
}
}
set = map.entrySet();
it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
if (me.getValue().equals("Delhi")) {
System.out.println("Value Delhi exists");
break;
}
}
set = map.entrySet();
it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
System.out.println(me);
}
}
}
3.Write a program that will have a Properties class which is
capable of storing some States of India and their Capital. Use
an Iterator to list all the elements of the Properties.
SOLUTION
package MAP;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
while (it.hasNext()) {
Entry<Object, Object> me = it.next();
System.out.println(me);
}
}
}
SOLUTION
ContactList Class
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
while (it.hasNext()) {
Map.Entry<String, Integer> me = it.next();
if (me.getKey().equals(name)) {
return true;
}
}
return false;
}
while (it.hasNext()) {
Map.Entry<String, Integer> me = it.next();
if (me.getValue().intValue() == number) {
return true;
}
}
return false;
}
while (it.hasNext()) {
Map.Entry<String, Integer> me = it.next();
System.out.println(me);
}
}
MAIN CLASS
System.out.println("Police: " +
contactsList.doesContactNameExist("Police"));
System.out.println("98765432: " +
contactsList.doesContactNumberExist(98765432));
System.out.println();
contactsList.listAllContacts();
}
}
5.Implement the assignment 1 using TreeMap.
SOLUTION
CountryMap CLASS
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public CountryMap1() {
M1 = new TreeMap<String, String>();
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
if (me.getValue().equals(capitalName))
return me.getKey();
}
return null;
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
M2.put(me.getValue(), me.getKey());
}
return M2;
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
list.add(me.getKey());
}
return list;
}
MAIN CLASS
import java.util.HashMap;
public class Assignment5 {
countryMap.saveCountryCapital("India", "Delhi");
countryMap.saveCountryCapital("Japan", "Tokyo");
countryMap.saveCountryCapital("USA", "Washington, D.C.");
System.out.println(countryMap.getCapital("India"));
System.out.println(countryMap.getCountry("Tokyo"));
System.out.println(countryMap.toArrayList());
}
6.Implement the assignment 1 using HashTable.
SOLUTION
CountryMap CLASS
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public CountryMap() {
M1 = new Hashtable<String, String>();
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
if (me.getValue().equals(capitalName))
return me.getKey();
}
return null;
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
M2.put(me.getValue(), me.getKey());
}
return M2;
}
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
list.add(me.getKey());
}
return list;
}
MAIN CLASS
import java.util.HashMap;
public class Assignment6 {
countryMap.saveCountryCapital("India", "Delhi");
countryMap.saveCountryCapital("Japan", "Tokyo");
countryMap.saveCountryCapital("USA", "Washington, D.C.");
System.out.println(countryMap.getCapital("India"));
System.out.println(countryMap.getCountry("Tokyo"));
System.out.println(countryMap.toArrayList());
}
}
MULTITHREADING
THREAD CREATION ASSIGNMENT
1.Create two threads and assign names ‘Scooby’ and ‘Shaggy’
to the two threads. Display both thread names.
SOLUTION
SOLUTION
import java.util.Random;
public class Assignment2 implements Runnable {
SOLUTION
package Milestone2.ThreadControl;
}
public void run() {
for (int i = 1; i <= 10; i++) {
if (i == 6)
try {
t1.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i);
}
}
SOLUTION
public class Assignment2 implements Runnable {
static Thread oddThread;
static Thread evenThread;
if
(Thread.currentThread().getName().equals("OddThread")) {
if (i % 2 == 1) System.out.println(i);
}
}
}
}
3.Create three threads- with different priorities – MAX, MIN,
NORM- and start the threads at the same time. Observe the
completion of the threads.
SOLUTION
public class Assignment3 implements Runnable {
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
}