I need to write a program that takes the a text file and does the following: The
ID: 3819660 • Letter: I
Question
I need to write a program that takes the a text file and does the following:
The program reads a text file with student records (first name, last name and grade) and prints them in the terminal window.
After the name and grade it also prints the type of the grade: excellent (> 89), ok [60,89], and failure (< 60).
Then the program prints the number of students, and their average grade and also the number of students and their average in each grade category (excellent, ok, and failure).
For example, with the input file students.txt the program must print the following:
Requirements and restrictions
Use the Student.java and Students.java classes from the course website to represent and process student records and modify them accordingly:
The encapsulation principle must be strictly enforced.
The main method in class Students should only read the text file, and print student records (student names, grade and grade type) and statistics (number of students and averages) using methods of class Student.
All counting, totaling, computing averagas, checking grade intervals, and assigning grade types to students should be implemented in class Student. Hints: use static variables in class Student and add methods to compute and return (or print) averages. Modify the toString() method to return the grade type too.
Heres what I have so far:
public class Student
{
private String fname, lname;
private int grade;
public Student(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}
public String toString()
{
return fname + " " + lname + " " + grade;
}
}
Explanation / Answer
I HAVE DONE THIS IN ECLIPSE, I PUT SOME COMMENTS FOR BETTER UNDERSTANDING.
HERE IS THE CODE:
//1. students.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class students {
public static void main(String args[]) throws FileNotFoundException {
//creating File instance to reference text file in Java
File text = new File("D:/document.txt"); //specify your file location
//Creating Scanner instnace to read File in Jav
Scanner scnr = new Scanner(text);
int lineNumber = 1;
int count = 0;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
count++;
}
student student1=new student();// creating object to access the class student.
student1.average_mark(text); // call for average grades of all students.
student1.exellent_students(text);// call for exellent student calculation.
student1.ok_students(text);// call for finding ok students.
student1.failure_students(text);// call for finding failure students.
}
}
//2. student.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class student{
//method to find the average of grades. We havn't defined any limkitation for arrays.
//If necessary you can limit the value of array size.
public static void average_mark(File text) throws FileNotFoundException
{
Scanner scnr = new Scanner(text);
int count = 0;
float total_mark=0;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
// Splitting the words from text file and loading to array
String[] strArray=line.split(" ");
// Calculating total marks based on our requirement.
total_mark=total_mark+Integer.parseInt(strArray[2]);// finding total marks of all students
count++;
}
if(count==0)
{
System.out.println("The file is empty");
}
else{
double average=total_mark/count;
System.out.println("There are "+count+" students with average grade "+ average);
}
}
public static void exellent_students(File text) throws FileNotFoundException
{
Scanner scnr = new Scanner(text);
int count=0;
float total_mark=0;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
// Splitting the words from text file and loading to array
String[] strArray=line.split(" ");
String status="excellent";
if(strArray[3].equals(status))
{
// Calculating total marks based on our requirement.
total_mark=total_mark+Integer.parseInt(strArray[2]);
count++;
}
}
if(count==0)
{
System.out.println("There are no 'Excellent' students");
}
else{
float average1=total_mark/count;
System.out.println("There are "+count+" excellent students with average grade "+ average1);
}
}
public void ok_students(File text) throws FileNotFoundException {
{
Scanner scnr = new Scanner(text);
int count=0;
float total_mark=0;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
// Splitting the words from text file and loading to array
String[] strArray=line.split(" ");
String status="ok";
if(strArray[3].equals(status))
{
// Calculating total marks based on our requirement.
total_mark=total_mark+Integer.parseInt(strArray[2]);
count++;
}
}
if(count==0)
{
System.out.println("There are no 'Ok' students");
}
else{
double average2=total_mark/count;
System.out.println("There are "+count+" ok students with average grade "+ average2);
}
}
}
void failure_students(File text) throws FileNotFoundException
{
Scanner scnr = new Scanner(text);
int count=0;
float total_mark=0;
while(scnr.hasNextLine()){
String line = scnr.nextLine();
// Splitting the words from text file and loading to array
String[] strArray=line.split(" ");
String status="failure";
if(strArray[3].equals(status))
{
// Calculating total marks based on our requirement.
total_mark=total_mark+Integer.parseInt(strArray[2]);
count++;
}
}
if(count==0)
{
System.out.println("There are no 'Failure' students");
}
else{
double average3=total_mark/count;
System.out.println("There are "+count+" failure students with average grade "+ average3);
}
}
}
OUTPUT:
1 :John Smith 90 excellent
2 :Barack Obama 95 excellent
3 :Al Clark 80 ok
4 :Sue Taylor 55 failure
5 :Ann Miller 75 ok
6 :George Bush 58 failure
7 :John Miller 65 ok
There are 7 students with average grade 74.0
There are 2 excellent students with average grade 92.5
There are 3 ok students with average grade 73.33
There are 2 failure students with average grade 56.5