0% found this document useful (0 votes)
41 views21 pages

CSE-1007 (Java Programming) Digital Assignment-2

This document contains code for 5 Java programming assignments. The first assignment validates a registration number and mobile number based on length. The second validates an employee ID format. The third simulates multi-threaded voting and counting. The fourth recommends activities based on the time of day. The fifth reads and writes donor objects with serialization and finds donors who last donated over 6 months ago with blood type A+.

Uploaded by

Ayush Roy
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)
41 views21 pages

CSE-1007 (Java Programming) Digital Assignment-2

This document contains code for 5 Java programming assignments. The first assignment validates a registration number and mobile number based on length. The second validates an employee ID format. The third simulates multi-threaded voting and counting. The fourth recommends activities based on the time of day. The fifth reads and writes donor objects with serialization and finds donors who last donated over 6 months ago with blood type A+.

Uploaded by

Ayush Roy
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/ 21

CSE-1007 (Java Programming)

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

if(empid.length()!=8 || empid.charAt(2)!='-' || empid.charAt(4)!='-')


throw new InvalidEmployeeCode();

if(!(empid.charAt(3)=='F' || empid.charAt(3)=='S' || empid.charAt(3)=='f' ||


empid.charAt(3)=='s'))
throw new InvalidEmployeeCode();

for(int i=5;i<=7;i++)
{
char c=empid.charAt(i);
if(!(c>='0' && c<='9'))
throw new InvalidEmployeeCode();
}

if(!(empid.charAt(0)==year.charAt(2) && empid.charAt(1)==year.charAt(3)))


throw new InvalidEmployeeCode();

System.out.println("\nEmployee ID: "+ empid +"\nName: "+name+"\nBirth year: "+year);


}
}

class InvalidEmployeeCode extends Exception


{
public String toString()
{
return "Employee code is invalid";
}
}
Sample I/O: -
3)

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

class vote extends Thread


{
Random rand = new Random();
int max = 750;
int min = 100;
int v, s;
Vector vec;
public vote(int v, Vector vec)
{
this.v = v;
this.vec = vec;
}
public void run()
{
try
{
while(vec.size() < 240)
{
System.out.println("Thread " + this.getId() + " (voting)");
vec.add(v);
s = rand.nextInt((max - min) + 1) + min;
System.out.println("Thread " + this.getId() + " (sleeping for " + s + ")");
Thread.sleep(s);
}
}
catch(InterruptedException e)
{
System.out.println("Voting Exception: " + e);
}
}
}

public class C2Q3


{
public static void main(String[] args)
{
Vector votevec = new Vector(240);
vote a = new vote(1, votevec);
a.start();
vote b = new vote(2, votevec);
b.start();
vote c = new vote(3, votevec);
c.start();
try
{
a.join();
b.join();
c.join();
System.out.println("Voting has ended!");
}
catch(Exception e)
{
System.out.println(e);
}
count ac = new count(1, votevec);
count bc = new count(2, votevec);
count cc = new count(3, votevec);
ac.start();
bc.start();
cc.start();
try
{
ac.join();
bc.join();
cc.join();
System.out.println("Counting ended");
}
catch(Exception e)
{
System.out.println(e);
}
int av = ac.count;
int bv = bc.count;
int cv = cc.count;
System.out.println("elections.Vote Vector:" + "\n" + votevec);
System.out.println(av + " votes for A");
System.out.println(bv + " votes for B");
System.out.println(cv + " votes for C");
if(cv >= av && cv >= bv)
{
if(cv == bv || cv == av)
System.out.println("Tie");
else
System.out.println("C won");
}
else if(bv >= av && bv >= cv)
{
if(av == bv || bv == cv)
System.out.println("Tie");
else
System.out.println("B won");
}
else if(av >= bv && av >= cv)
{
if(av == bv || av == cv)
System.out.println("Tie");
else
System.out.println("A won");
}
}
}
Sample I/O: -
4)

Code: -
import java.util.*;
class ClockAPP extends Exception
{
public String disp(String msg)
{
return msg;
}
}

public class C2Q4


{
public static void main (String[] args) throws ClockAPP
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter time in hours: ");
float time=sc.nextFloat();
String timeofDay="";

if(time>=5 && time<12)


{
try
{
timeofDay="Morning, Have Fresh Vegetable Juice and then Sugar Tablet with mild
walking.";
throw new ClockAPP();
}
catch(ClockAPP e)
{
System.out.println(e.disp(timeofDay));
}
}

else if(time>=12 && time <17)


{
try
{
timeofDay="Its Day Time, after lunch have tablet to avoid sleep.";
throw new ClockAPP();
}
catch(ClockAPP e)
{
System.out.println(e.disp(timeofDay));
}
}
else if(time>=17 && time <18.30)
{
try
{
timeofDay="Hello, Good Evening have a dinner";
throw new ClockAPP();
}
catch(ClockAPP e)
{
System.out.println(e.disp(timeofDay));
}
}

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

You might also like