Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Milestone - 2: Collection

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

Milestone – 2

NAME :- SHIVAM SHARMA

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 {

public static void main(String[] args) {


ArrayList <String> list = new ArrayList<String>();
list.add("January");
list.add("February");
list.add("March");
list.add("April");
list.add("May");
list.add("June");
list.add("July");
list.add("August");
list.add("September");
list.add("October");
list.add("November");
list.add("December");
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}

}
}
2. Create an application for employee management having
following classes:

a) Create an Employee class with following attributes and


behaviors : i) EmpId Int ii) EmpName String iii) EmpEmail String
iv) EmpGender char v) EmpSalary float vi) GetEmployeeDetails()
-> prints employee details

b) Create one more class EmployeeDB which has the following


methods. i) boolean addEmployee(Employee e) ii) boolean
deleteEmployee(int empId) iii) String showPaySlip(int empId) iv)
Employee[] listAll()

SOLUTION
Employee Class

package Assignment2;

public class Employee {


private int EmpId;
private String EmpName;
private String EmpEmail;
private char EmpGender;
private float EmpSalary;

public Employee() {}

public Employee(int empId, String empName, String empEmail,


char empGender, float empSalary) {
super();
EmpId = empId;
EmpName = empName;
EmpEmail = empEmail;
EmpGender = empGender;
EmpSalary = empSalary;
}

public String GetEmployeeDetails() {


return "Employee [EmpId=" + EmpId + ", EmpName=" +
EmpName + ", EmpEmail=" + EmpEmail
+ ", EmpGender=" + EmpGender + ", EmpSalary="
+ EmpSalary + "]";
}

public int getEmpId() {


return EmpId;
}

public void setEmpId(int empId) {


EmpId = empId;
}

public String getEmpName() {


return EmpName;
}

public void setEmpName(String empName) {


EmpName = empName;
}

public String getEmpEmail() {


return EmpEmail;
}

public void setEmpEmail(String empEmail) {


EmpEmail = empEmail;
}

public char getEmpGender() {


return EmpGender;
}

public void setEmpGender(char empGender) {


EmpGender = empGender;
}

public float getEmpSalary() {


return EmpSalary;
}

public void setEmpSalary(float empSalary) {


EmpSalary = empSalary;
}

EmployeeDB Class
package Assignment2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class EmployeeDB {


List<Employee> employeeDb = new ArrayList<>();

public boolean addEmployee(Employee e) {


return employeeDb.add(e);
}

public boolean deleteEmployee(int empId) {


boolean isRemoved = false;

Iterator<Employee> it = employeeDb.iterator();

while (it.hasNext()) {
Employee emp = it.next();
if (emp.getEmpId() == empId) {
isRemoved = true;
it.remove();
}
}

return isRemoved;
}

public String showPaySlip(int empId) {


String paySlip = "Invalid employee id";

for (Employee e : employeeDb) {


if (e.getEmpId() == empId) {
paySlip = "Pay slip for employee id " + empId
+ " is " +
e.getEmpSalary();
}
}

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 {

public static void main(String[] args) {


EmployeeDB empDb = new EmployeeDB();

Employee emp1 = new Employee(101, "Bob",


"bob@w3epic.com", 'M', 25000);
Employee emp2 = new Employee(102, "Alice",
"alice@w3epic.com", 'F', 30000);
Employee emp3 = new Employee(103, "John",
"john@w3epic.com", 'M', 20000);
Employee emp4 = new Employee(104, "Ram",
"ram@w3epic.com", 'M', 50000);

empDb.addEmployee(emp1);
empDb.addEmployee(emp2);
empDb.addEmployee(emp3);
empDb.addEmployee(emp4);

for (Employee emp : empDb.listAll())


System.out.println(emp.GetEmployeeDetails());

System.out.println();
empDb.deleteEmployee(102);

for (Employee emp : empDb.listAll())


System.out.println(emp.GetEmployeeDetails());

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());
}
}

4. Create an ArrayList which will be able to store only numbers


like int,float,double,etc, but not any other data type.

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);
}
}

5. Implement the assignment 1 using Linked List.

SOLUTION
package Milestone2;
import java.util.LinkedList;
public class Assignment5 {

public static void main(String[] args) {


LinkedList<String> list = new LinkedList<String>();
list.add("January");
list.add("February");
list.add("March");
list.add("April");
list.add("May");
list.add("June");
list.add("July");
list.add("August");
list.add("September");
list.add("October");
list.add("November");
list.add("December");
System.out.println(list);
}
}

6. Implement the assignment 1 using Vector.

SOLUTION
package Milestone2;
import java.util.*;
public class Assignment6 {

public static void main(String[] args) {


Vector<String> vec = new Vector<String>();
vec.add("January");
vec.add("February");
vec.add("March");
vec.add("April");
vec.add("May");
vec.add("June");
vec.add("July");
vec.add("August");
vec.add("September");
vec.add("October");
vec.add("November");
vec.add("December");
System.out.println("Elements are: "+vec);
}
}
7. Write a program that will have a Vector which is capable of
storing emp objects. Use an Iterator and enumeration to list all
the elements of the Vector.

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;

