Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import java.util.Arrays;
- public class IPJASS2Q2 {
- //Initializing arrays
- static String names[] = new String[20]; // a one-dimensional array to store the student names
- static int Scores[][] = new int[20][5]; // a (parallel) two-dimensional array to store the test score
- static char grades[] = new char[20]; // a parallel one-dimensional array to store grades
- public static int read_data(String filename) throws FileNotFoundException {
- String inputLine;
- int arraylength=0;
- Scanner scanner = new Scanner(new File(filename));
- //data storing
- while (scanner.hasNextLine()) {
- inputLine = scanner.nextLine();
- String[] temp = inputLine.split(" ");
- for(int i=0;i<temp.length;i++) {
- if(i==0)
- names[arraylength] = temp[i];
- else
- Scores[arraylength][i-1] = Integer.parseInt(temp[i]);
- }
- arraylength++;
- }
- //computing grades
- return arraylength;
- }
- public static void calculate(int arraylength) {
- for(int i=0;i<arraylength;i++) {
- //getting sum of 5 scores
- int s=0;
- for(int j=0;j<5;j++) {
- s+=Scores[i][j];
- }
- //calculating avg
- double avg = s/5;
- //calculating grade
- if(avg >=85)
- grades[i] = 'A';
- else if(avg >= 75)
- grades[i] = 'B';
- else if(avg >= 65)
- grades[i] = 'C';
- else if(avg >= 50)
- grades[i] = 'D';
- else
- grades[i] = 'F';
- }
- }
- public static void print_data(int arraylength) {
- for(int i=0;i<arraylength;i++) {
- System.out.printf("\n %s got %c Grade",names[i],grades[i]);
- System.out.println(names+" Average Score Is : " +);
- }
- }
- public static void main(String[] args) throws FileNotFoundException {
- //reading data from file
- int arraylength = read_data("C:\\Users\\BW\\IdeaProjects\\IPJAssignment\\src\\data");
- //calculating avg score and grades
- calculate(arraylength);
- //to print the data
- print_data(arraylength);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement