Question & Answer: Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students……

JAVA

Assignment 5

————————–

Object Relationship and File IO

Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number.

The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is to be read from a text file. The output from the program should be similar to the following:

Here is some sample data (not to be used) for calculations:

Stud Q1 Q2 Q3 Q4 Q5

1234 78 83 87 91 86

2134 67 77 84 82 79

1852 77 89 93 87 71

High Score 78 89 93 91 86

Low Score 67 77 84 82 71

Average     73.4    83.0    88.2     86.6    78.6

The program should print the lowest and highest scores for each quiz.

Plan of Attack

Learning Objectives

You will apply the following topics in this assignment:

File Input operations.

Working and populating an array of objects.

Wrapper Classes.

Object Oriented Design and Programming.

Understanding Requirements

Here is a copy of actual data to be used for input.

Stud Qu1 Qu2 Qu3 Qu4 Qu5

1234 052 007 100 078 034

2134 090 036 090 077 030

3124 100 045 020 090 070

4532 011 017 081 032 077

5678 020 012 045 078 034

6134 034 080 055 078 045

7874 060 100 056 078 078

8026 070 010 066 078 056

9893 034 009 077 078 020

1947 045 040 088 078 055

2877 055 050 099 078 080

3189 022 070 100 078 077

4602 089 050 091 078 060

5405 011 011 000 078 010

6999 000 098 089 078 020

Essentially, you have to do the following:

Read Student data from a text file.

Compute High, Low and Average for each quiz.

Print the Student data and display statistical information like High/Low/Average..

Design

This program can be written in one class. But dividing the code into simple and modular classes based on functionality, is at the heart of Object Oriented Design.

You must learn the concepts covered in the class and find a way to apply.

Please make sure that you put each class in its own .java file.

package lab2;

class Student {

private int SID;

private int scores[] = new int[5];

//write public get and set methods for

//SID and scores

//add methods to print values of instance variables.

}

/************************************************************************************/

package lab2;

class Statistics

{

int [] lowscores = new int [5];

int [] highscores = new int [5];

float [] avgscores = new float [5];

void findlow(Student [] a) {

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

}

void findhigh(Student [] a) {

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

}

void findavg(Student [] a) {

/* This method will find avg score for each quiz and store it in an array names avgscores. */

}

//add methods to print values of instance variables.

}

************************************************************************************/

package lab2;

class Util {

Student [] readFile(String filename, Student [] stu) {

//Reads the file and builds student array.

//Open the file using FileReader Object.

//In a loop read a line using readLine method.

//Tokenize each line using StringTokenizer Object

//Each token is converted from String to Integer using parseInt method

//Value is then saved in the right property of Student Object.

}

}

************************************************************************************/

//Putting it together in driver class:

public static void main(String [] args) {

Student lab2 [] = new Student[40];

//Populate the student array

lab2 = Util.readFile(“filename.txt”, lab2);

Statistics statlab2 = new Statistics();

statlab2.findlow(lab2);

//add calls to findhigh and find average

//Print the data and statistics

}

Topics to Learn

Working with Text Files

//ReadSource.java — shows how to work with readLine and FileReader

public class ReadSource {

public static void main(String[] arguments) {

try {

FileReader file = new FileReader(“ReadSource.java”);

BufferedReader buff = new BufferedReader(file);

boolean eof = false;

while (!eof) {

String line = buff.readLine();

if (line == null)

eof = true;

else

System.out.println(line);

}

buff.close();

} catch (IOException e) {

System.out.println(“Error — ” + e.toString());

}

}

}

//How do you tokenize a String? You can use other ways of doing this, if you like.

StringTokenizer st = new StringTokenizer(“this is a test”);

while (st.hasMoreTokens()) {

System.out.println(st.nextToken());

}

//How to convert a String to an Integer

int x = Integer.parseInt(String) ;

PreviousNext

————————-

Assignment 6

————————

This assignment is built using the code from Assignment 5.

