Question
can someone solve this program for me and explain how they got the answer thank you! its a two dimensional array should print out the
can someone solve this program for me and explain how they got the answer thank you!
its a two dimensional array should print out the lowest and highest scores from a file with six columns and eight rows.
class Student2 { protected static 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 // add methods to print values of instance variables. public Student2(int sid, int[] scores) { SID = sid; this.scores = scores; } public int getSID() { return this.SID; } public void setSID() { this.SID = SID; } public int[] getScores() { return this.scores; } public void setScores(int index, int score) { this.scores[index] = score; } } class Statistics { 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[] temp = new int[stdArray.length]; for(int lowNum = 0; lowNum < 5; lowNum++) { for(int tempArray=0; tempArray.length; tempArray++) { score = stdArray[tempArray].getScore(); temp[tempArray] = score[lowNum]; } lowscores[lowNum] = 100; for(int findLow = 0; findLow < stdArray.length; tempArray++) { if(lowscores[lowNum] > temp[findLow]) lowscores[lowNum] = temp[findLow]; } } } // This method will find lowest score and store it in an array names lowscores void calculateHigh(Student2 [] a) { { int[] temp = new int[stdArray.length]; for(int highNum = 0; highNum < 5; highNum++) { for(int tempArray=0; tempArray.length; tempArray++) { score = stdArray[tempArray].getScore(); temp[tempArray] = score[highNum]; } lowscores[highNum] = 100; for(int findhigh = 0; findhigh < stdArray.length; tempArray++) { if(lowscores[highNum] > temp[findhigh]) highscores[highNum] = temp[findhigh]; } } } // This method will find highest score and store it in an array names highscores } void calculateAvg(Student2 [] a) { for( int labIndex = 0; labIndex < Student.NUMBER_OF_CSC20_LABS; i++) { int average = 0; for( int student = 0; student < a.length; i++) { average += a[student].getScores(labIndex); } avgscores[labIndex] = (double) average / a.length; } // 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 { static Student2 [] readFile(String filename, Student [] stu) { try { FileReader file = new FileReader("p1file.java"); BufferedReader buff = new BufferedReader(file); Stringline = buff.readLine(); int index = 0; while((line = buff.readLine()) != null) // necessary? { System.out.println(line); String[] result = line.split("\\s"); Integer SID = Integer.parseInt(result[0]); int[]score = new int[5]; stu[index] = new Student(SID, score); } buff.close(); } catch( IOException e) { System.out.println(" Error" + e.toString()); } return 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 Test{ public static void main(String [] args) { Student studArr[] = new Student[35]; // populate the student array studArr = Util.readFile("filename.txt", studArr); Statistics stat = new Statistics(); stat.calculateLow(studArr); // add calls for the high and average values // Print the data and statistics } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started