To be done in Java: Write a program that • creates three text files: grade1.txt,
ID: 3776192 • Letter: T
Question
To be done in Java:
Write a program that • creates three text files: grade1.txt, grade2.txt, grade3.txt
o Each contains 5 students’ names (first name, last name) and grades (integers) that are obtained from the keyboard input.
• combine all three files into one text file: allgrades.txt o It contains all 15 students' names and grades from the 3 text file. o It also contains letter grades for each student
If grade < 60, it adds letter grade "F"
If 60 <= grade <70, it adds letter grade "D".
If 70 <= grade < 80, it adds letter grade "C".
If 80 <= grade < 90, it adds letter grade "B".
If 90 <= grade, it adds letter grade "A". • displays the contents of text file allgrade.txt on the console
Explanation / Answer
SOURCE CODE:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class grades {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
File f1=new File("grades1.txt");
File f2=new File("grades2.txt");
File f3=new File("grades3.txt");
File f4=new File("allgrades.txt");
f1.createNewFile();
f2.createNewFile();
f3.createNewFile();
f4.createNewFile();
FileWriter fw1=new FileWriter(f1);
FileWriter fw2=new FileWriter(f2);
FileWriter fw3=new FileWriter(f3);
FileWriter fw4=new FileWriter(f4);
//For file grades1.txt
for(int i=0;i<5;i++)
{
String grade="";
System.out.println("Enter the name of student: ");
String name=br.readLine();
System.out.println("Enter the grade of the student: ");
int gr=Integer.parseInt(br.readLine());
fw1.write(name+" "+gr+" ");
if(gr>=90)
grade="A";
else if(gr>=80 && gr<90)
grade="B";
else if(gr>=70 && gr<80)
grade="C";
else if(gr>=60 && gr<70)
grade="D";
else if(gr<60)
grade="F";
fw4.write(name+" "+gr+" "+grade+" ");
}
//For file grades.txt
for(int i=0;i<5;i++)
{
String grade="";
System.out.println("Enter the name of student: ");
String name=br.readLine();
System.out.println("Enter the grade of the student: ");
int gr=Integer.parseInt(br.readLine());
fw2.write(name+" "+gr);
if(gr>=90)
grade="A";
else if(gr>=80 && gr<90)
grade="B";
else if(gr>=70 && gr<80)
grade="C";
else if(gr>=60 && gr<70)
grade="D";
else if(gr<60)
grade="F";
fw4.write(name+" "+gr+" "+grade+" ");
}
//For file grades.txt
for(int i=0;i<5;i++)
{
String grade="";
System.out.println("Enter the name of student: ");
String name=br.readLine();
System.out.println("Enter the grade of the student: ");
int gr=Integer.parseInt(br.readLine());
fw3.write(name+" "+gr+" ");
if(gr>=90)
grade="A";
else if(gr>=80 && gr<90)
grade="B";
else if(gr>=70 && gr<80)
grade="C";
else if(gr>=60 && gr<70)
grade="D";
else if(gr<60)
grade="F";
fw4.write(name+" "+gr+" "+grade+" ");
}
fw1.close();
fw2.close();
fw3.close();
fw4.close();
}
}
SAMPLE OUTPUT:
Enter the name of student:
Prantik Panja
Enter the grade of the student:
90
Enter the name of student:
Alik Pramanick
Enter the grade of the student:
100
Enter the name of student:
Soubhik Biswas
Enter the grade of the student:
90
Enter the name of student:
Shourjya Ghosh
Enter the grade of the student:
91
Enter the name of student:
Vishal Singh
Enter the grade of the student:
89
Enter the name of student:
Koushambi Bhowmick
Enter the grade of the student:
87
Enter the name of student:
Tandrima Bhattacharrya
Enter the grade of the student:
84
Enter the name of student:
Akash Ghosh
Enter the grade of the student:
93
Enter the name of student:
Arka Ghosh
Enter the grade of the student:
89
Enter the name of student:
Akisha
Enter the grade of the student:
78
Enter the name of student:
Niyati Sinha
Enter the grade of the student:
80
Enter the name of student:
Debasmita Podder
Enter the grade of the student:
95
Enter the name of student:
Krisnendu Palit
Enter the grade of the student:
83
Enter the name of student:
Ayontirtha Das
Enter the grade of the student:
73
Enter the name of student:
Irfan Hussain
Enter the grade of the student:
67
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.