Buat Tif

mail@pastecode.io avatar
unknown
java
a month ago
3.7 kB
6
Indexable
Never
package Testing4;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        task3();
    }
    public static void task3(){
        Scanner scan = new Scanner(Main.class.getResourceAsStream("scores.csv"));

        // Input testcase
        int T = Integer.parseInt(scan.nextLine());
        while(T!=0){
            System.out.println("ID Number\t|\tStudent Name\t|\tMathematics\t|\tPhysics\t|\tChemistry\t|\tBiology\t|");
            System.out.println("===========================================================================================");
            // Input jmlh student
            int student = Integer.parseInt(scan.nextLine());

            // Ini var buat nyimpen best2 itu
            double bestScore = 0;
            String bestStudId = "";
            String bestStudName = "";

            // Ini looping per line / per student
            for(int i = 0;i<student;i++){
                // input line
                String line = scan.nextLine();
                String[] lineSplit = line.split(",");
                //simpan score di array baru
                double[] score = new double[4];
                for(int j = 0;j<score.length;j++){
                    // Ganti yg kosong jadi 0
                    if(lineSplit[j+2].equals("")){
                        score[j] = 0;
                    }
                    else{
                        score[j] = Double.parseDouble(lineSplit[j+2]);
                    }
                }
                if(Integer.parseInt(lineSplit[0])%2 == 0){
                    double[] scoreSorted = score;
                    Arrays.sort(scoreSorted);
                    double med = (scoreSorted[1] + scoreSorted[2]) / 2.0;
                    for(int k = 0;k<score.length;k++){
                        if(score[k] == 0){
                            score[k] = med;
                        }
                    }
                }
                else {
                    double sum = 0;
                    for(double k : score){
                        sum+=k;
                    }
                    double avg = sum/3;
                    for(int k = 0;k<score.length;k++){
                        if(score[k] == 0){
                            score[k] = avg;
                        }
                    }
                }

                // Bandingin best student nya disini
                // Find avg score
                double sum = 0;
                for(double l : score){
                    sum+=l;
                }
                double avg = sum/score.length;
                if(avg > bestScore){
                    bestStudId = lineSplit[0];
                    bestStudName = lineSplit[1];
                    bestScore = avg;
                }

                // Generate output
                System.out.println(lineSplit[0]+"\t\t\t|\t"+lineSplit[1]+"\t\t\t|\t"+String.format("%.1f",score[0])+"\t\t|\t"+String.format("%.1f",score[1])+"\t|\t"+String.format("%.1f",score[2])+"\t\t|\t"+String.format("%.1f",score[3])+"\t|");
//                for(double p : score){
//                    System.out.println(p);
//                }
            }
            System.out.println("=================");
            System.out.println("BEST STUDENT");
            System.out.println("ID Number = " + bestStudId);
            System.out.println("Name = " + bestStudName);
            System.out.println("Average Score = " + String.format("%.2f",bestScore));
            System.out.println("=================");
            T--;
        }

    }


}

Leave a Comment