6-2 编写Student类,使用泛型和集合框架编写StudentList类,实现计算班级平均分的功能。

Main类:在main方法中,调用constructStudentList方法构建一个Worker对象链表,调用computeAverageScore方法计算一个班级的平均分并输出到屏幕。 根据要求,编写Student类和StudentList类。

Student类的编程要求如下:

1.成员变量包括:学生姓名(String name)、班级编码(String classCode)、分数(int score)
2.根据程序需求编写构造方法。
3.根据程序需求编写set和get方法。
StudentList类的编程要求如下:

1.根据程序需求编写构造方法。
2.constructStudentList方法:调用readInStudent方法读入多个学生信息,将Student对象添加到链表中,构建一个Student对象链表,最后返回链表。
3.readInStudent方法:使用scanner从键盘读入一个学生的姓名、班级和分数,构建一个Student对象并返回。
4.computeAverageScore(List list)方法:遍历链表,累加链表中所有学生人数和总分数,计算出某个班级平均分并返回。
函数接口定义:
StudentList类的编程要求如下:

List<Student> constructStudentList();
Student readInStudent();
double computeAverageScore(List<Student> list);

裁判测试程序样例:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        StudentList sl=new StudentList();        
        List<Student> list=sl.constructStudentList();        
        System.out.println(sl.computeAverageScore(list));    
    }
}

/* 请在这里填写答案 */
/*请在这里补充Student类*/


/*请在这里补充StudentList类*/

输入样例:

class191
Tom class191 100
Jerry class192 95
Owen class193 90
Jim class191 80 #

以上输入样例中:

第一行为计算平均分的班级编号"class191"。
第二行到最后一行为输入的学生信息,每一行表示一个学生:

   学生姓名 班级编号 分数

最后一行的"#"字符表示数据输入结束。
输出样例:
在这里给出相应的输出。例如:

90.0

输出结果“90.0”为"class191"班所有学生的平均分。
代码

class StudentList{
	public String temp;
	List<Student> constructStudentList(){
		Scanner sc=new Scanner(System.in);
		List<Student> list=new ArrayList<Student>();
		temp=sc.nextLine();
		String nc;
		String cl;
		int  s;
		for(int i=0;;i++)
		{
			int j=0;
			Student w=new Student();
			String ch=sc.nextLine();
			String[] arr = ch.split(" ");
			nc=arr[j];j++;
			cl=arr[j];j++;
			s=Integer.parseInt(arr[j]);j++;
			w.setName(nc);
			w.setclassCode(cl);
			w.setscore(s);
			list.add(w);
			if(arr.length>3)
			{
				break;
			}
		}
		sc.close();
		return list;
	}
	Student readInStudent() {
		Student a=new Student();
		return a;
	}
	double computeAverageScore(List<Student> list) {
		double pri=0;
		double num=0;
		for(int i=0;i<list.size();i++)
		{
			if(temp.equals(list.get(i).getclassCode()))
			{
				pri=pri+list.get(i).getscore();
				num++;
			}
		}
		pri=pri/num;
		return pri;
	}
}


class Student {    
    public static final String WorkerList = null;
	private String name;
    private String classCode;
    private int score;
    public Student() { }
    public Student(String name,String classCode,int score){
        this.name = name;
        this.classCode = classCode;
        this.score=score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getclassCode(){
        return classCode;
    }
    public void setclassCode(String classCode){
        this.classCode = classCode;
    }
    public int getscore(){
        return score;
    }
    public void setscore(int score){
        this.score = score;
    }
    public String toString() {
        return name + " "+classCode+" "+score;
    }    
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值