Write a complete program to get data from file name DATA.TXT one line at a time
ID: 3627570 • Letter: W
Question
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 +%40assignments + %10 Participation+ %final
If total >= 90 then A else if total>=80 then B….
You can print same output to screen as well as RESULT.txt.
Thank you!
Explanation / Answer
import java.util.*;
import java.io.*;
public class GradeStudents {
/*
* Given a number as a parameter, return the corresponding letter grade
*/
public static String grade(double total) {
final String [] GRADES = {"A","B","C","D"};
final int [] CUTOFFS = {90,80,70,60};
for (int i = 0; i < CUTOFFS.length; i++) {
if (total > CUTOFFS[i]) return GRADES[i];
}
return "E";
}
public static void main(String[] args) throws Exception {
final String INFILE = "DATA.TXT";
final String OUTFILE = "RESULT.TXT";
Scanner sc = new Scanner(new File(INFILE));
FileWriter fw = new FileWriter(OUTFILE);
PrintWriter pw = new PrintWriter(fw);
while (sc.hasNext()) {
String line = sc.nextLine();
String [] tokens = line.split("\s+"); // split the line up into the strings separated by whitespace
String name = tokens[0];
String ssn = tokens[1];
double quiz = Double.parseDouble(tokens[2]);
double mid = Double.parseDouble(tokens[3]);
double assignments = Double.parseDouble(tokens[4]);
double participation = Double.parseDouble(tokens[5]);
double fin = Double.parseDouble(tokens[6]);
double total = .15*quiz + .15*mid + .40*assignments + .10*participation + .20*fin;
String outputLine = (name+" "+ssn+" "+grade(total));
pw.println(outputLine);
System.out.println(outputLine);
}
pw.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.