Java Programming Lab-4
Java Programming Lab-4
Eg., Read the Register Number and Mobile Number of a student. If the Register Number does
not contain exactly 9 characters or if the Mobile Number does not contain exactly 10 characters,
throw an IllegalArgumentException. If the Mobile Number contains any character other than a
digit, raise a NumberFormatException. If the Register Number contains any character other than
digits and alphabets, throw a NoSuchElementException. If they are valid, print the message
‘valid’ else ‘invalid’
Code-
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author batch1
*/
if(r.length() != 9){
System.out.println("Invalid");
if(n.length() != 10){
System.out.println("Invalid");
Pattern a = Pattern.compile(pattern);
Matcher m1 = a.matcher(n);
if(!m1.find()){
Pattern b = Pattern.compile(pattern2);
Matcher m2 = b.matcher(r);
if(!m2.find()){
String reg;
String no;
System.out.println("Enter the registeration number followed by phone number");
reg = sc.nextLine();
no = sc.nextLine();
validate(reg, no);
System.out.println("Valid");
Output-
Program 2) Write a program to demonstrate the knowledge of students in multithreading.
Eg., Three students A, B and C of B.Tech- II year contest for the PR election. With the total
strength of 240 students in II year, simulate the vote casting by generating 240 random numbers
(1 for student A, 2 for B and 3 for C) and store them in an array. Create four threads to equally
share the task of counting the number of votes cast for all the three candidates. Use synchronized
method or synchronized block to update the three count variables. The main thread should
receive the final vote count for all three contestants and hence decide the PR based on the values
received.
Code-
MultiThreadVote.java-
import elections.Vote;
import elections.count;
import java.util.Vector;
Vector votevec = new Vector(240); // creating a vote array for 240 votes
a.start();
Vote b = new Vote(2, votevec);
b.start();
c.start();
try{
a.join();
b.join();
c.join();
}catch(Exception e){System.out.println(e);}
ac.start();
bc.start();
cc.start();
try{
ac.join();
bc.join();
cc.join();
}catch(Exception e){System.out.println(e);}
int av = ac.count;
int bv = bc.count;
int cv = cc.count;
if(av == bv || av == cv)
System.out.println("Tie in elections!");
else
if(av == bv || bv == cv)
System.out.println("Tie in elections!");
else
}
else if(cv >= av && cv >= bv){
if(cv == bv || cv == av)
System.out.println("Tie in elections!");
else
Vote.java-
package elections;
import java.util.Random;
import java.util.Vector;
int v, s;
Vector vec;
this.v = v;
this.vec = vec;
}
try
vec.add(v);
Thread.sleep(s);
catch(InterruptedException e)
}
Count.java-
package elections;
/**
* @author batch1
*/
import java.util.Vector;
Vector vec;
int k, i;
this.k = k;
this.vec = vec;
@Override
try{
if(vec.elementAt(i).equals(k))
count++;
}
catch(Exception e){
System.out.println(e);
Output-
Counting has ended!
elections.Vote Vector:
[3, 2, 1, 1, 2, 3, 3, 2, 1, 2, 1, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 2, 3, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 1, 3, 2, 1, 3, 2, 1, 1, 3, 2,
3, 1, 3, 1, 2, 3, 1, 2, 1, 2, 1, 3, 1, 2, 3, 2, 1, 2, 3, 1, 2, 3, 2, 1, 3, 2, 3, 1, 3, 3, 2, 1, 3, 1, 2, 2, 1, 3, 2, 2, 1, 2, 3,
2, 3, 1, 2, 3, 1, 2, 3, 2, 1, 3, 1, 2, 3, 2, 1, 3, 1, 3, 2, 1, 2, 1, 3, 3, 2, 1, 3, 1, 2, 3, 3, 1, 2, 1, 2, 3, 1, 3, 2, 1, 3, 1,
2, 2, 3, 1, 3, 2, 1, 3, 2, 3, 2, 1, 3, 2, 1, 3, 2, 3, 1, 2, 3, 2, 3, 1, 1, 2, 3, 2, 1, 2, 1, 2, 3, 2, 3, 1, 2, 1, 3, 1, 2, 3, 2,
2, 1, 1, 3, 1, 2, 2, 3, 1, 2, 1, 3, 2, 3, 2, 1, 3, 1, 2, 1, 3, 1, 3, 1, 2, 3, 1, 3, 2, 1, 2, 3, 2, 3, 1, 3, 2, 3, 1, 1, 2, 3, 2,
1, 3, 2, 1, 2, 3, 2, 3, 1, 2, 2, 3, 2, 1, 3, 1, 3, 1, 2, 3, 1, 2, 3, 1, 2]
80 votes for A
83 votes for B
77 votes for C
Create ‘n’ objects of this class for all the regular donors at Vellore. Write these objects to a file.
Read these objects from the file and display only those donors’ details whose blood group is
‘A+ve’ and had not donated for the recent six months.
Code-
import java.io.*;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.joda.time.LocalDate;
import org.joda.time.DateTime;
import org.joda.time.Period;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String name,addr,contact,bldgrp;
Date date;
String temp;
Matcher m;
Pattern r = Pattern.compile(pattern);
try{
Files.deleteIfExists(Paths.get("donations.txt"));
catch(NoSuchFileException e)
catch(IOException e)
System.out.println("Invalid permissions.");
System.out.println("Deletion successful.");
n = sc.nextInt();
sc.nextLine();
Donor arr[] = new Donor[n];
System.out.print("Name: ");
arr[i].name = sc.nextLine();
System.out.print("Address: ");
arr[i].addr = sc.nextLine();
System.out.print("Contact: ");
arr[i].contact = sc.nextLine();
arr[i].bldgrp = "";
m = r.matcher(arr[i].bldgrp);
while(!m.find()){
arr[i].bldgrp = sc.nextLine();
m = r.matcher(arr[i].bldgrp);
while(!flag){
System.out.print("Date: ");
temp = sc.nextLine();
try {
arr[i].date = ft.parse(temp);
flag = true;
} catch (ParseException e) {
flag = false;
outputstream.write("----------------\n".getBytes());
outputstream.write("Name: ".getBytes());
outputstream.write(arr[i].name.getBytes());
outputstream.write("\n".getBytes());
outputstream.write("Address: ".getBytes());
outputstream.write(arr[i].addr.getBytes());
outputstream.write("\n".getBytes());
outputstream.write("Contact: ".getBytes());
outputstream.write(arr[i].contact.getBytes());
outputstream.write("\n".getBytes());
outputstream.write(arr[i].bldgrp.getBytes());
outputstream.write("\n".getBytes());
outputstream.write("Date: ".getBytes());
outputstream.write(arr[i].date.toString().getBytes());
outputstream.write("\n".getBytes());
outputstream.write("\n".getBytes());
fos.write(c);
Date dt2;
try {
dt2 = ft2.parse(fileDate[1]);
System.out.println(content[i-3]);
System.out.println(content[i-2]);
System.out.println(content[i-1]);
System.out.println(content[i]);
System.out.println(content[i+1]);
System.out.println("\n");
} catch (ParseException e) {
Output-