create a program which will do the following: Read from a file a student\'s firs
ID: 669218 • Letter: C
Question
create a program which will do the following:
Read from a file a student's first name, last name, full name (with spaces), TNumber, Major, Class Name, 3 exam grades, and 5 homework grades. All grades should be in the 0 to 100 range.
Determine the average of the exam grades and homework grades. Then, determine the overall average for the student with a weighted average. Exams are worth 60% (20% each) and homework is worth 40%.
Output all of the student's information, grades and the student's averages (exam average, homework average, and course average) to both the screen and to a file.
Format all numeric values to 2 decimal places.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Student {
public static void main(String[] args) throws Exception{
//the contents in the file are in the following format each separated by '#'
//first name, last name, full name (with spaces), TNumber, Major, Class Name, 3 exam grades, and 5 homework grades.
//change delimiter as per ur need
final String delimiter = "#";
BufferedReader br = null;
String sDetails = "";
try {
String fname;
String lname;
String fullname;
int tnumber;
String major;
String className;
double examGrades[] = new double[]{ 0,0,0};
double hwGrades[] = new double[]{ 0,0,0};
String sCurrentLine;
br = new BufferedReader(new FileReader("input.txt"));
sCurrentLine = br.readLine();
if (sCurrentLine != null) {
System.out.println(sCurrentLine);
String[] fields = sCurrentLine.split(delimiter);
if(fields != null && fields.length >0) {
fname = fields[0];
lname = fields[1];
fullname = fields[2];
tnumber = Integer.parseInt(fields[3]);
major = fields[4];
className = fields[5];
for(int i =0; i < 3; i++)
examGrades[i] = Double.parseDouble(fields[6+i]);
for(int i =0; i < 5; i++)
hwGrades[i] = Double.parseDouble(fields[9+i]);
//calculate exam Grades average
double examGrTotal = (examGrades[0]+examGrades[1]+examGrades[2]);
double examGrAvg = examGrTotal/3.0;
double hwGrTotal = (hwGrades[0]+hwGrades[1]+hwGrades[2]+hwGrades[3]+hwGrades[4]);
double hwGrAvg = hwGrTotal/5.0;
//Exams are worth 60% (20% each) and homework is worth 40% - total 8 grades
double courseAvg = ((examGrTotal * 60/100)+(hwGrTotal * 40))/8;
//print student details
System.out.println(" Student details : ");
System.out.println(" Firstname : "+fname+" Last Name: "+lname+" Full Name: "+fullname);
System.out.println(" TNumber : "+tnumber+" Major: "+major+" Class Name : "+className);
System.out.println(" 3 exam grades: "+examGrades[0]+ ","+examGrades[1]+","+examGrades[2]);
System.out.println(" 5 homework grades "+hwGrades[0]+ ","+hwGrades[1]+ ","+hwGrades[2]+ ","+hwGrades[3]+ ","+hwGrades[4]);
System.out.println(" Averages : ");
System.out.println(" Exams Grades Avg: "+examGrAvg+" Homework Grades Avg :"+hwGrAvg+ " Course Avg: "+courseAvg);
sDetails = (" Firstname : "+fname+" Last Name: "+lname+" Full Name: "+fullname);
sDetails += (" TNumber : "+tnumber+" Major: "+major+" Class Name : "+className);
sDetails += (" 3 exam grades: "+examGrades[0]+ ","+examGrades[1]+","+examGrades[2]);
sDetails += (" 5 homework grades "+hwGrades[0]+ ","+hwGrades[1]+ ","+hwGrades[2]+ ","+hwGrades[3]+ ","+hwGrades[4]);
sDetails +=(" Averages : ");
sDetails +=(" Exams Grades Avg: "+examGrAvg+" Homework Grades Avg :"+hwGrAvg+ " Course Avg: "+courseAvg);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
//write to file
if(sDetails != null && !sDetails.isEmpty()) {
try {
File file = new File("output.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(sDetails);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.