COMPUTER SCIENCE PRACTICAL
COMPUTER SCIENCE
PRACTICAL
JAVA Programs
By Mohak Seth
[Type text] Page 1
COMPUTER SCIENCE PRACTICAL
INDEX
S. NO./TOPIC PAGE NUMBER
1. LOOPING
a. Sphenic Number..........................................................................3
b. Smith Number....................................................................................5
c. Kaprekar Number........................................................................7
2. RECURSION
a. Decimal Number to Octal Equivalent.................................................9
b. Perfect Number...................................................................................11
c. Disarium Number..........................................................................13
d. Find HCF and LCM.........................................................................15
3. STRINGS
a. SwapSort.............................................................................................16
b. Rearrange…................................................................................18
c. Palindrome…..................................................................................20
4. ARRAYS
a. Symmetric…...............................................................................21
b. Date and Month…..........................................................................23
c. Insertion Sort…...............................................................................25
5. FILE HANDLING
a. Marks Updation.............................................................................27
b. Shopkeeper’s Stock….....................................................................33
Mohak Seth | Class Page 2
COMPUTER SCIENCE PRACTICAL
LOOPING
Question 1
Write a program to check whether a number is a Sphenic number or
not.
A Sphenic Number is a positive integer n which is product of exactly
three distinct prime numbers.
import [Link].*;
class Sphenic
{
int n;
Sphenic()
{ n=
0;
}
void accept()
{
Scanner in=new Scanner([Link]);
[Link](“Enter a positive Integer”);
n=[Link]();
}
int primeFact()
{
int c=0,d=0,i,m=n;
for(i=2;i<=m;)
{
if(m%i==0)
{ c+
+;
m=m/i;
if(c>1)
return 0;
d++;
}
else
{ i+
+;c=0;
}
}
return d;
Mohak Seth | Class Page 3
COMPUTER SCIENCE PRACTICAL
}
void check()
{
if(primeFact()==3)
{
[Link](“Sphenic Number”);
}
else
[Link](“Not a Sphenic Number”);
}
static void main()
{
Sphenic obj=new Sphenic();
[Link]();
[Link]();
}
}
Output
Enter a positive Integer
30
Sphenic Number
Enter a positive Integer
66
Sphenic Number
Enter a positive Integer
60
Not a Sphenic Number
Mohak Seth | Class Page 4
COMPUTER SCIENCE PRACTICAL
Question 2
Write a program to check whether a number is a Smith number or not.
A Smith number is a composite number for which, in a given number
base, the sum of its digits is equal to the sum of the digits in its prime
factorization in the given number base.
import [Link].*;
class Smith
{
int sumDig(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
int sumPrimeFact(int n)
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(i);
n=n/i;
}
else
i++;
}
return sum;
}
boolean isComposite(int n)
{
int c=0;
for(int i=1; i<=n; i++)
{
if(n%i==0)
{ c+
+;
}
}
Mohak Seth | Class Page 5
COMPUTER SCIENCE PRACTICAL
if(c>2)
return true;
else
return false;
}
public static void main()
{
Smith ob=new Smith();
Scanner sc = new Scanner([Link]);
[Link]("Enter a Number : ");
int n=[Link]();
if([Link](n) == false)
{
[Link]("You have entered a non-Composite Number. Please enter a
composite number");
}
else
{
int a=[Link](n);
int b=[Link](n);
[Link]("Sum of Digit = "+a);
[Link]("Sum of Prime Factor = "+b);
if(a==b)
[Link](n+" is a Smith Number");
else
[Link](n+" is Not a Smith Number");
[Link]("");
}
}
}
Output
Enter a Number : 22
Sum of Digit = 4
Sum of Prime Factor = 4
22 is a Smith Number
Enter a Number : 5
You have entered a non-Composite Number. Please enter a composite number
Enter a Number : 99
Sum of Digit = 18
Sum of Prime Factor = 9
99 is Not a Smith Number
Mohak Seth | Class Page 6
COMPUTER SCIENCE PRACTICAL
Question 3
Write a program in JAVA to print all the Kaprekar number between
two limits.
A Kaprekar number is a number whose square when divided into two
parts and such that sum of parts is equal to the original number and
none of the parts has value 0.
import [Link].*;
class Kaprekar_Num
{
int p;
int q;
void input()
{
Scanner in=new Scanner([Link]);
[Link]("Enter two numbers");
p=[Link]();
q=[Link]();
if(p>q&&p>5000&&q>5000)
{
[Link]("ERROR!!");
[Link](0);
}
}
boolean Kaprekar(int n)
{
if(n==1)
return true;
int sq_n=n*n;
int count_digits=0;
while(sq_n!=0)
{
count_digits++;
sq_n=sq_n/10;
}
sq_n=n*n;
for(int i=1;i<count_digits;i++)
{
int e=(int)[Link](10,i);
if(e==n)
continue;
int sum=(sq_n/e)+(sq_n%e);
if(sum==n)
return true;
}
Mohak Seth | Class Page 7
COMPUTER SCIENCE PRACTICAL
return false;
}
void display()
{
for(int i=p;i<=q;i++)
{
if(Kaprekar(i))
[Link](i+” “);
}
}
public static void main()
{
Kaprekar_Num obj=new Kaprekar_Num();
[Link]();
[Link]();
}
}
Output
Enter two numbers
1
150
1 9 45 55 99
Mohak Seth | Class Page 8
COMPUTER SCIENCE PRACTICAL
Recursion
Question 4
Write a program in java to convert a decimal number to its octal
equivalent using recursive technique.
Example
Decimal Number-98
Octal Equivalent-142
import [Link].*;
class DeciOct
{
int n,oct;
DeciOct()
{
n = 0;
oct = 0;
}
void getNum(int num)
{
n = num;
}
void deciOct()
{
if(n > 0)
{
int rem = n % 8;
n /= 8;
deciOct();
oct = oct * 10 + rem;
}
}
void show()
{
[Link]("Decimal number: " + n);
deciOct();
[Link]("Octal equivalent: " + oct);
}
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("N = ");
int num = [Link]([Link]());
Mohak Seth | Class Page 9
COMPUTER SCIENCE PRACTICAL
DeciOct obj = new DeciOct();
[Link](num);
[Link]();
}
}
Output
N = 42
Decimal number: 42
Octal Equivalent: 52
N = 67
Decimal number: 67
Octal Equivalent: 103
N = 18
Decimal number: 18
Octal Equivalent: 22
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 5
Design a class perfect to check if a given number is a perfect number
or not using recursive technique.
A number is said to be perfect if sum of the factors of the number
excluding itself is equal to the original number.
Example: 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding
itself)
import [Link].*;
class Perfect
{
int num;
Perfect(int nn)
{
num=nn;
}
public int sumofFact(int i)
{
int n=num;
if(i>n/2)
return 1;
else
{
if(n %i==0)
return i +sumofFact(i+1);
else
return sumofFact(i+1);
}
}
public void check()
{
int s=sumofFact(2);
if(s==num)
[Link](num+" is a perfect number");
else
[Link](num+" is not a perfect number");
}
public static void main()
{
Scanner sc=new Scanner([Link]);
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
[Link]("Enter a number");
int n= [Link]();
Perfect obj=new Perfect(n);
[Link]();
}
}
Output
Enter a number
6
6 is a perfect number
Enter a number
28
28 is a perfect number
Enter a number
98
98 is not a perfect number
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 6
Enter a positive integer and check whether it’s a Disarium number or
not using recursive technique.
A disarium number is a number in which the sum of the digits to the
power of their respective position is equal to the number itself.
Example: 135 = 11+ 32 + 53 Hence, 135 is a disarium number.
import [Link].*;
class Disarium
{
int num;
int size;
Disarium(int nn)
{
num = nn;
size = 0;
}
void countDigit()
{
int len = (""+num).length();
size = len;
}
int sumofDigits(int n, int p)
{
if(n==0)
return 0;
else
return (int)[Link]((n%10),p) + sumofDigits((n/10), p-1);
}
void check()
{
if(num == sumofDigits(num, size))
[Link](num + " is a Disarium Number");
else
[Link](num + " is not a Disarium Number");
}
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("Enter a number");
int n = [Link]();
Disarium obj = new Disarium(n);
[Link]();
[Link]();
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
}
}
Output
Enter a number
135
135 is a Disarium Number
Enter a number
89
89 is a Disarium Number
Enter a number
518
518 is a Disarium Number
Enter a number
66
66 is not a Disarium Number
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 7
Write a program in Java to find the HCF and LCM of a number using
recursive technique.
Example: 90 and 144
HCF=18
LCM=720
import [Link].*;
class HCFLCM
{
public static int gcd(int p, int q)
{
if(q == 0)
return p;
return gcd(q, p % q);
}
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("First number: ");
int a = [Link]([Link]());
[Link]("Second number: ");
int b = [Link]([Link]());
int hcf = gcd(a, b);
int lcm = a * b / hcf;
[Link]("HCF: " + hcf);
[Link]("LCM: " + lcm);
}
}
Output
First number: 50
Second number: 20
HCF: 10
LCM: 100
First number: 72
Second number: 90
HCF: 18
LCM: 360
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
STRINGS
Question 8
A class SwapSort has been defined to perform string related
operations on a word input.
1) To interchange/swap the first and last characters of the word in
‘wrd’ and stores the new word in ‘swapwrd’
2) Sorts the characters of the original word in alphabetical order and
stores it in ‘sortwrd’.
import [Link].*;
public class SwapSort
{
String wrd,swapwrd,sortwrd;
int len;
static Scanner x=new Scanner([Link]);
SwapSort()
{
swapwrd="";
sortwrd="";
}
void readword()
{
[Link]("Enter word in Upper case");
wrd=[Link]();
len=[Link]();
}
void swapchar()
{
swapwrd=[Link](len-1) + [Link](1,len-1) + [Link](0);
}
void sortword()
{
char c;
for(int i=65;i<=90;i++)
{
for(int j=0;j<len;j++)
{
c=[Link](j);
if(c==i)
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
sortwrd += c;
}
}
}
void display()
{
[Link]("Original word = " + wrd);
[Link]("Swapped word = " + swapwrd);
[Link]("Sorted word = " + sortwrd);
}
static void main()
{
SwapSort x=new SwapSort();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Output
Enter word in Upper case
ANITEJ
Original word = ANITEJ
Swapped word = JETANI
Sorted word = AEIJNT
Enter word in Upper case
COMPUTER
Original word = COMPUTER
Swapped word = RETUPMOC
Sorted word = CEMOPRTU
Enter word in Upper case
SCIENCE
Original word = SCIENCE
Swapped word = ECNEICS
Sorted word = CCEEINS
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 9
A class rearrange has been defined to modify a word by bringing all
the vowels in the word at the beginning followed by the consonants.
Example: ORIGINAL becomes OIIARGNL
import [Link].*;
public class Rearrange
{
String wrd,newwrd;
static Scanner x=new Scanner([Link]);
Rearrange()
{
wrd="";
newwrd="";
}
void readword()
{
[Link]("Enter a word" );
wrd=[Link]();
wrd=[Link]();
}
void freq_vow_con()
{
int s=0,s1=0;
char ch;
for(int i=0;i<[Link]();i++)
{
ch=[Link](i);
if("AEIOU".indexOf(ch)!=-1)
s++;
}
s1= [Link]()-s;
[Link]("Vowels = "+ s);
[Link]("Consonants = " + s1);
}
void arrange()
{
char ch;
String p="",q="";
for(int i=0;i<[Link]();i++)
{
ch=[Link](i);
if("AEIOU".indexOf(ch)!=-1)
p +=ch;
else
q +=ch;
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
}
newwrd= p+q;
}
void display()
{
[Link]("Original word = "+ wrd);
[Link]("Rearranged word = "+ newwrd);
}
static void main()
{
Rearrange obj=new Rearrange();
[Link]();
obj.freq_vow_con();
[Link]();
[Link]();
}
}
Output
Enter a word
ANITEJ
Vowels = 3
Consonants = 3
Original word = ANITEJ
Rearranged word = AIENTJ
Enter a word
COMPUTER
Vowels = 3
Consonants = 5
Original word = COMPUTER
Rearranged word = OUECMPTR
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 10
Write a program in Java to check whether a string is a palindrome or
not using recursive technique.
Example: WOW is a Palindrome while LOW is not a Palindrome.
import [Link].*;
public class RecursivePalindromeJava
{
public static boolean checkPalindrome(String str)
{
if([Link]() == 0 || [Link]() == 1)
return true;
if([Link](0) == [Link]([Link]() - 1))
return checkPalindrome([Link](1, [Link]() - 1));
return false;
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Please enter a string : ");
String strInput = [Link]();
if(checkPalindrome(strInput))
{
[Link](strInput + " is a palindrome");
}
else
{
[Link](strInput + " is not a palindrome");
}
}
}
Output
Please enter a string :
MALAYALAM
MALAYALAM is a palindrome
Please enter a string :
WOW
WOW is a palindrome
Please enter a string :
WALES
WALES is not a palindrome
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Arrays
Question 11
Write a program in java to check if the matrix is symmetric or not.
A symmetric matrix is a square matrix that is equal to its
transpose.
import [Link].*;
class Symmetric
{
public static void main()throws IOException
{
InputStreamReader in = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(in);
[Link]("Matrix size: ");
int m = [Link]([Link]());
int ld = 0;
int rd = 0;
boolean flag = true;
if(m <2 || m >= 10)
{
[Link]("Size out of range!");
return;
}
int a[][] = new int[m][m];
[Link]("Enter matrix elements:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
a[i][j] = [Link]([Link]());
if(i == j)
ld += a[i][j];
if(i + j == m - 1)
rd += a[i][j];
}
}
[Link]("Original Matrix:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
[Link](a[i][j] + "\t");
if(a[i][j] != a[j][i])
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
flag = false;
}
[Link]();
}
if(flag)
[Link]("The matrix is symmetric.");
else
[Link]("The matrix is not symmetric.");
[Link]("Sum of left diagonal elements: " + ld);
[Link]("Sum of right diagonal elements: " + rd);
}
}
Output
Matrix size: 2
Enter matrix elements:
1
2
3
4
Original Matrix:
1 2
3 4
The matrix is not symmetric
Sum of left diagonal elements: 5
Sum of right diagonal elements: 5
Matrix size: 2
Enter matrix elements:
1
2
2
1
Original Matrix:
1 2
2 1
The matrix is symmetric
Sum of left diagonal elements: 2
Sum of right diagonal elements: 4
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 12
Design a class convert to find the date and the month from a given day
number for a particular year.
Example: If day number is 64 and the year is 2020, then the
corresponding date would be:
March 4, 2020 i.e. (31 + 29 + 4 = 64)
import [Link].*;
class Convert_arr
{
int n,d,m,y;
Convert_arr( )
{ n=
0;
y=0;
}
void accept()
{
Scanner x=new Scanner([Link]) ;
[Link](("Enter day number and year")); ;
n=[Link]() ;
y=[Link]() ;
}
void day_to_date()
{
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(y%4==0)
a[2]=29;
int s=0, c=0;
while(s<n)
s=s+a[c++];
s=s-a[--c];
d=n-s;
m=c;
}
void display()
{
String x[]={"","JANUARY","FEBRUARY","MARCH","APRIL","MAY",
"JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
[Link]("\n Day Number: " + n);
[Link]("\n Date: " );
[Link](x[m]+" " +d + ", " + y);
[Link]("");
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
}
static void main()
{
Convert_arr obj =new Convert_arr();
[Link]( ) ;
obj.day_to_date();
[Link]();
}
}
Output
Enter day number and year
343
2020
Day Number: 343
Date: DECEMBER 8, 2020
Enter day number and year
145
2011
Day Number: 145
Date: MAY 25, 2011
Enter day number and year
109
2004
Day Number: 109
Date: APRIL 18, 2004
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 13
Write a program in Java to sort an array of 5 elements in ascending
order using insertion sort technique.
import [Link].*;
class InsertionSort
{
void sort(int arr[])
{
int n = [Link];
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[])
{
int n = [Link];
for (int i = 0; i < n; ++i)
[Link](arr[i] + " ");
[Link]();
}
public static void main()
{
Scanner in=new Scanner([Link]);
int arr[] = new int[5];
[Link]("Enter 5 numbers");
for(int i=0;i<5;i++)
{
arr[i]=[Link]();
}
InsertionSort ob = new InsertionSort();
[Link](arr);
printArray(arr);
}
}
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Output
Enter 5 numbers
01469
Enter 5 numbers
98
78
53
32
1 32 53 78 98
Enter 5 numbers
33
11
48
87
68
11 33 48 68 87
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
File handling
Question 14
Write a program in java to input name, marks and age of a student in a
file ([Link]).
Display the following details. Ask the user to enter the name to be
searched and update its marks by replacing it with another value
entered by the user.
import [Link].*;
import [Link].*;
public class BinFiles
{
String name; int age; double marks;
static Scanner in = new Scanner([Link]);
static Scanner sc = new Scanner([Link]);
void addrecord()throws IOException
{
DataOutputStream out = new DataOutputStream(new
FileOutputStream("[Link]",true));
try
{
for(int i = 1 ; i<=4;i++)
{
[Link]("enter a name, age and marks ");
name = [Link]();
age = [Link]();
marks = [Link]();
[Link](name);
[Link](age);
[Link](marks);
}
[Link]();
}
catch(IOException e)
{
[Link](e);
}
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
}
void print()throws IOException
{
//reading data from the file
boolean eofile = false;
try
{
DataInputStream in = new DataInputStream(new FileInputStream("[Link]"));
do
{
try
{
name = [Link]();
age = [Link]();
marks = [Link]();
//if(marks>=90) [Link](name+"\t"+age+"\
t"+marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
[Link]();
}
catch(FileNotFoundException ee)
{
[Link]("File not found");
}
}
void update()throws IOException
{
DataOutputStream out1 = new DataOutputStream(new FileOutputStream("[Link]"));
String nm;double mm;
[Link]("Enter name for search");
nm = [Link]();
[Link]("Enter changed marks ");
mm = [Link]();
boolean eofile = false;
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("[Link]"));
do
{
try
{
name = [Link]();
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
age = [Link]();
marks = [Link]();
if([Link](nm)==0)
marks=mm;
[Link](name);
[Link](age);
[Link](marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
[Link]();
[Link]();
}
catch(FileNotFoundException eee)
{
[Link]("File does not exists ");
}
DataInputStream in2 = new DataInputStream(new FileInputStream("[Link]"));
DataOutputStream out2 = new DataOutputStream(new FileOutputStream("[Link]"));
eofile=false;
do
{
try
{
name = [Link]();
age = [Link]();
marks = [Link]();
[Link](name);
[Link](age);
[Link](marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
[Link]();
[Link]();
}
public static void main()throws IOException
{
int ch=0;
BinFiles obj = new BinFiles();
do
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
{
[Link]("1 . Add records ");
[Link]("2 . Update records ");
[Link]("3 . Display records ");
[Link]("4 . Exit");
[Link]("Enter your choice ");
ch = [Link]();
switch(ch)
{
case 1:[Link]();
break;
case 2:[Link]();
break;
case 3:[Link]();
break;
case 4:[Link](0);
break;
default :[Link]("Wrong choice entered ");
}
}
while(ch!= 4);
}
}
Output
Enter your choice
Enter a name, age and marks
Jake
25
85
Enter a name, age and marks
Amy
24
100
Enter a name, age and marks
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Terry
30
98
Enter a name, age and marks
Raymond
35
100
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Enter your choice
Jake 25 85.0
Amy 24 100.0
Terry 30 98.0
Raymond 35 100.0
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Enter your choice
Enter name for search
Jake
Enter changed marks
95
1 . Add records
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
2 . Update records
3 . Display records
4 . Exit
Enter your choice
Jake 25 95.0
Amy 24 100.0
Terry 30 98.0
Raymond 35 100.0
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Enter your choice
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
Question 15
A shopkeeper maintains a record of the products sold in the shop on a
computer file “[Link]”.
It contains the following information itcode (ITEM CODE), itname
(ITEM NAME), price (PRICE) and qty (QUANTITY).
Write a program in java to update the file [Link] by increasing the
quantity by 10 for all the products. Assume that the file [Link] is
already created for you.
Display the updated stock.
import [Link].*;
import [Link].*;
public class BinFiles2
{
long itcode;String itname;double price;int qty;
static Scanner in = new Scanner([Link]);
static Scanner sc = new Scanner([Link]);
void input()throws IOException
{
DataOutputStream out = new DataOutputStream(new
FileOutputStream("[Link]",true));
try
{
for(int i = 1 ; i<=2;i++)
{
[Link]("enter an item Code, item Name, price and quantity ");
itcode = [Link]();
itname = [Link]();
price = [Link]();
qty=[Link]();
[Link](itcode);
[Link](itname);
[Link](price);
[Link](qty);
}
[Link]();
}
catch(IOException e)
{
[Link](e);
}
}
void update()throws IOException
{
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
DataOutputStream out1 = new DataOutputStream(new FileOutputStream("[Link]"));
boolean eofile = false;
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("[Link]"));
do
{
try
{
itcode= [Link]();
itname = [Link]();
price = [Link]();
qty=[Link]();
qty=qty+10;
[Link](itcode);
[Link](itname);
[Link](price);
[Link](qty);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
[Link]();
[Link]();
}
catch(FileNotFoundException eee)
{
[Link]("File does not exists ");
}
DataInputStream in2 = new DataInputStream(new FileInputStream("[Link]"));
DataOutputStream out2 = new DataOutputStream(new FileOutputStream("[Link]"));
eofile=false;
do
{
try
{
itcode= [Link]();
itname = [Link]();
price = [Link]();
qty=[Link]();
[Link](itcode);
[Link](itname);
[Link](price);
[Link](qty);
}
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
catch(EOFException e)
{
eofile = true;
}
}
while(!eofile);
[Link]();
[Link]();
}
void print()throws IOException
{
boolean eofile=false;
{
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("[Link]"));
do
{
try
{
itcode= [Link]();
itname = [Link]();
price = [Link]();
qty=[Link]();
[Link](itcode+"\t"+itname+"\t"+price+"\t"+qty);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
[Link]();
}
catch(FileNotFoundException eee)
{
[Link]("File does not exist");
}
}
}
public static void main()
{
int ch;
BinFiles2 obj=new BinFiles2();
do
{
[Link]("1 . Add records ");
[Link]("2 . Update records ");
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
[Link]("3 . Display records ");
[Link]("4 . Exit");
[Link]("Enter your choice ");
ch = [Link]();
switch(ch)
{
case 1:[Link]();
break;
case 2:[Link]();
break;
case 3:[Link]();
break;
case 4:[Link](0);
break;
default :[Link]("Wrong choice entered ");
}
}
while(ch!= 4);
}
}
Output
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Enter your choice
Enter an item code, item name, price and quantity
1102345
CHIPS
20
10
Enter an item code, item name, price and quantity
48190
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
DAIRY MILK
80
22
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Enter your choice
Item Code Item Name Price Quantity
1102345 CHIPS 20 10
48190 DAIRY MILK 80 22
1 . Add records
2 . Update records
3 . Display records
4 . Exit
Enter your choice
Mohak Seth | Class Page
COMPUTER SCIENCE PRACTICAL
THANK
YOU!
Mohak Seth | Class Page