0% found this document useful (0 votes)
44 views8 pages

DA1 Answer

The document describes a Java program that collects voter information from a user including name, age, phone number, and country of residence. It generates exceptions if the input is invalid, such as if the name contains numbers, age is less than 18, phone number is more than 10 digits, or country of residence is not India. The program uses a DataInputStream and BufferedReader to read input from the keyboard and handles exceptions for invalid input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views8 pages

DA1 Answer

The document describes a Java program that collects voter information from a user including name, age, phone number, and country of residence. It generates exceptions if the input is invalid, such as if the name contains numbers, age is less than 18, phone number is more than 10 digits, or country of residence is not India. The program uses a DataInputStream and BufferedReader to read input from the keyboard and handles exceptions for invalid input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Parth Sunil Chavan

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:

Code Reference: Professor Vishnu Sir

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

Plate p=new Plate();


Server s=new Server(p);
Customer c=new Customer(p);
s.start();
c.start();

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

class InvalidAgeException extends Exception{


public String toString()
{
return ("Age less than 18.");
}
}
class InvalidContactException extends Exception{
public String toString()
{
return ("Mobile Number is more than 10 digits.");
}
}
class InvalidCountryException extends Exception{
public String toString()
{
return ("Country of residence is not India.");
}
}

public class DA1_2


{
public static void main(String[] args) throws IOException {

InputStreamReader isr=new InputStreamReader(System.in);


BufferedReader br=new BufferedReader(isr);
DataInputStream dis = new DataInputStream(System.in);
boolean flag=false;
try{
System.out.print("Enter your name: ");
String name=br.readLine();
char[] chars=name.toCharArray();
for(char c:chars)
{
if(Character.isDigit(c))
{
flag=false;
throw new InvalidNameException();
}
else
{
flag=true;
}
}
}
catch(InvalidNameException e)
{
System.out.println("Name having numeral value");
}
try{

System.out.print("Enter age: ");


int age=Integer.parseInt(dis.readLine());
if(age<18)
{
flag=false;
throw new InvalidAgeException();

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

- The DataInputStream works with the binary data, while the


BufferedReader work with character data.

- All primitive data types can be handled by using the corresponding


methods in DataInputStream class, while only string data can be read
from BufferedReader class and they need to be parsed into the
respective primitives.

- DataInputStream is a part of filtered streams, while BufferedReader is


not.

- DataInputStream consumes less amount of memory space being it is


binary stream, where as BufferedReader consumes more memory
space being it is character stream.

- The data to be handled is limited in DataInputStream, where as the


number of characters to be handled has wide scope in BufferedReader.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy