Computer Practical 2-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

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 java.util.*;
class Sphenic
{
int n;
Sphenic()
{ n=
0;
}
void accept()
{
Scanner in=new Scanner(System.in);
System.out.println(“Enter a positive Integer”);
n=in.nextInt();
}
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)
{
System.out.println(“Sphenic Number”);
}
else
System.out.println(“Not a Sphenic Number”);
}
static void main()
{
Sphenic obj=new Sphenic();
obj.accept();
obj.check();
}
}

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 java.util.*;
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(System.in);
System.out.print("Enter a Number : ");
int n=sc.nextInt();
if(ob.isComposite(n) == false)
{
System.out.println("You have entered a non-Composite Number. Please enter a
composite number");
}
else
{
int a=ob.sumDig(n);
int b=ob.sumPrimeFact(n);
System.out.println("Sum of Digit = "+a);
System.out.println("Sum of Prime Factor = "+b);
if(a==b)
System.out.print(n+" is a Smith Number");
else
System.out.print(n+" is Not a Smith Number");
System.out.println("");
}
}
}

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 java.util.*;
class Kaprekar_Num
{
int p;
int q;
void input()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter two numbers");
p=in.nextInt();
q=in.nextInt();
if(p>q&&p>5000&&q>5000)
{
System.out.println("ERROR!!");
System.exit(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)Math.pow(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))
System.out.print(i+” “);
}
}
public static void main()
{
Kaprekar_Num obj=new Kaprekar_Num();
obj.input();
obj.display();
}
}

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 java.io.*;
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()
{
System.out.println("Decimal number: " + n);
deciOct();
System.out.println("Octal equivalent: " + oct);
}
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("N = ");
int num = Integer.parseInt(br.readLine());

Mohak Seth | Class Page 9


COMPUTER SCIENCE PRACTICAL

DeciOct obj = new DeciOct();


