Updated - FS Lab Manual
Updated - FS Lab Manual
PART – A
1. Write a program to read series of names, one per line, from standard input and write these names
spelled in reverse order to the standard output using I/O redirection and pipes. Repeat the exercise
using an input file specified by the user instead of the standard input and using an output file
specified by the user instead of the standard output.
2. Write a program to read and write student objects with fixed-length records and the fields
delimited by “|”. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
3. Write a program to read and write student objects with Variable - Length records using any
suitable record structure. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
4. Write a program to write student objects with Variable - Length records using any suitable record
structure and to read from this file a student record using RRN.
5. Write a program to implement simple index on primary key for a file of student objects.
Implement add ( ), search ( ), delete ( ) using the index.
6. Write a program to implement index on secondary key, the name, for a file of student objects.
Implement add( ), search ( ), delete ( ) using the secondary index.
7. Write a program to read two lists of names and then match the names in the two lists using
Consequential Match based on a single loop. Output the names common to both the lists.
8. Write a program to read ‘k’ lists of names and merge them using k-way merge algorithm with k=8
Page 1
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
1. Write a program to read series of names, one per line, from standard input and write these
names spelled in reverse order to the standard output using I/O redirection and pipes. Repeat
the exercise using an input file specified by the user instead of the standard input and using an
output file specified by the user instead of the standard output.
import java.util.*;
import java.io.*;
class Lab1
{
Scanner scan = new Scanner(System.in);
public static void main(String args[]) throws IOException
{
Lab1 obj = new Lab1();
int choice;
while(true)
{
System.out.println("**********************");
System.out.println("1. Accept Input from Standard Input Device");
System.out.println("2. Accept Input from File");
System.out.println("3. Exit");
System.out.println("**********************");
Page 2
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
scan.nextLine();
while(n!=0)
{
System.out.println("Enter the name to be Reversed");
org = scan.nextLine();
rev= Rev_String(org);
System.out.println("Reverse of entered string is: "+rev);
n=n-1;
}
}
br.close();
pw.close();
}
Output:
**********************
1. Accept Input from Standard Input Device
Page 3
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
**********************
1. Accept Input from Standard Input Device
2. Accept Input from File
3. Exit
**********************
Please enter your choice:
2
Enter the input file name(with extension)
f1.txt
Enter the output file name(with extension)
f2.txt
All Reverse Names written to f2.txt
**********************
1. Accept Input from Standard Input Device
2. Accept Input from File
3. Exit
**********************
Please enter your choice:
3
You chose exit!
Page 4
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 5
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 6
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
2. Write a program to read and write student objects with fixed-length records and the fields
delimited by “|”. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
import java.io.*;
import java.util.*;
class Lab2
{
final int size=50;
Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException,NullPointerException
{
Lab2 obj = new Lab2();
int choice;
while(true)
{
System.out.println("**********************");
System.out.println("1.Pack()");
System.out.println("2.Unpack()");
System.out.println("3.Search()");
System.out.println("4.Modify()");
System.out.println("5.Exit");
System.out.println("**********************");
System.out.println("Please enter your choice:");
choice = obj.scan.nextInt();
obj.scan.nextLine();
switch(choice)
{
case 1:
obj.pack();
break;
case 2:
obj.unpack();
break;
case 3:
obj.search();
break;
case 4:
obj.modify();
break;
case 5:
System.out.println("You chose exit!");
System.exit(0);
default:
System.out.println("Invalid Option");
}
Page 7
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
}
}
Page 8
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 9
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Output :
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
1
Enter Name, USN, Sem and Branch
Monisha
1BY15CV030
06
Civil
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
Page 10
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
5.Exit
**********************
Please enter your choice:
1
Enter Name, USN, Sem and Branch
Allan Jones
1BY15ME013
06
Mechanical
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
1
Enter Name, USN, Sem and Branch
Suraj Kumar
1BY15EE052
06
Electrical and Electronics
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
1
Enter Name, USN, Sem and Branch
Deepak Joshua
1BY15EC010
06
Electronics and Communication
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
2
Page 11
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 12
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
File Modified
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
2
The details are: Monisha 1BY15CV030 06 Civil
The details are: Allan Jones 1BY15ME013 06 Mechanical
The details are: Suraj Kumar 1BY15EE052 06 Electrical and Electroni
The details are: Deepak Joshua 1BY15EC010 06 ECE
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
5
You chose exit!
Page 13
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
3. Write a program to read and write student objects with Variable - Length records using any
suitable record structure. Implement pack ( ), unpack ( ), modify ( ) and search ( ) methods.
import java.io.*;
import java.util.*;
class Lab3
{
Scanner scan = new Scanner(System.in);
Page 14
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
pw.println(b);
pw.flush();
pw.close();
}
Page 15
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
usn=result[1];
sem= result[2];
branch=result[3];
if(usn.equals(usn1))
{
System.out.println("Match found. The details of the record are:");
System.out.println(name + " " + usn + " " + sem + " " + branch);
br.close();
return;
}
}
System.out.println("Record not found");
br.close();
}
if(usn.equals(usn1))
{
System.out.println("The details are: " + name + " " + usn + " " + sem +
" " + branch);
System.out.println("enter name, usn,sem and branch");
String name11 = scan.nextLine();
String usn11 = scan.nextLine();
String sem11 = scan.nextLine();
String branch11 = scan.nextLine();
String b = name11+"|"+usn11+"|"+sem11+"|"+branch11+"|";
int le = b.length();
String s1 = "-";
Page 16
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
if(le<50)
{
for(int j=le;j<=50;j++)
b = b.concat(s1);
pw.println(b);
}
}
else
{
pw.println(r);
}
}
pw.flush();
pw.close();
br.close();
file.delete()
temp.renameTo(file)
System.out.println("File Modified");
}
}
Output:
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
1
Enter Name, USN, Sem and Branch
Monisha
1BY15CV030
06
Civil
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
1
Page 17
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 18
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 19
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
File Modified
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
2
The details are: Monisha 1BY15CV030 06 Civil
The details are: Allan Jones 1BY15ME013 06 Mechanical
The details are: Suraj Kumar 1BY15EE052 06 Electrical and Electronics
The details are: Deepak Joshua 1BY16EC010 04 ECE
**********************
1.Pack()
2.Unpack()
3.Search()
4.Modify()
5.Exit
**********************
Please enter your choice:
5
You chose exit!
Page 20
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
4. Write a program to write student objects with Variable - Length records using any suitable
record structure and to read from this file a student record using RRN.
import java.io.*;
import java.util.*;
class Lab4
{
public static int count;
public static final int[] rrn = new int[20];
Scanner scan = new Scanner(System.in);
int choice;
while(true)
{
System.out.println("**********************");
System.out.println("1. Pack()");
System.out.println("2. Unpack()");
System.out.println("3. Search()");
System.out.println("4. Exit");
System.out.println("**********************");
System.out.println("Please enter your choice:");
choice = obj.scan.nextInt();
obj.scan.nextLine();
switch(choice)
{
case 1:
obj.pack();
break;
case 2:
obj.unpack();
break;
case 3:
System.out.println("Enter the rrn number to search the record");
int r = obj.scan.nextInt();
obj.search(r);
break;
case 4:
System.out.println("You chose exit!");
System.exit(0);
default:
Page 21
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
System.out.println("Invalid option");
break;
}
}
}
Page 22
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
{
String[] result = s.split("\\|");
name = result[0];
usn = result[1];
sem = result[2];
branch = result[3];
System.out.println("The details are: " + name + " " + usn + " " + sem + " " +
branch);
}
br.close();
}
Output:
**********************
1. Pack()
2. Unpack()
3. Search()
4. Exit
**********************
Please enter your choice:
1
Enter Name,USN,Sem and Branch
Monisha
1BY15CV030
06
Civil
Page 23
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 24
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Page 25
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
5. Write a program to implement simple index on primary key for a file of student objects.
Implement add ( ), search ( ), delete ( ) using the index.
import java.io.*;
import java.util.Scanner;
Page 26
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
break;
default:
System.out.println("Invalid Option");
}
}
}
Page 27
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
{
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("f1.txt"),true));
System.out.println("Enter USN,Name,Sem and Branch ");
String usn = s.nextLine();
String name = s.nextLine();
String sem = s.nextLine();
String branch = s.nextLine();
String b = usn+"|"+name+"|"+sem+"|"+branch+"|"+"$";
pw.println(b);
pw.close();
create_index();
}
if(pos!=-1)
display_record(pos);
else
System.out.println("Record not found");
}
if(usn_list[mid].compareTo(key)>0)
high = mid - 1;
if(usn_list[mid].compareTo(key)<0)
low = mid + 1;
}
return -1;
}
Page 28
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
file.seek(address);
String s = file.readLine();
while(s!=null)
{
String[] result = s.split("\\|");
usn = result[0];
name = result[1];
sem = result[2];
branch = result[3];
System.out.println("\nRecord Details");
System.out.println("USN: " + usn);
System.out.println("Name: " + name);
System.out.println("Sem: " + sem);
System.out.println("Branch: " + branch);
break;
}
file.close();
}
Page 29
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
String ch = s.nextLine();
if(ch.equalsIgnoreCase("y"))
{
int address= Address_list[pos];
String del_ch="*";
file.seek(address);
file.writeBytes(del_ch);
System.out.println("Record is deleted");
}
file.close();
}
}
Output:
******Menu******
1. Add Record
2. Search Record
3. Remove Record
4. Exit
****************
Page 30
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Record Details
USN: 1BY15ME013
Name: Allan Jones
Sem: 06
Branch: Mechanical
Record Details
USN: 1BY15ME013
Name: Allan Jones
Sem: 06
Branch: Mechanical
Are you sure you want to delete? (Y/N)
y
Record is deleted
Page 31
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
6. Write a program to implement index on secondary key, the name, for a file of student
objects. Implement add ( ), search ( ), delete ( ) using the secondary index.
import java.io.*;
import java.util.Scanner;
Page 32
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
break;
default:
System.out.println("Invalid Option");
}
}
}
Page 33
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
pw.println(b);
pw.close();
create_index();
}
int t = 0;
pos = search_index(key);
if(pos!=-1)
{
display_record(pos);
t = pos;
while((t<count)&&(Name_list[++t].equals(key)))
display_record(t);
t = pos;
while((t>=0) &&(Name_list[--t].equals(key)))
display_record(t);
}
else
System.out.println("Record not found");
}
Page 34
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
{
mid = (low + high)/2;
if(Name_list[mid].equals(key))
return mid;
if(Name_list[mid].compareTo(key)>0)
high = mid - 1;
if(Name_list[mid].compareTo(key)<0)
low = mid + 1;
}
return -1;
}
file.seek(address);
String s = file.readLine();
while(s!=null)
{
String[] result = s.split("\\|");
usn = result[0];
name = result[1];
sem = result[2];
branch = result[3];
System.out.println("\nRecord Details");
System.out.println("USN: " + usn);
System.out.println("Name: " + name);
System.out.println("Sem: " + sem);
System.out.println("Branch: " + branch);
break;
}
file.close();
}
Page 35
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
pos = search_index(key);
if(pos != -1)
{
delete_from_file(pos);
t = pos;
while((t<count)&&(Name_list[++t].equals(key)))
delete_from_file(t);
t=pos;
while((t>=0) &&(Name_list[--t].equals(key)))
delete_from_file(t);
create_index();
}
else
System.out.println("Record not found");
}
if(ch.equalsIgnoreCase("y"))
{
int address= Address_list[pos];
String del_ch="*";
file.seek(address);
String str = file.readLine();
int x = str.indexOf('|');
x++;
file.seek(address + x);
file.writeBytes(del_ch);
System.out.println("Record is deleted");
}
file.close();
}
}
Page 36
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Output:
******Menu******
1. Add Record
2. Search Record
3. Remove Record
4. Exit
****************
Page 37
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Computer Science
Record Details
USN: 1BY15CV030
Name: Monisha
Sem: 06
Branch: Civil
Record Details
USN: 1BY15CS026
Name: Monisha
Sem: 06
Branch: Computer Science
Record Details
USN: 1BY15CV030
Name: Monisha
Sem: 06
Branch: Civil
Are you sure you want to delete? (Y/N)
Y
Record is deleted
Record Details
USN: 1BY15CS026
Name: Monisha
Sem: 06
Branch: Computer Science
Are you sure you want to delete? (Y/N)
N
Page 38
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Record Details
USN: 1BY15CS026
Name: Monisha
Sem: 06
Branch: Computer Science
Page 39
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
7. Write a program to read two lists of names and then match the names in the two lists using
Cosequential Match based on a single loop. Output the names common to both the lists.
import java.util.*;
import java.io.*;
obj.readNames("list2.txt");
obj.combineLists();
obj.display();
sort(s,i);
for(j=0;j<i;j++)
pw.println(s[j]);
pw.close();
}
Page 40
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
String temp;
for(int i=0;i<count;i++)
for(int j=i+1;j<count;j++)
if(s[i].compareTo(s[j])>0)
{
temp = s[i];
s[i]=s[j];
s[j]=temp;
}
}
else if(name1.compareTo(name2)<0)
name1 = br1.readLine();
else
name2 = br2.readLine();
}
pw.close();
br2.close();
br1.close();
}
Page 41
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Output :
Enter the names in list 1 (Enter # to terminate the list)
Suraj
Monisha
Abhijeet
Akask
Vikranth
Praveen
#
Enter the names in list 2 (Enter # to terminate the list)
Abhijeet
Praveen
Prakash
Vikranth
Nisha
Anisha
Sanjay
#
Common names in both lists are:
Abhijeet
Praveen
Vikranth
Page 42
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
8. Write a program to read k Lists of names and merge them using k-way merge algorithm
with k = 8.
import java.io.*;
import java.util.*;
Page 43
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
temp = s[i];
s[i]=s[j];
s[j]=temp;
}
}
else if(name1.compareTo(name2)<0)
{
pw.println(name1);
name1 = br1.readLine();
}
else
{
pw.println(name2);
name2 = br2.readLine();
}
}
if(name1 == null)
{
Page 44
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
while(name2 != null)
{
pw.println(name2);
name2 = br2.readLine();
}
}
if(name2 == null)
{
while(name1 != null)
{
pw.println(name1);
name1 = br1.readLine();
}
}
pw.close();
br1.close();
br2.close();
f1.delete();
f2.delete();
f3.renameTo(new File("list"+count+".txt"));
count++;
}
n/=2;
}
}
Page 45
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Output:
Page 46
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Bruce
Clerk
Clinton
#
Enter the of names in list 8. Enter # to terminate list
Auther
Diana
Barry
Peter
Susan
Edmand
Lucy
#
Page 47
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Scott
Simran
Spock
Steve
Suraj
Susan
Tom
Tony
Vikranth
Vinay
Page 48
B M S INSTITUTE OF TECHNOLOGY AND MANAGEMENT
YELAHANKA BENGALURU – 560064
Merged List
Page 49