In JAVA please Objective: File Input output and flow of control. USING ARRAYS is
ID: 3742463 • Letter: I
Question
In JAVA please Objective:
File Input output and flow of control. USING ARRAYS is not required for this assignment.
Description: The program should first input the number of scores (that will be a fixed number for all students). Your program should also input the name of the file containing the scores of students.
The sample input file of student scores looks like this:
15 51 55 50 52
25 80 -1 70 -1
23 53 -1 57 40
21 50 -1 49 50
Each line represents one student. The first column is the student ID and the remaining columns are scores made by the student.
Two example files - “student1.txt” and “student2.txt” are given, -1 represents an excused score.
Your program should read the scores of each student and compute the average score for each student and output the grade of S(satisfactory) or U(unsatisfactory) or I (incomplete). -1 represents an excused score. If a student has 2 or more excused absences, then do not compute the average and give the student a letter grade of I. If the student has less than 2 excused absences then compute the average and determine the grade. Of course for a student with 1 excused absence the average should not count the –1 score. For example, if the input scores for a student were 53, -1, 49 and 50, your program would ignore the -1, since this is the student’s only absence. For this student average will be (53+49+50)/3. The letter grade of S should be assigned if the student's average is 50.0 or above and letter grade U if it is below 50.0.
At the end of the program the program should print out a summary of how many students made S, U and I. NOTE: YOUR PROGRAM SHOULD WORK FOR ANY NUMBER OF STUDENTS IN THE FILE.
Input: the input user should enter the name of the file and the number of scores for the given semester.
Below is a sample I/O. Only the bolded words are the inputs provided by the user.
Enter the name of the file that has the scores of students: student1.txt
Enter the number of scores for each student: 4
Output: the program should write student ID and grade to an output file. The program should also print summary of how many students made S, U and I to the output file. Let’s say if the input file of student scores looks like this:
15 51 55 50 52
25 80 -1 70 -1
23 53 -1 57 40
21 50 -1 49 50
The output file should look like this
ID=15 Avg=52 Grade=S
ID=25 Excused=2 Grade=I
ID=23 Avg=50.0 Grade=S
ID=21 Avg=49.7 Grade=U
Number of Grades of Type
S U I
2 1 1
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class StudentScore {
public static void main(String [] args){
String fileName ="";
String outfileName ="outputScore.txt";
String numberOfScoresStr ="";
int noOfScores = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the file that has the scores of students: ");
fileName = scan.nextLine();
System.out.println("Enter the number of scores for each student: ");
numberOfScoresStr = scan.nextLine();
noOfScores = Integer.valueOf(numberOfScoresStr);
//Reading the file
BufferedReader br = null;
BufferedWriter bw =null;
FileReader fr = null;
FileWriter fw = null;
String studentId="";
boolean isIDSet = false;
int scoresSum = 0;
int excuseCount = 0;
int scoreCount = 0;
double average =0;
String grade = "";
int gradeSCount = 0;
int gradeICount = 0;
int gradeUCount = 0;
try {
fw = new FileWriter(outfileName);
bw = new BufferedWriter(fw);
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
isIDSet = false;
scoresSum = 0;
excuseCount =0 ;
scoreCount = 0;
scoresSum = 0;
//reading each string from the lines
StringTokenizer st = new StringTokenizer(sCurrentLine);
while (st.hasMoreTokens()) {
String currentElement = st.nextToken() ;
if(!isIDSet){
studentId= currentElement;
isIDSet =true;
}
else if("-1".equalsIgnoreCase(currentElement)){
excuseCount= excuseCount + 1;
}
else{
scoreCount = scoreCount +1;
scoresSum = scoresSum + Integer.valueOf(currentElement);
}
}
//calculating average
if(excuseCount >= 2 ){
grade = "I";
gradeICount = gradeICount + 1;
}
else if(noOfScores >= scoreCount && excuseCount<=1 ){
average = scoresSum / scoreCount;
if(average >= 50.0){
grade = "S";
gradeSCount =gradeSCount +1;
}
else{
grade = "U";
gradeUCount = gradeUCount + 1;
}
}
//Writing output to file
String content ="ID="+studentId+" Avg="+average+" Grade="+grade;
bw.write(content);
String content1 = "This is the content to writessss into file ";
fw = new FileWriter("outputScore.txt");
bw = new BufferedWriter(fw);
bw.write(content1);
}
System.out.println("Number of Grades of Type ");
System.out.println(" S U I");
System.out.println(" "+gradeSCount +" "+gradeUCount+" "+gradeICount);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.