public Employee(int id, String name, String address, Double


salary) {
super();
this.id = id;
this.name = name;
this.address = address;
this.salary = salary;
}

public int getId() {


return id;
}
public String toString() {
return "Employee [id=" + id + ", name=" + name + ",
address=" + address + ", salary=" + salary + "]";
}
}

public class Assignment7 {

public static void main(String[] args) {


Vector<Employee> list = new Vector<>();

list.add(new Employee(101, "Bob", "123 street, India",


20000.0));
list.add(new Employee(102, "Alice", "234 street, India",
30000.0));
list.add(new Employee(103, "John", "345 street, India",
25000.0));
list.add(new Employee(104, "Stuart", "456 street, India",
40000.0));

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;

public class Country {


HashSet<String> H1 = new HashSet<>();

public HashSet<String> saveCountryNames(String CountryName) {


H1.add(CountryName);
return H1;
}

public String getCountry(String CountryName) {


Iterator<String> it = H1.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;

public class Assignment2 {

public static void main(String[] args) {


HashSet<String> set = new HashSet<>();

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 {

public static void main(String[] args) {


TreeSet<String> set = new TreeSet<>();
set.add("Bob");
set.add("Alice");
set.add("John");
set.add("Richard");

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");
}
}

4. Implement the assignment 1 using TreeSet.

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;
}

public String getCountry(String CountryName) {


Iterator<String> it = H1.iterator();
while (it.hasNext()) {
if (it.next().equals(CountryName))
return CountryName;
}

return null;
}
}

Main Class
public class Assignment4 {

public static void main(String[] args) {


Country1 countries = new Country1();
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"));
}

}
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

2. Develop a method getCapital(String CountryName) which returns the capital


for the country passed from the Map M1 created in step 1.

3. Develop a method getCountry(String capitalName) which returns the country


for the capital name passed from the Map M1 created in step 1.

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

5. Develop a method which iterates through the map M1 and creates an


ArrayList with all the Country names stored as keys. This method should return
the ArrayList. NOTE: You can test the methods using a main method.

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 class CountryMap {


private HashMap<String, String> M1;

public CountryMap() {
M1 = new HashMap<String, String>();
}

public HashMap<String, String> saveCountryCapital(String


CountryName, String capital) {
M1.put(CountryName, capital);
return M1;
}

public String getCapital(String CountryName) {


return M1.get(CountryName);
}

public String getCountry(String capitalName) {


Set<Entry<String, String>> set = M1.entrySet();
Iterator<Entry<String, String>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, String> me = it.next();

if (me.getValue().equals(capitalName))
return me.getKey();
}

return null;
}

public HashMap<String, String> swapKyeValue() {


HashMap<String, String> M2 = new HashMap<String,
String>();

Set<Entry<String, String>> set = M1.entrySet();


Iterator<Entry<String, String>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
M2.put(me.getValue(), me.getKey());
}

return M2;
}

public ArrayList<String> toArrayList() {


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

Set<Entry<String, String>> set = M1.entrySet();


Iterator<Entry<String, String>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
list.add(me.getKey());
}

return list;
}
}

MAIN CLASS
import java.util.HashMap;

public class Assignment1 {

public static void main(String[] args) {


CountryMap countryMap = new CountryMap();

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());

HashMap<String, String> M2 = countryMap.swapKyeValue();


System.out.println(M2);
}

}
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 {

public static void main(String[] args) {


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

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;

public class Assignment3 {

public static void main(String[] args) {


Properties properties = new Properties();

properties.setProperty("West Bengal", "Kolkata");


properties.setProperty("Rajasthan", "Jodhpur");
properties.setProperty("Bihar", "Patna");

Set<Entry<Object, Object>> set = properties.entrySet();


Iterator<Entry<Object, Object>> it = set.iterator();

while (it.hasNext()) {
Entry<Object, Object> me = it.next();
System.out.println(me);
}
}
}

4. Create a Collection “ContactList” using HashMap to store


name and phone number of contacts added. The program
should use appropriate generics (String, Integer) and 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

ContactList Class
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class ContactList {


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

public void addContact(String name, Integer number) {


contacts.put(name, number);
}
public void removeContact(String name) {
contacts.remove(name);
}
public String toString() {
return "ContactList [contacts=" + contacts + "]";
}

public boolean doesContactNameExist(String name) {


Set<Entry<String, Integer>> set = contacts.entrySet();
Iterator<Entry<String, Integer>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, Integer> me = it.next();

if (me.getKey().equals(name)) {
return true;
}
}

return false;
}

public boolean doesContactNumberExist(Integer number) {


Set<Entry<String, Integer>> set = contacts.entrySet();
Iterator<Entry<String, Integer>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, Integer> me = it.next();
if (me.getValue().intValue() == number) {

return true;
}
}

return false;
}

public void listAllContacts() {


Set<Entry<String, Integer>> set = contacts.entrySet();
Iterator<Entry<String, Integer>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, Integer> me = it.next();
System.out.println(me);
}

}
MAIN CLASS