obj.getNum(num);
obj.show();
}
}

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 java.util.*;
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)
System.out.println(num+" is a perfect number");
else
System.out.println(num+" is not a perfect number");
}
public static void main()
{
Scanner sc=new Scanner(System.in);

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

System.out.println("Enter a number");
int n= sc.nextInt();
Perfect obj=new Perfect(n);
obj.check();
}
}

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 java.util.*;
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)Math.pow((n%10),p) + sumofDigits((n/10), p-1);
}
void check()
{
if(num == sumofDigits(num, size))
System.out.println(num + " is a Disarium Number");
else
System.out.println(num + " is not a Disarium Number");
}
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int n = in.nextInt();
Disarium obj = new Disarium(n);
obj.countDigit();
obj.check();

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 java.io.*;
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(System.in));
System.out.print("First number: ");
int a = Integer.parseInt(br.readLine());
System.out.print("Second number: ");
int b = Integer.parseInt(br.readLine());
int hcf = gcd(a, b);
int lcm = a * b / hcf;
System.out.println("HCF: " + hcf);
System.out.println("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 java.util.*;
public class SwapSort
{
String wrd,swapwrd,sortwrd;
int len;
static Scanner x=new Scanner(System.in);
SwapSort()
{
swapwrd="";
sortwrd="";
}
void readword()
{
System.out.println("Enter word in Upper case");
wrd=x.next();
len=wrd.length();
}
void swapchar()
{
swapwrd=wrd.charAt(len-1) + wrd.substring(1,len-1) + wrd.charAt(0);
}
void sortword()
{
char c;
for(int i=65;i<=90;i++)
{
for(int j=0;j<len;j++)
{
c=wrd.charAt(j);
if(c==i)

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

sortwrd += c;
}
}
}
void display()
{
System.out.println("Original word = " + wrd);
System.out.println("Swapped word = " + swapwrd);
System.out.println("Sorted word = " + sortwrd);
}
static void main()
{
SwapSort x=new SwapSort();
x.readword();
x.swapchar();
x.sortword();
x.display();
}
}

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 java.util.*;
public class Rearrange
{
String wrd,newwrd;
static Scanner x=new Scanner(System.in);
Rearrange()
{
wrd="";
newwrd="";
}
void readword()
{
System.out.println("Enter a word" );
wrd=x.next();
wrd=wrd.toUpperCase();
}
void freq_vow_con()
{
int s=0,s1=0;
char ch;
for(int i=0;i<wrd.length();i++)
{
ch=wrd.charAt(i);
if("AEIOU".indexOf(ch)!=-1)
s++;
}
s1= wrd.length()-s;
System.out.println("Vowels = "+ s);
System.out.println("Consonants = " + s1);
}
void arrange()
{
char ch;
String p="",q="";
for(int i=0;i<wrd.length();i++)
{
ch=wrd.charAt(i);
if("AEIOU".indexOf(ch)!=-1)
p +=ch;
else
q +=ch;

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

}
newwrd= p+q;
}
void display()
{
System.out.println("Original word = "+ wrd);
System.out.println("Rearranged word = "+ newwrd);
}
static void main()
{
Rearrange obj=new Rearrange();
obj.readword();
obj.freq_vow_con();
obj.arrange();
obj.display();
}
}

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 java.util.*;
public class RecursivePalindromeJava
{
public static boolean checkPalindrome(String str)
{
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length() - 1))
return checkPalindrome(str.substring(1, str.length() - 1));
return false;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string : ");
String strInput = sc.nextLine();
if(checkPalindrome(strInput))
{
System.out.println(strInput + " is a palindrome");
}
else
{
System.out.println(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 java.io.*;
class Symmetric
{
public static void main()throws IOException
{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Matrix size: ");
int m = Integer.parseInt(br.readLine());
int ld = 0;
int rd = 0;
boolean flag = true;
if(m <2 || m >= 10)
{
System.out.println("Size out of range!");
return;
}
int a[][] = new int[m][m];
System.out.println("Enter matrix elements:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
a[i][j] = Integer.parseInt(br.readLine());
if(i == j)
ld += a[i][j];
if(i + j == m - 1)
rd += a[i][j];
}
}
System.out.println("Original Matrix:");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
System.out.print(a[i][j] + "\t");
if(a[i][j] != a[j][i])

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

flag = false;
}
System.out.println();
}
if(flag)
System.out.println("The matrix is symmetric.");
else
System.out.println("The matrix is not symmetric.");
System.out.println("Sum of left diagonal elements: " + ld);
System.out.println("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 java.util.*;
class Convert_arr
{
int n,d,m,y;
Convert_arr( )
{ n=
0;
y=0;
}
void accept()
{
Scanner x=new Scanner(System.in) ;
System.out.println(("Enter day number and year")); ;
n=x.nextInt() ;
y=x.nextInt() ;
}
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"};
System.out.print("\n Day Number: " + n);
System.out.print("\n Date: " );
System.out.print(x[m]+" " +d + ", " + y);
System.out.println("");

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

}
static void main()
{
Convert_arr obj =new Convert_arr();
obj.accept( ) ;
obj.day_to_date();
obj.display();
}
}

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 java.util.*;
class InsertionSort
{
void sort(int arr[])
{
int n = arr.length;
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 = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main()
{
Scanner in=new Scanner(System.in);
int arr[] = new int[5];
System.out.println("Enter 5 numbers");
for(int i=0;i<5;i++)
{
arr[i]=in.nextInt();
}
InsertionSort ob = new InsertionSort();
ob.sort(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 (Stud.dat).
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 java.io.*;
import java.util.*;
public class BinFiles
{
String name; int age; double marks;
static Scanner in = new Scanner(System.in);
static Scanner sc = new Scanner(System.in);
void addrecord()throws IOException
{
DataOutputStream out = new DataOutputStream(new
FileOutputStream("Stud.dat",true));
try
{
for(int i = 1 ; i<=4;i++)
{
System.out.println("enter a name, age and marks ");
name = in.nextLine();
age = sc.nextInt();
marks = sc.nextDouble();
out.writeUTF(name);
out.writeInt(age);
out.writeDouble(marks);
}
out.close();
}
catch(IOException e)
{
System.out.print(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("Stud.dat"));
do
{
try
{
name = in.readUTF();
age = in.readInt();
marks = in.readDouble();
//if(marks>=90) System.out.println(name+"\t"+age+"\
t"+marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
in.close();
}
catch(FileNotFoundException ee)
{
System.out.println("File not found");
}
}
void update()throws IOException
{
DataOutputStream out1 = new DataOutputStream(new FileOutputStream("temp.dat"));
String nm;double mm;
System.out.println("Enter name for search");
nm = in.nextLine();
System.out.println("Enter changed marks ");
mm = sc.nextDouble();
boolean eofile = false;
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("Stud.dat"));
do
{
try
{
name = in1.readUTF();

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

age = in1.readInt();
marks = in1.readDouble();
if(name.compareTo(nm)==0)
marks=mm;
out1.writeUTF(name);
out1.writeInt(age);
out1.writeDouble(marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
in1.close();
out1.close();
}
catch(FileNotFoundException eee)
{
System.out.println("File does not exists ");
}
DataInputStream in2 = new DataInputStream(new FileInputStream("temp.dat"));
DataOutputStream out2 = new DataOutputStream(new FileOutputStream("Stud.dat"));
eofile=false;
do
{
try
{
name = in2.readUTF();
age = in2.readInt();
marks = in2.readDouble();
out2.writeUTF(name);
out2.writeInt(age);
out2.writeDouble(marks);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
out2.close();
in2.close();
}
public static void main()throws IOException
{
int ch=0;
BinFiles obj = new BinFiles();
do

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

{
System.out.println("1 . Add records ");
System.out.println("2 . Update records ");
System.out.println("3 . Display records ");
System.out.println("4 . Exit");
System.out.println("Enter your choice ");
ch = sc.nextInt();
switch(ch)
{
case 1:obj.addrecord();
break;
case 2:obj.update();
break;
case 3:obj.print();
break;
case 4:System.exit(0);
break;
default :System.out.println("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 “Stock.dat”.
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 Stock.dat by increasing the
quantity by 10 for all the products. Assume that the file Stock.dat is
already created for you.
Display the updated stock.

import java.io.*;
import java.util.*;
public class BinFiles2
{
long itcode;String itname;double price;int qty;
static Scanner in = new Scanner(System.in);
static Scanner sc = new Scanner(System.in);
void input()throws IOException
{
DataOutputStream out = new DataOutputStream(new
FileOutputStream("Stock.dat",true));
try
{
for(int i = 1 ; i<=2;i++)
{
System.out.println("enter an item Code, item Name, price and quantity ");
itcode = in.nextLong();
itname = sc.nextLine();
price = sc.nextDouble();
qty=sc.nextInt();
out.writeLong(itcode);
out.writeUTF(itname);
out.writeDouble(price);
out.writeInt(qty);
}
out.close();
}
catch(IOException e)
{
System.out.print(e);
}
}
void update()throws IOException
{

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

DataOutputStream out1 = new DataOutputStream(new FileOutputStream("temp2.dat"));


boolean eofile = false;
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("Stock.dat"));
do
{
try
{
itcode= in1.readLong();
itname = in1.readUTF();
price = in1.readDouble();
qty=in1.readInt();
qty=qty+10;
out1.writeLong(itcode);
out1.writeUTF(itname);
out1.writeDouble(price);
out1.writeInt(qty);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
in1.close();
out1.close();
}
catch(FileNotFoundException eee)
{
System.out.println("File does not exists ");
}
DataInputStream in2 = new DataInputStream(new FileInputStream("temp2.dat"));
DataOutputStream out2 = new DataOutputStream(new FileOutputStream("Stock.dat"));
eofile=false;
do
{
try
{
itcode= in2.readLong();
itname = in2.readUTF();
price = in2.readDouble();
qty=in2.readInt();
out2.writeLong(itcode);
out2.writeUTF(itname);
out2.writeDouble(price);
out2.writeInt(qty);
}

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

catch(EOFException e)
{
eofile = true;
}
}
while(!eofile);
out2.close();
in2.close();
}
void print()throws IOException
{
boolean eofile=false;
{
try
{
DataInputStream in1 = new DataInputStream(new FileInputStream("Stock.dat"));
do
{
try
{
itcode= in1.readLong();
itname = in1.readUTF();
price = in1.readDouble();
qty=in1.readInt();
System.out.println(itcode+"\t"+itname+"\t"+price+"\t"+qty);
}
catch(EOFException e)
{
eofile = true;
}
}while(!eofile);
in1.close();
}
catch(FileNotFoundException eee)
{
System.out.println("File does not exist");
}
}
}
public static void main()
{
int ch;
BinFiles2 obj=new BinFiles2();
do
{
System.out.println("1 . Add records ");
System.out.println("2 . Update records ");

Mohak Seth | Class Page


COMPUTER SCIENCE PRACTICAL

System.out.println("3 . Display records ");


System.out.println("4 . Exit");
System.out.println("Enter your choice ");
ch = sc.nextInt();
switch(ch)
{
case 1:obj.input();
break;
case 2:obj.update();
break;
case 3:obj.print();
break;
case 4:System.exit(0);
break;
default :System.out.println("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

You might also like