HW4 File Input/Output You have been hired by a college to write a program that w
ID: 3560077 • Letter: H
Question
HW4
File Input/Output
You have been hired by a college to write a program that will read in a set of data about students detail from a text file and write that data with an additional information into another text file
Concepts
User Interface
As this is file input output program you are not responsible for a user interface for this assignment.
For this Assignment Following are the steps
Step 1 File Input:
Following is Program specification for input/reading from student.txt
Create a text file named student.txt using notepad or textedit(for mac) with following data
1 Adam 50 50 50
2 Eric 34 28 38
3 Joash 44 48 49
4 Bill 34 30 45
This data contains rollno , name and marks in three subjects (eng, math and science)
read Data from student.txt file
Step 2 File Output
Following is Program specification for writing/output in result.txt
After Reading data from student.txt file you have to write that data into another file named result.txt , while writing data in result.txt you have to add one more data that is Grade (which should be the result of following criteria)
Totalmarks% >=94 A grade
Totalmarks% >=84 B grade
Totalmarks % >=74 C grade
Below 74 D grade
Here totalmarks% you have to calculate from total marks(eng+math+science)
Following is one sample line to show how result.txt should look like after writing into it
1 Adam 50 50 50 A
(same way you will have another three records )
Please note only data is there in file , no need to write heading like rollno,name etc
Documentation and Style
Explanation / Answer
import java.util.*;
import java.io.*;
public class file_operations
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(new File("student.txt"));
PrintWriter printwriter = new PrintWriter(new File("result.txt"));
int roll_no;
String name;
int[] marks=new int[3];
while(in.hasNext()) // read data from file
{
roll_no = in.nextInt();
name = in.next();
for(int i=0; i<3; i++)
marks[i] = in.nextInt();
int Totalmarks = marks[0] + marks[1] + marks[2];
char grade = 'A';
if(Totalmarks >=94) grade = 'A';
else if(Totalmarks >=84) grade = 'B';
else if(Totalmarks >=74) grade = 'C';
else if(Totalmarks <=74) grade = 'D';
printwriter.println(roll_no + " " + name + " " + marks[0] + " " + marks[1] + " " + marks[2] + " " + grade);
}
in.close();
printwriter.close();
} // end main
} // end class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.