Respuesta :
Answer:
package driverstudentquiz;
public class Student {
private int SID;
private int scores[] = new int[5];
//get and set functions for scores and ID
public int getSID(){
return this.SID;
}
public void setSID(int id){
this.SID= id;
}
public int[] getScores(){
return this.scores;
}
public void setScores(int[] s){
this.scores= s;
}
public void printData(){ //print all data of a student
System.out.println("\t\t\t\t"+ SID +"\t"+ scores[0] +"\t"+ scores[1] +"\t"+ scores[2] + "\t"+ scores[3] +"\t"+ scores[4] +"\n");
}
}
Statistics class :
package driverstudentquiz;
public class Statistics {
private int[] lowscores= new int[5];
private int[] highscores= new int[5];
private float[] avgscores= new float[5];
void findlow(Student[] a, int c){ // finds low score.
int i=0;
for(i=0;i<5; i++){
int[] temp= a[0].getScores();
int min = temp[i];
for(int j=0; j< c; j++){
int[] s= a[j].getScores();
if(min > s[i]){
min = s[i];
}
}
lowscores[i]= min;
}
}
void findhigh(Student[] a, int c){ // find high score
int i=0;
for(i=0;i<5; i++){
int[] temp= a[0].getScores();
int max = temp[i];
for(int j=0; j< c; j++){
int[] s= a[j].getScores();
if(max < s[i]){
max = s[i];
}
}
highscores[i]= max;
}
}
void findavg(Student[] a, int c){ // find average score.
int i=0;
for(i=0;i<5; i++){
int sum =0;
for(int j=0; j<c; j++){
int[] s= a[j].getScores();
sum= sum + s[i];
}
avgscores[i]= sum/c;
}
}
public void printStatistics(Student[] a, int c){ // print statistics for all students and all quizes
findlow(a,c);
findhigh(a,c);
findavg(a,c);
System.out.println("Lowest score :\t\t"+ lowscores[0] +"\t"+ lowscores[1] +"\t"+ lowscores[2] +"\t"+ lowscores[3] +"\t"+ lowscores[4] );
System.out.println("High score :\t\t"+ highscores[0] +"\t"+ highscores[1] +"\t"+ highscores[2] +"\t"+ highscores[3] +"\t"+ highscores[4] );
System.out.println("Average score :\t\t"+ avgscores[0] +"\t"+ avgscores[1] +"\t"+ avgscores[2] +"\t"+ avgscores[3] +"\t"+ avgscores[4] );
}
}
Util class :
package driverstudentquiz;
import java.io.*;
import java.util.StringTokenizer;
public class Util {
static int studentCount;
static Student [] readFile(String filename, Student [] stu) {
//Reads the file and builds student array.
int i=0;
try {
FileReader file = new FileReader(filename); //Open the file using FileReader Object.
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while (!eof) {
String line = buff.readLine(); //In a loop read a line using readLine method.
if (line == null)
eof = true;
else{
// System.out.println(line);
stu[i]= new Student();
StringTokenizer st = new StringTokenizer(line); //Tokenize each line using StringTokenizer Object
while(st.hasMoreTokens()){
stu[i].setSID(Integer.parseInt(st.nextToken())); //Value is then saved in the right property of Student Object.
int[] arr= new int[5];
for(int j=0;j<5;j++){
arr[j]= Integer.parseInt(st.nextToken()); //Each token is converted from String to Integer using parseInt method
}
stu[i].setScores(arr); //Value is then saved in the right property of Student Object.
}
}
i++;
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
studentCount = i-1;
return stu;
}
}
package driverstudentquiz;
public class DriverStudentQuiz {
public static void main(String[] args) {
Student arrStudent[] = new Student[40];
int studentCount=0;
arrStudent = Util.readFile("Data.txt", arrStudent);
studentCount= Util.studentCount; // find number of lines from file. which will show number of students in array.
Statistics s = new Statistics();
for(int i = 0; i<studentCount; i++){ // print student data fetched from file
arrStudent[i].printData();
}
s.printStatistics(arrStudent,studentCount); // print statistics of students
}
}
Explanation: