首页 > 代码库 > 框架习题

框架习题

1、有如下Student对象,属性如图所示:

 

其中classNum表示学生的班级编号,比如:class05

有如下List:

List list = new ArrayList();
       list.add(new Student(“Tom”, 18, 100, “class05”));
       list.add(new Student(“Jerry”, 22, 70, “class04”));
       list.add(new Student(“Owen”, 25, 90, “class05”));
       list.add(new Student(“Jim”, 30,80 , “class05”));
       list.add(new Student(“Steve”, 28, 66, “class06”));
       list.add(new Student(“Kevin”, 24, 100, “class04”));

在这个list 的基础上,完成下列要求:
       1) 计算所有学生的平均年龄
       2) 计算各个班级的平均分

package com.newer.cn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestStudent {

    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student("Tom", 18, 100, "class05"));
        list.add(new Student("Jerry", 22, 70, "class04"));
        list.add(new Student("Owen", 25, 90, "class05"));
        list.add(new Student("Jim", 30, 80, "class05"));
        list.add(new Student("Steve", 28, 66, "class06"));
        list.add(new Student("Kevin", 24, 100, "class04"));
        list.add(new Student("Jim", 30, 80, "class07"));
        list.add(new Student("Steve", 28, 66, "class06"));
        list.add(new Student("Kevin", 24, 100, "class07"));
        // 求平均年龄
        double sumAge = 0;
        for (Student stu : list) {
            sumAge += stu.getAge();
        }
        /*for (int i = 0; i < list.size(); i++) {
            Student stu =list.get(i);
            sumAge +=stu.getAge();            
        }*/
        System.out.println("平均年龄是:" + sumAge / list.size());
        // 求每个班的平均分
        Map<String, List<Student>> map = new HashMap<String, List<Student>>();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            String classNum = s.getClassNum();
            if (map.containsKey(classNum)) {
                map.get(classNum).add(s);
            } else {
                List<Student> l = new ArrayList<Student>();
                l.add(s);
                map.put(classNum, l);
            }
        }
        // [class06,class05,class04]
        Set<String> cls = map.keySet();
        for (String s : cls) {
            double sumScore = 0;
            List<Student> ls = map.get(s);
            for (Student student : ls) {
                sumScore += student.getScore();
            }
            System.out.println(s + "班的平均分是:" + sumScore / ls.size());
        }
    }

}

2、已知有16支足球队参加2008北京奥运会。写一个程序,随机把16支球队分成四组

package com.newer.cn;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class Test1 {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");
        list.add("G");
        list.add("H");
        list.add("I");
        list.add("J");
        list.add("K");
        list.add("L");
        list.add("M");
        list.add("N");
        list.add("O");
        list.add("P");
        System.out.println(list);
        Collections.shuffle(list);
        System.out.println(list);
        List<List<String>> group = new ArrayList<List<String>>();
        group.add(new ArrayList<String>());
        group.add(new ArrayList<String>());
        group.add(new ArrayList<String>());
        group.add(new ArrayList<String>());
        int a=0;
        for (int i = 0; i < list.size(); i++) {
            group.get(a).add(list.get(i));
            if(group.get(a).size()==4)
                a++;
        }
        for (List k : group)
            System.out.println(k);

    }

}

 

框架习题