DA1 Answer
DA1 Answer
20BCI0055
CSE1007 Slot: L23+L24
LAB DA-1
1] Consider you want to eat a set of items in an order at famous Vellore Barbeque Nation (V-
BBQ). The sequence includes juice, soup, chicken tikka, kabab, paneer tikka, fish65, biriyani
rice, lime juice, butter milk, fruit salad, and pan. The same sequence has been updated to the
server who serves the food on your plate. He can serve the food in an order mentioned by the
client. Create a Server and Customer problem in Threads using Thread Synchronization for the
above mentioned scenario. The graphical representation of the scenario is given below:
Java Code:
import java.rmi.ServerRuntimeException;
import java.util.*;
public class DA1_1
{
public static class Plate
{
private String item;
private boolean empty=true;
public synchronized void put(String item)
{
while(!empty)
{
try{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
empty=false;
this.item=item;
notifyAll();
}
public synchronized String take()
{
while(empty)
{
try{
wait();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
empty=true;
notifyAll();
return this.item;
}
}
public static class Server extends Thread
{
private Plate plate;
public Server(Plate plate)
{
this.plate=plate;
}
public void run()
{
String
items[]={"Chicken65","Burger","Pizza","Paneer65","Buttermilk"};
Random random=new Random();
for(int i=0;i<items.length;i++)
{
plate.put(items[i]);
try{
Thread.sleep(random.nextInt(5000));
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("Put "+items[i]);
}
}
}
public static class Customer extends Thread
{
private Plate plate;
public Customer(Plate plate)
{
this.plate=plate;
}
public void run()
{
Random random=new Random();
for(String
message=plate.take();!message.equals("Done");message=plate.take())
{
System.out.println("message: "+message);
}
try
{
Thread.sleep(random.nextInt(5000));
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
public static void main(String[] args) {
}
}
Output:
2]A person in our country is eligible to cast their vote if the one satisfies two conditions. (i) The
person must be an Indian, (ii) must have at least 18 years. If the Government has assigned a task
to maintain the database of our Indian voters list. In this regard, you started collecting the
information of each person if the one is eligible. Collect the information for the following
attributes: name, age, phone number, and country of residence. Generate the following exceptions
when the information entered by user is wrong. a. Throw InvalidNameException if the entered
name is having any numerals.
b. Throw InvalidAgeException if the age is <18
c. Throw InvalidContactException if the mobile number is more than 10 digits.
d. Throw InvalidCountryException if the country of residence is not India.
Use the combination of DataInputStream and BufferedReader classes to read the input from
keyboard. Give some differences between them.
Java Code:
import java.io.*;
import javax.naming.InvalidNameException;
class VotersInfo
{
String name;
int age;
String phone;
String country;
public VotersInfo(String n,int a,String p,String c)
{
this.name=n;
this.age=a;
this.phone=p;
this.country=c;
}
}
else
flag=true;
}
catch(InvalidAgeException e)
{
System.out.println(e);
}
try{
System.out.print("Enter phone number: ");
String phone=br.readLine();
if(phone.length()>10)
{
flag=false;
throw new InvalidContactException();
}
else
flag=true;
}
catch(InvalidContactException e)
{
System.out.println(e);
}
try{
System.out.print("Enter the country: ");
String country=br.readLine();
if(country.equals("India"))
{
flag=true;
}
else{
flag=false;
throw new InvalidCountryException();
}
}
catch(InvalidCountryException e)
{
System.out.println(e);
}
finally
{
System.out.println("\n");
if(flag==true)
{
System.out.println("Eligible to cast vote");
}
else
System.out.println("Not eligible to cast vote");
}
}
Output:
Difference between DataInputStream and BufferedReader: