I need help with two classes in a Java project im working on I want to say I am
ID: 3816825 • Letter: I
Question
I need help with two classes in a Java project im working on
I want to say I am on the right path but I just cant get it together
File:
Stud Lb1 Lb2 Lb3 Lb4 Lb5
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
---------------------------------------------------------------------------------------------------------------
class Util
{
final int NUMBERofLABS = 5;
public static void main(String [] args)
{
// populate the student array
Student studArr[] = new Student[35];
//read the text file
studArr = Util.readFile("Data.txt", studArr);
//create stat object
Statistics stat = new Statistics();
// add calls for the high and average values
stat.calculateLow(studArr);
stat.calculateHigh(studArr);
stat.calculateAvg(studArr);
// Print the data and statistics
}
static 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.
}
}
---------------------------------------------------------------------------------------------------------------
class Statistics
{
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)
{
int length= a.length-1;
// This method will find lowest score
//and store it in an array names lowscores
//This method will find lowest score and store it in an array names lowscores
int[] tmp=new int [length];
for(int lowNum=0;lowNum<5;lowNum++)
{
for(int tmpArray=0; tmpArray<length;tmpArray++)
{
int[] score=a[tmpArray].getScores();
tmp[tmpArray]=score[lowNum];
}
//Start here for lowest
lowscores[lowNum]=100;
for(int findlow=0;findlow<length;findlow++)
{
if(lowscores[lowNum]>tmp[findlow])
{
lowscores[lowNum]=tmp[findlow];
}
System.out.println(lowscores[]);
}
}
}
void calculateHigh(Student [] a)
{
// This method will find 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 Student
{
final int NUMBER_OF_CSC20_LABS = 5;
private int SID;
private int scores[] = new int[NUMBER_OF_CSC20_LABS];
// write public getter and setter methods for
// SID and scores
public Student (int newID, int newstudArr[])
{
SID = newID;
scores = newstudArr;
}
// set SID
public void setID (int newID)
{
this.SID = newID;
}
// set scores
public void setScores (int newstudArr[])
{
this.scores = newstudArr;
}
// get SID
public int getSID ()
{
return SID;
}
// get scores
public int[] getScores()
{
return scores;
}
}
Explanation / Answer
Util.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Util {
final int NUMBERofLABS = 5;
public static void main(String [] args) throws Exception
{
// populate the student array
Student studArr[] = new Student[35];
//read the text file
studArr = Util.readFile("Data.txt", studArr);
//create stat object
Statistics stat = new Statistics();
// add calls for the high and average values
stat.calculateLow(studArr);
stat.calculateHigh(studArr);
stat.calculateAvg(studArr);
// Print the data and statistics
stat.printLowScores();
stat.printHighScores();
stat.printAvgScores();
}
static Student [] readFile(String filename, Student [] stu) throws Exception
{
// Reads the file and builds student array.
File file = new File(filename);
// Open the file using FileReader Object.
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String data;
int count=0;
// In a loop read a line using readLine method.
data=br.readLine();//reading the first Line;
data=br.readLine();
while(data != null)
{
int ssid=0;
int scores[]= new int[5];
StringTokenizer strToken = new StringTokenizer(data);
while(strToken.hasMoreElements()){
ssid=Integer.parseInt(strToken.nextToken());
scores[0] =Integer.parseInt(strToken.nextToken());
scores[1] =Integer.parseInt(strToken.nextToken());
scores[2] =Integer.parseInt(strToken.nextToken());
scores[3] =Integer.parseInt(strToken.nextToken());
scores[4] =Integer.parseInt(strToken.nextToken());
}
//System.out.println(data);
stu[count]=new Student(ssid, scores);
data=br.readLine();
count++;
}
Student newStu[]=new Student[count];
for(int i=0;i<stu.length;i++){
if(stu[i]==null)
break;
newStu[i]=stu[i];
}
return newStu;
// 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.
}
}
Statistics.java
public class Statistics {
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];
//calculate the lowest scores in each subjects
void calculateLow(Student [] a)
{
int length= a.length-1;
// This method will find lowest score
//and store it in an array names lowscores
//This method will find lowest score and store it in an array names lowscores
int[] tmp=new int [length+1];
for(int lowNum=0;lowNum<5;lowNum++)
{
for(int tmpArray=0; tmpArray<=length;tmpArray++)
{
int[] score=a[tmpArray].getScores();
tmp[tmpArray]=score[lowNum];
}
//Start here for lowest
lowscores[lowNum]=100;
for(int findlow=0;findlow<=length;findlow++)
{
if(lowscores[lowNum]>tmp[findlow])
{
lowscores[lowNum]=tmp[findlow];
}
}
}
}
//calculate the high scores in each subjects
void calculateHigh(Student [] a)
{
// This method will find highest score
//and store it in an array names highscores
int length= a.length-1;
int[] tmp=new int [length+1];
for(int lowNum=0;lowNum<5;lowNum++)
{
for(int tmpArray=0; tmpArray<=length;tmpArray++)
{
int[] score=a[tmpArray].getScores();
tmp[tmpArray]=score[lowNum];
}
highscores[lowNum]=tmp[0];
for(int findlow=0;findlow<=length;findlow++)
{
if(highscores[lowNum]<tmp[findlow])
{
highscores[lowNum]=tmp[findlow];
}
}
}
}
//calculate the average in each subjects
void calculateAvg(Student [] a)
{
int length= a.length-1;
int[] tmp=new int [length+1];
for(int lowNum=0;lowNum<5;lowNum++)
{
float sum=0;
for(int tmpArray=0; tmpArray<=length;tmpArray++)
{
int[] score=a[tmpArray].getScores();
tmp[tmpArray]=score[lowNum];
}
for(int i=0;i<=length;i++){
sum=sum+tmp[i];
}
avgscores[lowNum]=sum/tmp.length;
}
}
//print the lowest in each subjects
void printLowScores(){
for(int temp=0;temp<lowscores.length;temp++){
System.out.print(lowscores[temp]+" ");
}
System.out.println();
}
//print the highest in each subjects
void printHighScores(){
for(int temp=0;temp<highscores.length;temp++){
System.out.print(highscores[temp]+" ");
}
System.out.println();
}
//print the average in each subjects
void printAvgScores(){
for(int temp=0;temp<avgscores.length;temp++){
System.out.print(avgscores[temp]+" ");
}
System.out.println();
}
}
Student.java
public class Student {
final int NUMBER_OF_CSC20_LABS = 5;
private int SID;
private int scores[] = new int[NUMBER_OF_CSC20_LABS];
// write public getter and setter methods for
// SID and scores
public Student (int newID, int newstudArr[])
{
SID = newID;
scores = newstudArr;
}
// set SID
public void setID (int newID)
{
this.SID = newID;
}
// set scores
public void setScores (int newstudArr[])
{
this.scores = newstudArr;
}
// get SID
public int getSID ()
{
return SID;
}
// get scores
public int[] getScores()
{
return scores;
}
}
---------output-------------------------
11 11 19 17 10
40 40 36 38 34
30.363636 27.09091 26.09091 26.363636 24.636364
-----output----------------
note: Feel free to ask any question in case of doubts