In this assignment create a separate serialized file using ObjectOutputStream for each student.

File should contain student’s score and statistics for entire class.

Using debug mode (with Debug flag) print the contents of the serialized file.

Create and implement an interface to:

Print student statistics.

Print scores for a given student id.

implement the Debug flag globally

Use an abstract class to implement all the methods declared in an interface.

————————–

Assignment 7

————————–

This assignment is built on top of Assignment 6. Add capability to handle Grade Book for different subjects (Chemistry and Computer Science). We will assume a maximum of five assignments and up to 40 students. Details will be provided in the class.

I need help with Assignment 7 which is built upon Assignment 5 and 6 which I have already done, can anyone help me with adding the gradebook and other subjects to my code?

Expert Answer

 

Driver.java
———————-
public class Driver {
public static void main(String[] args){
Student lab2[] = new Student[40];
DisplayStudent display = new DisplayStudent(“The student information”);

// Reading the record from the .txt file, you can modify the file name,
// The “studentless40.txt” is the records number less than 40
// The “student.txt” is the records number is 40
// The “studentmore40.txt” is the number of record more than 40

lab2 = Util.readFile(“studentless40.txt”, lab2);
// lab2 = Util.readFile(“studentmore40.txt”, lab2);
// lab2 = Util.readFile(“student.txt”, lab2);
Statistics statlab2 = new Statistics();

// finding the highest, lowest and average scores in the records
statlab2.findlow(lab2);
statlab2.findhigh(lab2);
statlab2.findavg(lab2);

// display all information about the student information
display.displayStudentInformation(lab2);
display.printlow(statlab2);
display.printhigh(statlab2);
display.printave(statlab2);
}
}
—————————————————————————–
DisplayStudent.java
———————————
public class DisplayStudent extends Display implements Printstatis{

public DisplayStudent(String title) {
super(title);
// TODO Auto-generated constructor stub
}

// print all student’ID and 5 quiz records.
@Override
public void displayStudentInformation(Student students[]) {
// TODO Auto-generated method stub
System.out.println(“Here is the whole information of students”);
System.out.println(“StudtQu1tQu2tQu3tQu4tQu5t”);
for (int i = 0; i < students.length; i++) {
if (students[i] == null)
break;
System.out.print(students[i].getSID() + “t”);
for (int j = 0; j < students[i].getScores().length; j++)
System.out.print(students[i].getScores()[j] + “t”);
System.out.println();
}
}

// Print the lowest scores for each quiz
@Override
public void printlow(Statistics sta) {
// TODO Auto-generated method stub
System.out.println(“Here are low score about five times quizs”);
for (int i = 0; i < sta.getLowscores().length; i++)
System.out.println(“Qu” + (i + 1) + “: ” + sta.getLowscores()[i]);
}

// Print the highest scores for each quiz
@Override
public void printhigh(Statistics sta) {
// TODO Auto-generated method stub
System.out.println(“Here are highest score about five times quizs”);
for (int i = 0; i < sta.getHighscores().length; i++)
System.out.println(“Qu” + (i + 1) + “: ” + sta.getHighscores()[i]);
}

// Print the average scores for each quiz
@Override
public void printave(Statistics sta) {
// TODO Auto-generated method stub
System.out.println(“Here are Average score about five times quizs”);
for (int i = 0; i < sta.getAvgscores().length; i++)
System.out.println(“Qu” + (i + 1) + “: ” + sta.getAvgscores()[i]);
}

}
——————————————————————
Display.java
—————————–
public abstract class Display {
private String title;

public Display(String title) {
this.title = title;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public void printtitle() {
System.out.println(title);
}

public abstract void displayStudentInformation(Student students[]);
}
————————————————————————–
Overrecordnum.java
——————————-
public class Overrecordnum extends Exception {
private static final long serialVersionUID = -8303213550926146667L;

public Overrecordnum(){
super();
printmyproblem();
}
public Overrecordnum(String message) {
super(message);

}
public void printmyproblem() {
System.out.println(“The number of records is over”);
}
public void fixproblem() {
System.out.println(“We would only take first 40 student records”);
}
}
——————————————————————-
Printstatis.java
————————
public interface Printstatis {
public abstract void printlow(Statistics sta);
public abstract void printhigh(Statistics sta);
public abstract void printave(Statistics sta);
}
———————————————————————-
Statistics.java
————————————–

public class Statistics {
private int[] lowscores = new int[5];        // The lowest score record for each quiz
private int[] highscores = new int[5];        // The highest score record for each quiz
private float[] avgscores = new float[5];    // The average score record for each quiz

public int[] getLowscores() {
return lowscores;
}

public void setLowscores(int[] lowscores) {
this.lowscores = lowscores;
}

public int[] getHighscores() {
return highscores;
}

public void setHighscores(int[] highscores) {
this.highscores = highscores;
}

public float[] getAvgscores() {
return avgscores;
}

public void setAvgscores(float[] avgscores) {
this.avgscores = avgscores;
}

public void findlow(Student[] a) {
/*
* This method will find the lowest score and store it in an array names
* lowscores.
*/

for (int i = 0; i < 5; i++) {
int lowest = 101;
for (int j = 0; j < a.length; j++) {
if (a[j] == null)
continue;
if (a[j].getScores()[i] < lowest) {
lowest = a[j].getScores()[i];
}
}
lowscores[i] = lowest;
}
}

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

public void findavg(Student[] a) {
/*
* This method will find avg score for each quiz and store it in an
* array names avgscores.
*/
for (int i = 0; i < 5; i++) {
int sum = 0;
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == null)
continue;
sum += a[j].getScores()[i];
count++;
}
try {
avgscores[i] = sum / count;
} catch (ArithmeticException e) {
avgscores[i] = 0;
}
}
}
}
————————————————————————–
Student.java
————————–
public class Student {
private int SID;                   // The student ID
private int scores[] = new int[5];   // Five scores record of the student

public Student() {}

public Student(int sID, int[] scores) {
SID = sID;
this.scores = scores;
}

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 void printScores() {
for(int i = 0; i < 5; i++) {
System.out.print(scores[i] + “t”);
}
System.out.println();
}

public void printSID() {
System.out.println(SID);
}
}
———————————————————
Util.java
—————————-
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Util extends DisplayStudent {

public Util(String title) {
super(title);
// TODO Auto-generated constructor stub
}

// Read the student information from a .txt file
public static Student[] readFile(String filename, Student[] stu) {
int linecount = 0;
int studentnum = 0;
try {
FileReader file = new FileReader(filename);
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else {
linecount++;
if (linecount == 1)
continue;
// throw the exception when the records is over 40
if (linecount > 41)
throw new Overrecordnum();
StringTokenizer st = new StringTokenizer(line);
Student s = new Student();
boolean checkSID = true;
int quiznum = 0;
int[] scores = new int[5];
while (st.hasMoreTokens()) {
if(checkSID) {
int id = Integer.parseInt(st.nextToken());
s.setSID(id);
checkSID = false;
}
else {
scores[quiznum++] = Integer.parseInt(st.nextToken());
}
}
s.setScores(scores);
stu[studentnum++] = s;
if(stu[0] == null) {
System.out.println(“There is no record”);
int[] zeroscore = new int[]{0,0,0,0,0};
stu[0].setSID(0);
stu[0].setScores(zeroscore);

}
}
}
buff.close();
} catch (Overrecordnum e){
e.fixproblem();

} catch (IOException e) {
System.out.println(“Error ­­ ” + e.toString());
}

return stu;
}
}
—————————————————————
studentless40.txt
———————–
Stud Qu1 Qu2 Qu3 Qu4 Qu5
1234 052 007 100 078 034
2134 090 036 090 077 030
3124 100 045 020 090 070
1234 052 007 100 078 034

Still stressed from student homework?
Get quality assistance from academic writers!