Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package driver; import db.Statistics; import db.Student; import util.Util; publi

ID: 3801966 • Letter: P

Question

package driver;

import db.Statistics;
import db.Student;
import util.Util;

public class Driver {

   public static void main(String [] args) {
       Student lab2 [] = new Student[40];
       //Populate the student array
       lab2 = Util.readFile("C:\Users\shadowfang\Desktop\StudentObjects\src\StudentData.txt", lab2);
       Statistics stats = new Statistics();
       stats.findlow(lab2);
       //add calls to findhigh and find average
       //Print the data and statistics
//       stats.findlow(lab2);
//       stats.findhigh(lab2);
//       stats.findavg(lab2);
//       System.out.println();
//       stats.print();
       }

}

*****************************************

package util;

import java.io.*;
import java.util.*;

import db.Student;

public class Util {
   public static Student[] readFile(String filename, Student[] stu) {
       // Reads the file and builds student array.  
       try {
           FileReader file = new FileReader(filename);
           BufferedReader buff = new BufferedReader(file);
           int count = 0;

           boolean eof = false;
           while (!eof) {
               String line = buff.readLine();
               if (line == null)
                   break;
               else {
                   System.out.println(line);

                   if (count > 0) {
                      
                       StringTokenizer st = new StringTokenizer(line);
                       for (int i = 0; i < stu.length; i++) {
                           stu[i] = new Student();
                       }
                       stu[count - 1].setSID(Integer.parseInt(st.nextToken()));
                       int scores[] = new int[5];
                       int scoreCount = 0;
                       while (st.hasMoreTokens()) {
                           scores[scoreCount] = Integer.parseInt(st.nextToken());
                           scoreCount++;
                           stu[count - 1].setScores(scores);
                       }
                   }
               }
               count++;
           }
           buff.close();
       } catch (IOException e) {
           System.out.println("Error -- " + e.toString());
       }
       return stu;
   }
}

**********************************

package db;

public class Student {
   private int SID;
   private int[] scores = new int[5];
   private int[] grades;

   // write public get and set methods for
   // SID and scores
   // add methods to print values of instance variables.
   public Student() {

   }

   public int getSID() {
       return SID;
   }

   public void setSID(int sID) {
       SID = sID;
   }

   public int[] getScores() {
       return scores;
   }

   public void setScores(int[] scores) {
       this.scores = scores;
   }

   public int[] getGrades() {
       return grades;
   }

   public void setGrades(int[] grades) {
       this.grades = grades;
   }

   public void recordScores(int i, int scores) {
       for (i = 0; i < 5; i++) {
           this.scores[i] = scores;
       }
   }

   public void printData() { // print all data of a student
       System.out.println(" " + SID + " " + scores[0] + " " + scores[1] + " " + scores[2] + " " + scores[3]
               + " " + scores[4] + " ");
   }
}

*****************************************************

package db;

import java.util.Arrays;

import util.Util;

public class Statistics {
   private int[] lowscores = new int[5];
   private int[] highscores = new int[5];
   private float[] avgscores = new float[5];
   private int tempH = 0;
   public int tempL = 100;
   private float tempA = (float) 0.0;

   /*
   * This method will find the lowest score and store it in an array names
   * lowscores.
   */

   public void findlow(Student[] a) {
       for (int i = 0; i < 5; i++) {
           int[] temp = a[0].getScores();
           tempL = temp[i];
           for (int j = 0; j < 40; j++) {
               int[] row = a[j].getScores();
               if (tempL > row[i]) {
                   tempL = row[i];                  
               }
           }
           lowscores[i] = tempL;
           System.out.println(lowscores[i]);
       }
       for(int i = 0; i < a.length; i++){
           System.out.printf("%s ", lowscores[i]);
       }
   }

   /*
   * This method will find the highest score and store it in an array names
   * highscores.
   */
   public void findhigh(Student[] a) {
       // for (int i = 0; i < 5; i++) {
       // for (int j = 0; j < 5; j++) {
       // if (tempH < a[j].getScores(i)) {
       // tempH = a[j].getScores(i);
       // }
       // }
       // highscores[i] = tempH;
       // }

   }

   /*
   * This method will find avg score for each quiz and store it in an array
   * names avgscores.
   */
   public void findavg(Student[] a) {

   }

   // add methods to print values of instance variables.
   // low
   public void printlow() {
       System.out.printf("Low Score ");
       for (int i = 0; i < 5; i++) {
           System.out.printf("%4s ", lowscores[i]);
       }
       System.out.printf(" ");
   }

   // high
   public void printhigh() {
       System.out.printf("High Score ");
       for (int i = 0; i < 5; i++) {
           System.out.printf("%4s ", highscores[i]);
       }
       System.out.printf(" ");
   }

   // avg
   public void printavg() {
       System.out.printf("Average ");
       for (int i = 0; i < 5; i++) {
           System.out.printf("%3.2f ", avgscores[i]);
       }
       System.out.printf(" ");
   }

   public void print() {
       printlow();
       printhigh();
       printavg();
   }

}

Explanation / Answer

Driver.java

