Java题目求解Write a program that reads a number of student’s nam

1个回答

  • />TestStudent.javapackage org.joyliu.demo;

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.util.ArrayList;

    import java.util.HashMap;

    import java.util.List;

    import java.util.Map;

    import java.util.Scanner;

    public class TestStudent {

    private static Map

    datas = new HashMap

    ();

    public static void main(String[] args) {

    String inPath = "G:/workspace_nl/myProject/src/org/joyliu/demo/score.txt";

    //1.load data

    Scanner scanner = null;

    try {

    scanner = new Scanner(new File(inPath));

    while(scanner.hasNextLine()) {

    String ls = scanner.nextLine();

    //System.out.println("===1.reading data========"+ls);

    loadData(ls);

    }

    scanner.close();

    } catch (FileNotFoundException e) {

    e.printStackTrace();

    }

    //2.calculate average,grade and display

    System.out.println("-----------------display average score and grade--------------------");

    System.out.println("IDtNametC1tC2tC3tC4tC5tAveragetGrade");

    Student obj = new Student(datas);

    System.out.println(obj);

    }

    /**

    * description:load data from txt file

    * @param score

    */

    public static void loadData(String score){

    String stuName = "";

    List

    scores = new ArrayList

    ();

    String[] ss = score.split(",");

    if(ss.length>0){

    stuName = ss[0]+"#"+ss[1];

    for (int i=2; i

    //System.out.println(i+"===="+ss[i]);

    scores.add(Integer.parseInt(ss[i]));

    }

    datas.put(stuName, scores);

    }

    //System.out.println("====total count==="+datas.size());

    }

    /**

    * @return the datas

    */

    public Map

    getDatas() {

    return datas;

    }

    /**

    * @param datas the datas to set

    */

    public void setDatas(Map

    datas) {

    this.datas = datas;

    }

    }

    Student.javapackage org.joyliu.demo;

    import java.math.BigDecimal;

    import java.util.ArrayList;

    import java.util.HashMap;

    import java.util.Iterator;

    import java.util.List;

    import java.util.Map;

    public class Student {

    private Map

    datas = new HashMap

    ();

    public Student() {

    }

    public Student(Map

    datas) {

    this.datas = datas;

    }

    /**

    * description:calculate the average of five test scores for each student.

    */

    public double calculateAverage (List scores){

    int sum = 0;

    int sc = 0;

    double avg = 0;

    for (Object o : scores) {

    sc = (Integer) o;

    sum += sc;

    }

    BigDecimal bd1 = new BigDecimal(sum);

    BigDecimal bd2 = new BigDecimal(scores.size());

    avg = bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP).doubleValue();

    return avg;

    }

    /**

    * description:calculate grade of scores

    */

    public String calculateGrade (double score){

    if(score >= 90)

    return "A";

    else if(score >= 80)

    return "B";

    else if(score >= 70)

    return "C";

    else if(score >= 60)

    return "D";

    else return "F";

    }

    /**

    * display the average scores and grade for each student

    */

    @Override

    public String toString() {

    List

    avgs = new ArrayList

    ();

    double avg = 0;

    double sum = 0;

    for (Iterator i = this.getDatas().keySet().iterator(); i.hasNext();) {

    Object obj = i.next();

    avg = this.displayStr(obj.toString(), this.getDatas().get(obj));

    avgs.add(avg);

    }

    for (Double anm : avgs) {

    sum += anm;

    }

    BigDecimal bd1 = new BigDecimal(sum);

    BigDecimal bd2 = new BigDecimal(avgs.size());

    System.out.println("Class Average = "+bd1.divide(bd2,2,BigDecimal.ROUND_HALF_UP).doubleValue());

    return "";

    }

    /**

    * @param name student Name

    * @param scores array

    * @return

    */

    public double displayStr(String name,List scores){

    double avg = 0;

    String[] fstr = name.split("#");

    System.out.print(fstr[0]+"t"+fstr[1]+"t");

    for (Object score : scores) {

    System.out.print(score+"t");

    }

    avg = this.calculateAverage(scores);

    System.out.print(avg+"t"+this.calculateGrade(avg)+"rn");

    return avg;

    }

    /**

    * @return the datas

    */

    public Map

    getDatas() {

    return datas;

    }

    /**

    * @param datas the datas to set

    */

    public void setDatas(Map

    datas) {

    this.datas = datas;

    }

    }test data:1,Balto,85,83,77,91,76

    2,Mickey,80,90,95,93,48

    3,Minnie,78,81,11,90,73

    4,Doc,92,83,30,69,87

    5,Goofy,23,45,96,38,59

    6,Duckey,60,85,45,39,67

    7,Grumpy,27,31,52,74,83

    8,Sunny,93,94,89,77,97

    9,Piggy,79,85,28,93,82

    10,Pluto,85,72,49,75,63

    result:-----------------display average score and grade--------------------

    ID Name C1 C2 C3 C4 C5 Average Grade

    1 Balto 85 83 77 91 76 82.4 B

    3 Minnie 78 81 11 90 73 66.6 D

    2 Mickey 80 90 95 93 48 81.2 B

    8 Sunny 93 94 89 77 97 90.0 A

    6 Duckey 60 85 45 39 67 59.2 F

    10 Pluto 85 72 49 75 63 68.8 D

    7 Grumpy 27 31 52 74 83 53.4 F

    5 Goofy 23 45 96 38 59 52.2 F

    9 Piggy 79 85 28 93 82 73.4 C

    4 Doc 92 83 30 69 87 72.2 C

    Class Average = 69.94