CSE-1007 (Java Programming) Digital Assignment-2
CSE-1007 (Java Programming) Digital Assignment-2
DIGITAL ASSIGNMENT-2
Name: - Ayush Roy Slot: - L51+L52
Reg No: - 20BCE0260 Date: - 22/10/21
1)
Code: -
import java.util.*;
public class C2Q1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Registration Number: ");
String regno=sc.nextLine();
System.out.print("Enter Mobile Number: ");
String mobile=sc.nextLine();
if(regno.length()!=9)
{
System.out.println("\nInvalid");
throw new IllegalArgumentException("Registration number should be of length 9
characters only!");
}
if(mobile.length()!=10)
{
System.out.println("\nInvalid");
throw new IllegalArgumentException("Mobile number should be of length 10 only!");
}
for(int i=0;i<10;i++)
{
char c=mobile.charAt(i);
if(!(c>='0'&& c<='9'))
{
System.out.println("\nInvalid");
throw new NumberFormatException("Mobile number can only contain digits!");
}
}
for(int i=0;i<9;i++)
{
char c=regno.charAt(i);
if(!(c>='0' && c<='9')&& !(c>='A' && c<='Z')&& !(c>='a' && c<='z'))
{
System.out.println("\nInvalid");
throw new NoSuchElementException("Registration number can only contain digits
and alphabets!");
}
}
System.out.println("\nValid");
}
}
Sample I/O: -
2)
Code: -
import java.util.*;
public class C2Q2
{
public static void main(String args[]) throws InvalidEmployeeCode
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Employee ID: ");
String empid=sc.nextLine();
System.out.print("Enter Name: ");
String name=sc.nextLine();
System.out.print("Enter Year: ");
String year=sc.nextLine();
for(int i=5;i<=7;i++)
{
char c=empid.charAt(i);
if(!(c>='0' && c<='9'))
throw new InvalidEmployeeCode();
}
Code: -
import java.util.*;
class count extends Thread
{
Vector vec;
int k, i;
public int count = 0;
public count(int k, Vector vec)
{
this.k = k;
this.vec = vec;
}
public void run()
{
try
{
for(i = 0; i < vec.capacity(); i++)
{
if(vec.elementAt(i).equals(k))
count++;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Code: -
import java.util.*;
class ClockAPP extends Exception
{
public String disp(String msg)
{
return msg;
}
}
else
{
try
{
timeofDay="Night, Go for sleep.";
throw new ClockAPP();
}
catch(ClockAPP e)
{
System.out.println(e.disp(timeofDay));
}
}
}
}
Sample I/O: -
5)
Code:-
import java.io.*;
import java.text.*;
import java.util.*;
class Donor implements Serializable
{
String name, address, blood;
Date dod;
int age;
Donor(String name, String address, String blood, Date dod, int age) {
this.name = name;
this.address = address;
this.blood = blood;
this.dod = dod;
this.age = age;
}
public void display()
{
System.out.println("\nName: " + name + "\nAddress: " + address + "\nBlood Group: " +
blood + "\nAge: " + age);
}
}
public class C2Q5
{
public static int getMonths(Date start, Date end)
{
Calendar startCal = new GregorianCalendar();
startCal.setTime(start);
Calendar endCal = new GregorianCalendar();
endCal.setTime(end);
int diffYear = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCal.get(Calendar.MONTH) -
startCal.get(Calendar.MONTH);
return diffMonth;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int i, n;
System.out.print ("Enter no. of regular donors in Vellore: ");
n= sc.nextInt();
Donor donors[] = new Donor[n];
String name, address, blood, dod;
int age;
try
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
for (i= 0; i < n; i++)
{
System.out.print ("\nEnter Details of Donor "+ (i+1) + "\nEnter name: ");
name= sc.next();
System.out.print ("Enter address: ");
address= sc.next();
System.out.print ("Enter blood group: ");
blood= sc.next();
System.out.print ("Enter last date of donation: ");
dod= sc.next();
System.out.print ("Enter age: ");
age= sc.nextInt();
donors[i] = new Donor(name, address, blood, sdf.parse(dod), age);
}
}
catch (ParseException e)
{
System.out.println(e);
}
String filename = "donations.txt";
try
{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(donors);
oos.close();
fos.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}
try
{
FileInputStream input = new FileInputStream(filename);
ObjectInputStream output = new ObjectInputStream(input);
Donor[] savedDonors = (Donor[])output.readObject();
input.close();
output.close();
System.out.println("\nDonor(s)");
for(Donor d: savedDonors)
{
if(getMonths(d.dod, new Date()) > 6 && d.blood.equals("A+"))
d.display();
}
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Sample I/O:-