This project is to keep records and perform analysis for a CSC 20 class of stude
ID: 3720540 • Letter: T
Question
This project is to keep records and perform analysis for a CSC 20 class of students. Our Spring 2018 CSC 20 lab may have up to 17 students. However, please make a note
that this number can be changed since there are more students to be added to the course. There are five labs (assumed we have five) during the section. Each student is identified by a four - digit CSUS student ID number.
The program is to output the studen t scores and calculate and print the statistics for each lab. 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 (for illustration only):
Stud L1 L2 L3 L4 L5
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
** Use one and two - dimensional arrays only. Test your program with the following data - Program should print all the lowest or highest scores for each lab. Here is copy of
actual data to be used for input.
Stud L1 L2 L3 L4 L5
1234 032 017 020 028 034
2134 030 036 030 017 030
3124 030 035 020 030 030
4532 031 017 031 032 027
5678 040 012 035 028 034
6134 034 040 035 018 025
7874 030 030 036 038 018
2877 035 030 019 022 030
3189 022 030 020 018 017
4602 039 040 021 038 016
5405 011 011 020 021 010
6999 022 028 029 011 020
8026 040 010 026 028 016
9893 024 009 017 027 020
1947 025 020 028 023 035
These Concepts MAY apply to this project;
1. Object Oriented Programming.
2. File IO.
3. Wrapper Classes.
Essentially you have to do the following:
(1) Read Student data from a formatted file.
(2) Compute High, Low and Average for each lab score.
(3) Print the student data and statistical information.
Designing:
This program can be written in one class. However, understanding division of responsibilities in each entity and linking them is at the heart of Object Oriented Design. Observe the classes you are being asked to create and analyze what you have learned from this design. Keep in mind for following:
A clear demonstration through your implementation that you understand why Abstract classes and Interfaces exist. Finding opportunities to use Abstract Classes and Interfaces.
Put each class in its own .java file.
Code Snippets:
The following code is only one of the design. There are other design options.
Assume you declared:
final int NUMBER_OF_CSC20_LABS = 5;
(note: You might choose to implement this assignment using ArrayList if you wish. This option is a better design since this implementation can work with any number of labs )
class Student
{
private int SID;
private int scores[] = new int[
NUMBER_OF_CSC20_LABS
]
;
//
write public get
ter
and set
ter
methods for
//
SID and scores
//
add methods to print values of instance variables.
}
class Statistics
{
private int [] lowscores = new int [NUMBER_OF_CSC20_LABS];
private int [] highscores = new int [NUMBER_OF_CS
C20_LABS];
private float [] avgscores = new float [NUMBER_OF_CSC20_LABS];
void calculateLow(Student [] a)
{
// This method will find lowest score and store it in an array names lowscores
}
void calculateHigh(Student [] a)
{
// This method will fi
nd highest score and store it in an array names highscores
}
void calculateAvg(Student [] a)
{
// This method will find avg score for each lab and store it in an array named avgscores
}
// add methods to print values of instance variables.
}
class
Util
{
public
static
Student[] readFile(String fileName)
throws
IOException
{
//
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 all together:
public static void main(String [] args)
{
Student[] stu
dArr
= Util.readFile(
"studentData.txt"
);
Statistic
s stat = new Statistics();
stat
.calculateLow(studArr);
// add calls for the high and average values
// Print the data and statistics
, etc.
}
Additional materials:
Working with Text files:
// ReadSource.java shows how to w
ork 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);
String line;
line = buff.readLine(
);
while (line != null)
{
System.out.println(line);
line = buff.readLine();
}
buff.close();
}
catch (IOException e)
{
System.out.println("Error " + e.toString());
}
}
}
How do you tokenize a String?
The following example illustrates how the
String.split method can be used to break up a string into
its basic tokens:
String[] result = "this is a test".split("
s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);
How to convert a String to an Integer:
int x = Integer.pars
eInt(str);
Explanation / Answer
Student.java
import java.util.Arrays;
public class Student {
/*
* declare constant NUMBER_OF_CSC20_LABS
*/
private static final int NUMBER_OF_CSC20_LABS = 5;
private int SID;
private int scores[] = new int[NUMBER_OF_CSC20_LABS];
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;
}
@Override
public String toString() {
return "Student [SID=" + SID + ", scores=" + Arrays.toString(scores) + "]";
}
}
Statistics.java
import java.util.Arrays;
public class Statistics {
/*
* declare constant NUMBER_OF_CSC20_LABS
*/
private static final int NUMBER_OF_CSC20_LABS = 5;
private int[] lowscores = new int[NUMBER_OF_CSC20_LABS];
private int[] highscores = new int[NUMBER_OF_CSC20_LABS];
private float[] avgscores = new float[NUMBER_OF_CSC20_LABS];
void calculateLow(Student[] a) {
/*
* Creating a two dimensional array of size, a.length * 5, where
* a.length is the number of students
*/
int scores[][] = new int[a.length][5];
/*
* storing array of scores for each student in this 2 dimensional array
*/
for (int i = 0; i < a.length; i++) {
scores[i] = a[i].getScores();
}
/*
* Iterating column wise and storing min score scored in a particular
* lab score
*/
for (int i = 0; i < 5; i++) {
int min = scores[0][i];
for (int j = 0; j < a.length; j++) {
if (scores[j][i] < min) {
min = scores[j][i];
}
}
lowscores[i] = min;
}
}
void calculateHigh(Student[] a) {
/*
* Creating a two dimensional array of size, a.length * 5, where
* a.length is the number of students
*/
int scores[][] = new int[a.length][5];
/*
* storing array of scores for each student in this 2 dimensional array
*/
for (int i = 0; i < a.length; i++) {
scores[i] = a[i].getScores();
}
/*
* Iterating column wise and storing max score scored in a particular
* lab score
*/
for (int i = 0; i < 5; i++) {
int max = scores[0][i];
for (int j = 0; j < a.length; j++) {
if (scores[j][i] > max) {
max = scores[j][i];
}
}
highscores[i] = max;
}
}
void calculateAvg(Student[] a) {
/*
* Creating a two dimensional array of size, a.length * 5, where
* a.length is the number of students
*/
int scores[][] = new int[a.length][5];
/*
* storing array of scores for each student in this 2 dimensional array
*/
for (int i = 0; i < a.length; i++) {
scores[i] = a[i].getScores();
}
/*
* Iterating column wise and storing max score scored in a particular
* lab score
*/
for (int i = 0; i < 5; i++) {
double sum = 0;
for (int j = 0; j < a.length; j++) {
sum += scores[j][i];
}
avgscores[i] = (float) (sum/a.length);
}
}
// add methods to print values of instance variables.
@Override
public String toString() {
return "Statistics [lowscores=" + Arrays.toString(lowscores) + ", highscores=" + Arrays.toString(highscores)
+ ", avgscores=" + Arrays.toString(avgscores) + "]";
}
}
Util.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Util {
public static Student[] readFile(String fileName) throws IOException {
/*
* number of lines in studentData.txt, since first line is the label we
* will reduce one afterwards
*/
int numOfLines = 0;
/*
* Using try with resources, introduced in java 7, so we do not need to
* worry about closing resources
*/
try (BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))) {
while (br.readLine() != null) {
numOfLines++;
}
}
/*
* creating student array
*/
Student[] stuArr = new Student[numOfLines - 1];
try (BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))) {
br.readLine(); // leaving first line..
String line = "";
int k = 0;
while ((line = br.readLine()) != null) {
String[] result = line.split("\s+");
int sid = Integer.parseInt(result[0]);
int[] score = new int[5];
for (int i = 0; i < score.length; i++) {
score[i] = Integer.parseInt(result[i + 1]);
}
/*
* creating student
*/
stuArr[k] = new Student(sid, score);
k++;
}
}
return stuArr;
}
}
App.java
import java.io.IOException;
public class App {
public static void main(String[] args) {
Student[] studArr = null;
try {
studArr = Util.readFile("studentData.txt");
} catch (IOException e) {
e.printStackTrace();
}
/*
* print student array read from file.. I have commented it, you may
* uncomment to see student array
*/
/*
* for(Student student : studArr) { System.out.println(student); }
*/
Statistics stat = new Statistics();
stat.calculateLow(studArr);
stat.calculateHigh(studArr);
stat.calculateAvg(studArr);
System.out.println(stat);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.