java test10089_Java题目求解Writeaprogramthatreadsanumberofstudent’snametogetherwithhisorhertestscores.Th...

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("ID\tName\tC1\tC2\tC3\tC4\tC5\tAverage\tGrade");

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)+"\r\n");

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值