IN JAVA PROGRAMMING!! Write a complete program to get data from file name DATA.T
ID: 3776010 • Letter: I
Question
IN JAVA PROGRAMMING!!
Write a complete program to get data from file name DATA.TXT one line at a time until there is no more data in that file. The following is one sample line in DATA.TXT ( have as many record as you wish in DATA.TXT) Name SSN quiz mid assignments participation final LISA 111-11-1111 100 100 100 100 100 Jack 222-22-2222 80 80 100 90 100 Note that the first line is no in DATA.txt. Your program should create a file name RESULT.txt that reports Name, SSN and a letter grade according to the following rules: Total= 15% quiz + 15% mid +40%assignments + 10% Participation+ 20%final If total >= 90 then A else if total>=80 then B…. You can print same output to screen as well as RESULT.txt
Explanation / Answer
Please follow the code and comments for description :
CODE :
import java.io.BufferedReader; // required imports for the code
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class GradesCalc { // class to run the code
public static void main(String[] args) throws FileNotFoundException, IOException { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data
String inpFile, name, SSN, grade, line, outFile; // required initialisations
int quiz, mid, assgn, participation, finalMarks;
double totalMarks;
System.out.println("Please Enter the File to read the Data : "); // prompt for the user
inpFile = sc.nextLine(); // get the data
System.out.println("Please Enter the File to write the Data : "); // prompt for the user
outFile = sc.nextLine(); // get the data
File file = new File(outFile); // create a file object
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw); // get the writer object
try (BufferedReader br = new BufferedReader(new FileReader(inpFile))) { // open the file using the reader
while ((line = br.readLine()) != null) { // check for the condition
StringTokenizer st = new StringTokenizer(line, " "); // tokenise the data
name = st.nextToken(); // get the respective data
SSN = st.nextToken();
quiz = Integer.parseInt(st.nextToken());
mid = Integer.parseInt(st.nextToken());
assgn = Integer.parseInt(st.nextToken());
participation = Integer.parseInt(st.nextToken());
finalMarks = Integer.parseInt(st.nextToken());
totalMarks = (quiz * 0.15) + (mid * 0.15) + (assgn * 0.40) + (participation * 0.10) + (finalMarks * 0.20); // calculate the total
if (totalMarks >= 90) { // check for the total and assign the grades
grade = "A";
} else if (totalMarks >= 80 && totalMarks < 90) {
grade = "B";
} else if (totalMarks >= 70 && totalMarks < 80) {
grade = "C";
} else if (totalMarks >= 60 && totalMarks < 70) {
grade = "D";
} else if (totalMarks >= 50 && totalMarks < 60) {
grade = "E";
} else {
grade = "F";
}
System.out.println(name + " " + SSN + " " + grade + " "); // print the data to console
bw.write(name + " " + SSN + " " + grade + " "); // print the data to console
bw.flush(); // flush the data to file
}
}
bw.close(); // close the writer object
}
}
OUTPUT :
Please Enter the File to read the Data :
data.txt
Please Enter the File to write the Data :
result.txt
Jack 222-22-2222 A
LISA 111-11-1111 A
Thomas 333-33-3333 E
Newton 444-44-4444 B
Michael 555-55-5555 C
data.txt :
Jack 222-22-2222 80 80 100 90 100
LISA 111-11-1111 100 100 100 100 100
Thomas 333-33-3333 75 65 25 43 86
Newton 444-44-4444 64 82 98 35 99
Michael 555-55-5555 86 87 75 56 68
result.txt :
Jack 222-22-2222 A
LISA 111-11-1111 A
Thomas 333-33-3333 E
Newton 444-44-4444 B
Michael 555-55-5555 C
Hope this is helpful.