Sunday, June 16, 2013

Score Management in java

- Use one dimensional Array:
package homework;

import java.io.*;

public class Topic7 {
    public static void main(String[] args) throws IOException{
        if(args.length != 1)
            System.exit(0);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int number_of_student= Integer.parseInt(args[0]);
        //int number_of_student= Integer.parseInt(in.readLine());
        String[] name = new String[number_of_student];
        int[] kor = new int[number_of_student];
        int[] eng = new int[number_of_student];
        int[] mat = new int[number_of_student];
        int[] tot = new int[number_of_student];
        float[] avg = new float[number_of_student];
        int[] rank = new int[number_of_student];
       
        for(int i=1;i<=number_of_student;i++){
            System.out.println("No. "+i+" Student Info :");
            System.out.print("Name ? ");
            name[i-1] = in.readLine();
            System.out.print("kor ? ");
            kor[i-1]=Integer.parseInt(in.readLine());
            System.out.print("eng ? ");
            eng[i-1]=Integer.parseInt(in.readLine());
            System.out.print("mat ? ");
            mat[i-1]=Integer.parseInt(in.readLine());
            tot[i-1]=kor[i-1]+eng[i-1]+mat[i-1];
            avg[i-1]=tot[i-1]/3;
            rank[i-1]=1;
        }

        for(int i=0;i<number_of_student;i++){
            for(int j=0;j<number_of_student;j++){
                if(avg[i]>avg[j]) rank[j]++;
            }
        }
        System.out.println();
        for(int i=0;i<number_of_student;i++){
            System.out.println(name[i]+"\t"+ kor[i] +"\t"+eng[i]+"\t"+mat[i]+"\t"+tot[i]+"\t"+avg[i]+"\t"+rank[i]);

        }
    }
}

-Use tow dimensional Array:


package homework;
import java.io.*;

public class Topic9 {
    public static void main(String[] args) throws IOException{
        if(args.length < 2){
            System.out.println("Please you input argument at least 2: ");
            System.exit(0);
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("How many classes ? ");
        int x = Integer.parseInt(in.readLine());
        String[][] name = new String[x][];
        float[][][] sub = new float[x][][];
        float[][]tot=new float[x][];
        int[][]crank=new int[x][];
        int[][]rank=new int[x][];
        float[][] avg = new float[x][];
        for(int i=0;i<x;i++){
            System.out.print(i+"th class: number of students? ");
            int num_of_student_per_class=Integer.parseInt(in.readLine());
            name[i] = new String[num_of_student_per_class];
            sub[i]=new float[name[i].length][args.length];
            tot[i]=new float[name[i].length];
            crank[i]=new int[name[i].length];
            rank[i]=new int[name[i].length];
            avg[i]=new float[name[i].length];
        }
        for(int i=0;i<x;i++){
            for(int j=0;j<name[i].length;j++){
                System.out.print("class"+i+": students"+j+"'s name = ");
                name[i][j]=in.readLine();
               
                for(int k=0;k<args.length;k++){
                    do{
                        System.out.print(args[k]+" = ");
                        sub[i][j][k]=Integer.parseInt(in.readLine());
                    }while(sub[i][j][k]<0 || sub[i][j][k]>100);
                    tot[i][j] +=sub[i][j][k];
                }
                crank[i][j]=1;rank[i][j]=1;avg[i][j]=(tot[i][j]/(args.length));
            }
        }
        for(int i=0;i<name.length;i++){
            for(int j=0;j<name[i].length;j++){
                for(int k=0;k<name[i].length;k++)
                    if(tot[i][j]>tot[i][k]) crank[i][k]++;
            }
        }
       
        for(int i=0;i<avg.length;i++)
            for(int j=0;j<avg[i].length;j++)
                for(int m=0;m<avg.length;m++)
                    for(int k=0;k<avg[m].length;k++)
                        if(avg[i][j]>avg[m][k]) rank[m][k]++;
                   

        System.out.println();
        for(int i=0;i<x;i++){
            for(int j=0;j<name[i].length;j++){
                System.out.print(name[i][j]+"\t");
                for(int k=0;k<sub[i][j].length;k++){
                    System.out.print(sub[i][j][k]+"\t");
                }
                System.out.print(tot[i][j]+"\t"+crank[i][j]+"\t"+rank[i][j]+"\t"+avg[i][j]+"\t");
                System.out.println();
            }
        }
   
    }
}

No comments:

Post a Comment