Write a program in Java and run it in BlueJ according to the following specifica
ID: 653855 • Letter: W
Question
Write a program in Java and run it in BlueJ according to the following specifications:
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:
John Smith 90 excellent
Barack Obama 95 excellent
Al Clark 80 ok
Sue Taylor 55 failure
Ann Miller 75 ok
George Bush 58 failure
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.3333
There are 2 failre students with average grade 56.5
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.
When you write your program
use proper names for the variables suggesting their purpose.
format your code accordingly using indentation and spacing.
use multiple line comment in the beginning of the code and write your name, e-mail address, class, and section.
for each line of code add a short comment to explain its meaning.
Extra credit (2 points): The program also prints the student with the highest and the student with the lowest grade.
Student.java -
/*
* Reading student records from a file, generating Student objects, counting and averaging
* Suggested exercises:
* - Use grade to determine the type of the student: excellent (> 89), ok [60,89], and failure (< 60)
* - Define an enum type {excellent, ok, failure} and use it to print the student type
* - Do counting and averaging within each student type (excellent, ok, and failure)
* - Count students by using a static variable in class Student
*/
import java.util.Scanner;
import java.io.*;
public class Students
{
public static void main (String[] args) throws IOException
{ String first_name, last_name;
int grade, total=0, count=0;
double average;
Scanner fileInput = new Scanner(new File("students.txt"));
while (fileInput.hasNext())
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
Student st = new Student(first_name, last_name, grade);
System.out.println(st);
total = total + grade;
count++;
}
average = (double)total/count;
System.out.println("There are " + count + " students with average grade " + average);
}
}
Explanation / Answer
Here i am attaching two files.
1. students.java- which prints only the detailes of data in the given file.
2.student.java- which prints the statistics of data in the text file by using methods from this file.
3. Sample output is also given
Proper explanation for each and every code is given as comment. please follow the comment.
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);
}
}
}
3. sample 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.