Page 364 - Cs_withBlue_J_C11_Flipbook
P. 364
Define a class called Result to perform read and write operations on a binary file “marks.dat”.
Program 3
The detail of the class is given as follows:
Data Members
int roll : To store roll number
String name : To store name of the student
double eng, maths, sci : To store marks in English, Maths and Science respectively
Member Methods
void createFile() : Accepts roll, name and marks in three subjects and stores it in
binary file “marks.dat”
void printAvg() : Reads binary file “marks.dat” and prints average marks obtained
by each student
static void main() : Creates object and executes other methods
1 import java.io.*;
2 import java.util.*;
3 class Result
4 {
5 int roll;
6 String name;
7 double eng, maths, sci;
8 void createFile() throws IOException
9 {
10 // declaring the classes required to create file
11 // parameter true opens file in append mode
12 FileOutputStream fo=new FileOutputStream("Marks.dat", true);
13 DataOutputStream dou= new DataOutputStream(fo);
14 Scanner sc=new Scanner(System.in);
15 char ch='y';
16 while(ch=='y' || ch=='Y') // loop to store multiple records
17 {
18 System.out.println("Enter roll no, name, marks in English, Maths and
Science");
19 // entering data using Scanner class
20 roll=sc.nextInt();
21 name=sc.next();
22 eng=sc.nextDouble();
23 maths=sc.nextDouble();
24 sci=sc.nextDouble();
25 // writing data to buffer using specific write methods of data type
362362 Touchpad Computer Science-XI