public class Driver
{
public static void main(String [] args)
{
Student lab6 [] = new Student[40];
//Populate the student array
for (int i = 0; i < 40; i++)
{
lab6[i] = new Student();
}
  
lab6 = Util.readFile("quiz.txt", lab6);
System.out.printf("Stud Qu1 Qu2 Qu3 Qu4 Qu5 ");
  
for (int row = 0; row < Util.numOfStudent; row++)
{
Student.printInfo(lab6[row]);
}
  
Statistics statlab6 = new Statistics();
//add calls to findhigh and find average
statlab6.findlow(lab6);
statlab6.findhigh(lab6);
statlab6.findavg(lab6);
//Print the data and statistics
System.out.println();
statlab6.printlow();
statlab6.printhigh();
statlab6.printavg();
}
}

Util.java

import java.io.*;
import java.util.*;

public class Util
{
public static int numOfStudent = 0;
  
static Student [] readFile(String filename, Student [] stu)
{
try
{
//Reads the file and builds student array.
//Open the file using FileReader Object.
FileReader file = new FileReader (filename);
BufferedReader buffer = new BufferedReader (file);
  
int row = 0, col = 0;
String line;
int[] scores = new int[5];
  
//In a loop read a line using readLine method.
while((line = buffer.readLine()) != null)
{
  
//Tokenize each line using StringTokenizer Object
StringTokenizer st = new StringTokenizer(line);
  
while(st.hasMoreTokens())
{
//Each token is converted from String to Integer using parseInt method
if(col == 0)
stu[row].setSID( Integer.parseInt(st.nextToken()));
else
scores[col - 1] = Integer.parseInt(st.nextToken());
  
col++;
stu[row].recordScores(scores);
} //end while tokens
col = 0;
row++;
  
//Value is then saved in the right property of Student Object.
} //end while buffer
countStudents(row);
buffer.close();
}
catch(IOException e)
{
System.out.println("Error: "+e);
} //end catch
return stu;
} //end readFile
  
public static void countStudents( int numOfStudents)
{
numOfStudent = numOfStudents;
}
  
} //end Util

Student.java

public class Student
{
private int SID;
private int scores[] = new int[5];
//write public get and set methods for
//SID and scores
public Student()
{
  
} //default constructor
  
public int getSID()
{
return SID;
} //end getSID
  
public void setSID (int SID)
{
this.SID = SID;
} //end setSID
  
public int getScores(int numOfQuiz)
{
return scores[numOfQuiz];
} //end getScores
  
public void setScores(int [] scores)
{
this.scores = scores;
} //end setScores
  
public void recordScores(int [] scores)
{
for( int i = 0; i < 5; i++)
{
this.scores[i] = scores[i];
} //end for
} //end recordScores
  
//add methods to print values of instance variables.
public static void printInfo(Student stu)
{
System.out.printf("%d ", stu.getSID());
for( int i = 0; i < 5; i++ )
{
System.out.printf("%3d ", stu.getScores(i));
} //end for
System.out.println();
} //end printInfo
} //end class Student

Statistics.java

public class Statistics
{
int [] lowscores = new int [5];
int [] highscores = new int [5];
float [] avgscores = new float [5];
  
int numStudent = Util.numOfStudent;
  
void findlow(Student [] a)
{
//This method will find lowest score and store it in an array names lowscores
int low = 100;
  
for (int col = 0; col < 5; col++)
{
for (int row = 0; row < numStudent; row++)
{
if (low > a[row].getScores(col))
{
low = a[row].getScores(col);
} //end if
} //end inner for
lowscores[col] = low;
low = 100;
} //end outer for
} //end findlow

void findhigh(Student [] a)
{
//This method will find highest score and store it in an array names highscores
int high = 0;
  
for(int col = 0; col < 5; col++)
{
for(int row = 0; row < numStudent; row++)
{
if(high < a[row].getScores(col))
{
high = a[row].getScores(col);
} //end if
} //end inner for
highscores[col] = high;
high = 0;
} //end outer for

}

void findavg(Student [] a)
{
//This method will find avg score for each quiz and store it in an array names avgscores
int sum = 0;
  
for (int col = 0; col < 5; col++)
{
for (int row = 0; row < numStudent; row++)
{
sum += a[row].getScores(col);
} //end inner for
if (numStudent == 0)
avgscores[col] = 0;
else
avgscores[col] = ( float ) sum / numStudent;
  
sum = 0;
} //end outer for
} //end findavg
  
//add methods to print values of instance variables.
public void printlow()
{
System.out.printf("Low Score ");
for(int i = 0; i < 5; i++)
{
System.out.printf("%4d ", lowscores[i]);
} //end for
System.out.println();
} //end printlow
  
public void printhigh()
{
System.out.printf("High Score ");
for (int i = 0; i < 5; i++)
{
System.out.printf("%4d ", highscores[i]);
} //end for
System.out.println();
} //end printhigh
  
public void printavg()
{
System.out.printf("Average ");
for (int i = 0; i < 5; i++)
{
System.out.printf("%4.1f ", avgscores[i]);
} //end for
System.out.println();
} //end printavg
}