public class Assignment4 {

public static void main(String[] args) {


ContactList contactsList = new ContactList();

contactsList.addContact("Bob Biswas", 98310983);


contactsList.addContact("Police", 100);
contactsList.addContact("Alice", 98765432);

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 class CountryMap1 {


private TreeMap<String, String> M1;

public CountryMap1() {
M1 = new TreeMap<String, String>();
}

public TreeMap<String, String> saveCountryCapital(String


CountryName, String capital) {
M1.put(CountryName, capital);
return M1;
}

public String getCapital(String CountryName) {


return M1.get(CountryName);
}
public String getCountry(String capitalName) {
Set<Entry<String, String>> set = M1.entrySet();
Iterator<Entry<String, String>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, String> me = it.next();

if (me.getValue().equals(capitalName))
return me.getKey();
}

return null;
}

public TreeMap<String, String> swapKyeValue() {


TreeMap<String, String> M2 = new TreeMap<String,
String>();

Set<Entry<String, String>> set = M1.entrySet();


Iterator<Entry<String, String>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
M2.put(me.getValue(), me.getKey());
}

return M2;
}

public ArrayList<String> toArrayList() {


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

Set<Entry<String, String>> set = M1.entrySet();


Iterator<Entry<String, String>> it = set.iterator();

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 {

public static void main(String[] args) {


CountryMap1 countryMap = new CountryMap1();

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 class CountryMap {


private Hashtable<String, String> M1;

public CountryMap() {
M1 = new Hashtable<String, String>();
}

public Hashtable<String, String> saveCountryCapital(String


CountryName, String capital) {
M1.put(CountryName, capital);
return M1;
}

public String getCapital(String CountryName) {


return M1.get(CountryName);
}

public String getCountry(String capitalName) {


Set<Entry<String, String>> set = M1.entrySet();
Iterator<Entry<String, String>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, String> me = it.next();

if (me.getValue().equals(capitalName))
return me.getKey();
}

return null;
}

public Hashtable<String, String> swapKyeValue() {


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

Set<Entry<String, String>> set = M1.entrySet();


Iterator<Entry<String, String>> it = set.iterator();

while (it.hasNext()) {
Map.Entry<String, String> me = it.next();
M2.put(me.getValue(), me.getKey());
}

return M2;
}

public ArrayList<String> toArrayList() {


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

Set<Entry<String, String>> set = M1.entrySet();


Iterator<Entry<String, String>> it = set.iterator();

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 {

public static void main(String[] args) {


CountryMap countryMap = new CountryMap();

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

public class Assignment1 {

public static void main(String[] args) {


Thread t1 = new Thread("Scooby") {
public void run() {
System.out.println("I'm " +
Thread.currentThread().getName());
}
};

Thread t2 = new Thread("Shaggy") {


public void run() {
System.out.println("I'm " +
Thread.currentThread().getName());
}
};
t1.start();
t2.start();
}

2. store colours in the form of an array ex: String


colours[]={"white","blue","black","green","red","yellow"}; display
all colours repeatedly by generating colour index from Random
class. If the random colour index matches to red stop display.
Note: perform this task by implementing Runnabe interface.

SOLUTION
import java.util.Random;
public class Assignment2 implements Runnable {

public static void main(String[] args) {


Assignment2 assignment2 = new Assignment2();
Thread t1 = new Thread(assignment2);
t1.start();
}
public void run() {
Random random = new Random();
String colours[] = {"white", "blue", "black", "green",
"red", "yellow"};
int index;
while ((index = random.nextInt(6)) != 4) {
System.out.println(colours[index]);
}
}

THREAD CONTROL AND PRIORITIES ASSIGNMENT


1.Create a thread which prints 1 to 10. After printing 5, there
should be a delay of 5000 milliseconds before printing 6.

SOLUTION
package Milestone2.ThreadControl;

public class Assignment1 implements Runnable {


static Thread t1;

public static void main(String[] args) {


t1 = new Thread(new Assignment1());
t1.start();

}
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);
}
}

2.Create two threads, one thread to display all even numbers


between 1 & 20, another to display odd numbers between 1 &
20. Note: Display all even numbers followed by odd numbers
Hint: use join.

SOLUTION
public class Assignment2 implements Runnable {
static Thread oddThread;
static Thread evenThread;

public static void main(String[] args) {


Assignment2 assignment2 = new Assignment2();

oddThread = new Thread(assignment2, "OddThread");


evenThread = new Thread(assignment2, "EvenThread");
oddThread.start();
evenThread.start();
}

public void run() {


try {
if
(Thread.currentThread().getName().equals("OddThread"))
evenThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

for (int i = 1; i <= 20; i++) {


if
(Thread.currentThread().getName().equals("EvenThread")) {
if (i % 2 == 0) System.out.println(i);
}

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 {

public static void main(String[] args) {


Assignment3 assignment3 = new Assignment3();

Thread t1 = new Thread(assignment3, "Thread1");


Thread t2 = new Thread(assignment3, "Thread2");
Thread t3 = new Thread(assignment3, "Thread3");

t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);

t1.start();
t2.start();
t3.start();
}

public void run() {


for (int i = 0; i < 100; i++)
System.out.println(Thread.currentThread().getName()
+ ": " + i);
}

You might also like