LAB SHEET2
1. Shayam reads the integers from the user and calculates the
division of the numbers. Help him to handle the scenario if the
user enters string values or zero in second integer.
import [Link];
import [Link];
public class Exec1 {
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
System. [Link](" Enter scored java marks and max marks of
the exam");
try
{
int a=[Link]();
int b=[Link]();
float c =(a/b);
[Link]("answer is "+ c);
}
catch(InputMismatchException e)
{
[Link]("Please enter integer value");
}
catch(ArithmeticException e1)
{
[Link]("please enter greater than zero");
}
finally
{
[Link]();
[Link]("from finally block");
} }
}
2. Write a program to check the password matching. If not, raise the
IOException.
import [Link];
import [Link];
class Passwordcheck
{
static void check(String s1, String s2)throws IOException
{
if([Link](s2))
[Link]("password are matching");
else
throw new IOException(" not matching. PLesae enter again");
}
}
public class exec3
{
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("enter password twice");
String pwd=[Link]();
String pwd1=[Link]();
try
{
[Link](pwd,pwd1);
}
catch(IOException e)
{
[Link]([Link]());
}
catch(Exception e2)
{
[Link]("other errors");
}}}
3. Write a java program to raise the user defined exception if the user age is
less than 18.
import [Link];
import [Link];
class myexec extends Exception
{
myexec()
{
super("Age should be greater than 18");
}
}
public class Exec2 {
public static void main(String[] args)
{
Scanner sc= new Scanner([Link]);
[Link]("Enter age");
try
{
int a=[Link]();
if(a<18)
throw new myexec();
else
[Link]("Eligible for voting");
}
catch(InputMismatchException e)
{
[Link]("Please enter integer value");
}
catch(myexec e1)
{
[Link]([Link]());
}
} }
4. Shyam wants to withdraw the amount from the bank. If his balance is less
than the withdrawl amount , throw the insuffiecntfundexception.
Otherwise display the current balance.
import [Link];
import [Link];
class InsufficientException extends Exception
{
InsufficientException(String msg)
{
super(msg);
}
}
public class exec4 {
public static void main(String[] args)
{ float balance=0;
Scanner sc= new Scanner([Link]);
[Link](" Enter account balance and withdrawal
amount");
try
{
balance=[Link]();
float wdamt =[Link]();
if(balance <wdamt)
throw new InsufficientException("balance is
less, cannot withdraw");
else
{
balance= balance-wdamt;
[Link]("balance amount" +
balance);
}
}
catch(InputMismatchException e)
{
[Link]("Only numeric values");
}
catch(InsufficientException e1)
{
[Link](e1);
} }
5. Write a java application to show the multiple threads.
class MyThreadDemo implements Runnable{
Thread t,t1;
MyThreadDemo () {
t = new Thread(this,"My thread");
t1= new Thread(this,"second");
[Link]();
[Link]();
}
public void run()
{
[Link]("Child thread started");
[Link]("thread details"+[Link]());
}
}
class ThreadDemo1 {
public static void main (String args[]){
new MyThreadDemo();
[Link]("Main thread started");
[Link]([Link]());
try
{
[Link](15000);
}
Catch(Exception e) {}
[Link](“Mian thread working”);
}
}
6. Write a code to print the numbers using the main thread and child thread.
public class Mythread2 extends Thread
{
Thread t1;
Mythread2()
{
t1=new Thread(this,"First");
[Link]();
}
public void run()
{
try
{
for(int i=1;i<=5;i++)
{
[Link]("from t1 "+ i);
[Link](5000);
}
}
catch(InterruptedException exception)
{
[Link]("Error occured ");
}
}
public static void main(String ss[]) throws Exception
{
Mythread2 obj=new Mythread2();
try
{
for(int i=1;i<=8;i++)
{
[Link]("from main thread "+ i);
[Link](10000);
}
}
catch(InterruptedException exception)
{
[Link]("Error occured ");
} } }
7. Anshul wants to print odd numbers and even numbers with the help of
threads. Help him to complete the task by reading the limit from him.
import [Link];
public class OddEvenThread
{
public static void main(String[] args)
{
int limit;
Scanner sc = new Scanner([Link]);
[Link]("Enter the limit : ");
limit = [Link]();
Thread t1 = new Thread(new OddThread(limit));
Thread t2 = new Thread(new EvenThread(limit));
[Link]();
[Link]();
try
{
[Link](25000);
}
catch(Exception e)
{
[Link]([Link]());
}
[Link]([Link]());
}
}
class EvenThread implements Runnable
{
int limit;
public EvenThread(int limit)
{
[Link] = limit;
}
public void run()
{
for (int even =2;even <= limit; even+=2)
{
[Link]("Even thread : " + even);
try
{
[Link](5000);
}
catch(Exception e)
{
[Link]([Link]());
}
}
}
}
class OddThread implements Runnable
{
int limit;
public OddThread(int limit)
{
[Link] = limit;
}
public void run()
{
for (int odd=1;odd <= limit;odd+=2)
{
[Link]("Odd thread : " + odd);
try
{
[Link](5000);
}
catch(Exception e)
{
[Link]([Link]());
}
}
}
}
8. Shashi is developing java application using multiple threads. He ensures
the main thread is the last one to exit from the program. Help him to
understand this concept with the help of an example.
class jointhread extends Thread
{
public void run()
{
String n= [Link]().getName();
try
{
for(int i=1;i<=3;i++)
{
[Link](n + " :" + i);
[Link](3000);
}
}
catch(InterruptedException e)
{
[Link](e);
}
}
}
class demojoin1
{
public static void main(String args[])
{
jointhread t1=new jointhread();
jointhread t2=new jointhread();
[Link]("First Thread");
[Link]("Second Thread");
[Link]();
[Link]();
[Link]("Thread is alive "+[Link]());
[Link]("Thread is alive "+[Link]());
try
{
[Link]("waiting");
[Link]();
[Link]();
}
catch(Exception e)
{
[Link](e);
}
[Link]("T1 is alive "+[Link]());
[Link]("T2 is alive"+ [Link]());
[Link]("main thread alive
"+[Link]().isAlive());
}
}
9. Write a Java program represents a basic scenario of a bus with only
one available seat and three customers are trying to book the seats. The
Bus class should implement the Runnable interface, and the customer
class contains the main method where three threads (representing
customers) try to book a seat in the bus. Ensure that only one thread
can execute the run method at a time, preventing race conditions.
class Bus implements Runnable
{
int ava=1,pass;
Bus(int pass)
{
[Link]=pass;
}
public synchronized void run()
{
String name=[Link]().getName();
if(ava>=pass)
{
[Link](" Booking is confirmed " +name);
ava=ava-pass;
}
else
{
[Link]("Seats are not available");
}
}
}
class Customer
{
public static void main(String args[])
{
Bus r=new Bus(1);
Thread t1 =new Thread(r);
Thread t2 =new Thread(r);
Thread t3 =new Thread(r);
[Link]("Himanshi");
[Link]("Naveen");
[Link]("Akshay");
[Link]();
[Link]();
[Link]();
}
}
10. Create an ArrayList or any other type of list. Add elements to the
list. Use an iterator or an enhanced for loop to iterate through the list
elements.
import [Link].*;
class Collection1
{
public static void main(String args[])
{
ArrayList<String> list=new ArrayList<String>();
[Link]("Ravi");
[Link]("Vijay");
[Link]("Greeshma");
[Link]("Vanita");
//Traversing list through Iterator
Iterator itr=[Link]();
while([Link]())
{
[Link]([Link]());
[Link]([Link](3));
} } }
11. write a code to sort out the numbers using Arraylist.
import [Link];
import [Link]; // Import the Collections class
public class Collection2 {
public static void main(String[] args) {
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
[Link](33);
[Link](15);
[Link](20);
[Link](34);
[Link](8);
[Link](12);
[Link](myNumbers);
for (int i : myNumbers)
{
[Link](i);
} } }
12. Write a Java program to iterate through all elements in a linked list starting
at the specified position.
import [Link];
import [Link];
public class Collection3
{
public static void main(String[] args) {
// create an empty linked list
LinkedList<String> l_list = new LinkedList<String>();
// use add() method to add values in the linked list
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("White");
l_list.add("Pink");
// set Iterator at specified index
Iterator p = l_list.listIterator(1);
// print list from second position
while ([Link]()) {
[Link]([Link]());
}
}
}
13. Write a Java program to convert a hash set to a List/ArrayList.
import [Link].*;
public class Collection4 {
public static void main(String[] args)
{
// Create a empty hash set
HashSet<String> h_set = new HashSet<String>();
// use add() method to add values in the hash set
h_set.add("Red");
h_set.add("Green");
h_set.add("Black");
h_set.add("White");
h_set.add("Pink");
h_set.add("Yellow");
[Link]("Original Hash Set: " + h_set);
// Create a List from HashSet elements
List<String> list = new ArrayList<String>(h_set);
// Display ArrayList elements
[Link]("ArrayList contains: "+ list);
}
}
14. Write a code to reverses the priorities of the String values in the
PriorityQueue:
import [Link];
import [Link];
public class Collection5
{
static class CustomComparator implements Comparator<String> {
@Override
public int compare(String item1, String item2)
{
return [Link](item2) < 0 ? 1 : -1;
}
}
public static void main(String[] args)
{
PriorityQueue<String> food = new PriorityQueue<String>(new
CustomComparator());
[Link]("Cabbage");
[Link]("Pizza");
[Link]("Sausage");
[Link]("Potatoes");
[Link]("Salad");
while ([Link]() > 0) {
[Link]([Link]());
} } }
15. Write a Java program to associate the specified value with the specified
key in a Tree Map.
import [Link].*;
public class Collection6 {
public static void main(String args[])
{
// Create a tree map
TreeMap<Integer,String> tree_map=new TreeMap<Integer,String>();
// Put elements to the map
tree_map.put(1, "Red");
tree_map.put(2, "Green");
tree_map.put(3, "Black");
tree_map.put(4, "White");
tree_map.put(5, "Blue");
for ([Link]<Integer,String> entry : tree_map.entrySet())
{
[Link]([Link]() + "=>" + [Link]());
}
}
}
16. Write a program to show HashMap.
import [Link].*;
class Collection7
{
public static void main(String[] ar)
{
HashMap<String,Double> hm = new HashMap<String,Double>();
[Link]("Komal",100.123);
[Link]("swati",92.23);
[Link]("Kushal",1378.00);
[Link]("Jhanvi",-12.67);
Set<[Link]<String,Double>> set=[Link]();
for([Link]<String,Double> obj:set)
{
[Link]([Link]()+" ");
[Link]([Link]());
}
}
}