博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java课堂作业(十一)
阅读量:5303 次
发布时间:2019-06-14

本文共 4327 字,大约阅读时间需要 14 分钟。

/*

某中学有若干学生(学生对象放在一个List中),每个学生都有一个姓名属性、班级名称属性(String)和考试成绩属性(int),某次考试结束后,每个学生都获得了一个考试成绩。请打印出每个班级的总分和平均分。

*/

转自:http://www.cnblogs.com/zebbin/archive/2013/01/22/2872025.html

1 package com.study.java11; 2  3 //Student类 4 public class Student 5 { 6     private String name; //学生姓名 7     private Team team;    //学生所在班级 8     private int score;    //学生成绩 9     public Student(String name,int score)10     {11         this.setName(name);12         this.setScore(score);13     }14     public String getName()15     {16         return name;17     }18     public void setName(String name)19     {20         this.name = name;21     }22     public int getScore()23     {24         return score;25     }26     public void setScore(int score)27     {28         this.score = score;29     }30     public Team getTeam()31     {32         return team;33     }34     public void setTeam(Team team)35     {36         this.team = team;37     }38     39     public String toString() //覆写toString方法40     {41         return "\t|-姓名:"+this.name +";成绩:"+this.score;42     }43 }
1 package com.study.java11; 2  3 //Team类 4 import java.util.ArrayList; 5 import java.util.Iterator; 6 import java.util.List; 7  8  9 public class Team10 {11     private String name; //班级名称12     private List
list; //学生list(一个班级里有很多学生)13 14 public Team()15 {16 this.list = new ArrayList
();17 }18 19 public Team(String name)20 {21 this();22 this.setName(name);23 }24 25 public String getName()26 {27 return name;28 }29 30 public void setName(String name)31 {32 this.name = name;33 }34 35 public List
getList()36 {37 return list;38 }39 40 public void setList(List
list)41 {42 this.list = list;43 }44 public String toString()45 {46 return "班级名称"+this.name;47 }48 49 public double sum()50 {51 Iterator
iter =list.iterator(); 52 double sum = 0;53 while(iter.hasNext())54 {55 Student s = iter.next();56 sum += s.getScore();57 }58 59 return sum;60 }61 62 public double average()63 {64 return this.sum()/list.size();65 }66 }
1 package com.study.java11; 2  3 import java.util.Iterator; 4  5  6 public class TestList 7 { 8     public static void main(String[] args) 9     {10         Student s1 = new Student("张三",46);11         Student s2 = new Student("李四",98);12         Student s3 = new Student("王五",47);13         Student s4 = new Student("任六",87);14         Student s5 = new Student("黑七",45);15         Student s6 = new Student("赵四",87);16         Student s7 = new Student("孙九",95);17         Student s8 = new Student("钱七",76);18         19         Team t1 = new Team("一班");20         Team t2 = new Team("二班");21         22         t1.getList().add(s1);23         t1.getList().add(s2);24         t1.getList().add(s6);25         t1.getList().add(s8);26         27         t2.getList().add(s3);28         t2.getList().add(s4);29         t2.getList().add(s5);30         t2.getList().add(s7);31         32         s1.setTeam(t1);33         s2.setTeam(t1);34         s6.setTeam(t1);35         s8.setTeam(t1);36         37         s3.setTeam(t2);38         s4.setTeam(t2);39         s5.setTeam(t2);40         s7.setTeam(t2);41         42         System.out.println(t1);43         Iterator
iter1 = t1.getList().iterator();44 while(iter1.hasNext())45 {46 Student s = iter1.next();47 System.out.println(s);48 }49 50 System.out.println("总成绩为:"+t1.sum()+",平均成绩为:"+t1.average());51 System.out.println("--------------------------------");52 53 System.out.println(t2);54 Iterator
iter2 = t2.getList().iterator();55 while(iter2.hasNext())56 {57 Student s = iter2.next();58 System.out.println(s);59 }60 61 System.out.println("总成绩为:"+t2.sum()+",平均成绩为:"+t2.average());62 System.out.println("--------------------------------");63 64 }65 }

 

转载于:https://www.cnblogs.com/dongwenbo/p/3298358.html

你可能感兴趣的文章
Leetcode: Find Leaves of Binary Tree
查看>>
Vue 模板解释
查看>>
http://www.bootcss.com/
查看>>
20145308 《网络对抗》 注入shellcode+Return-to-libc攻击 学习总结
查看>>
将多张图片和文字合成一张图片
查看>>
自己动手写ORM(01):解析表达式树生成Sql碎片
查看>>
如何使用USBWebserver在本机快速建立网站测试环境
查看>>
百度Ueditor编辑器的Html模式自动替换样式的解决方法
查看>>
变量提升
查看>>
线性表可用顺序表或链表存储的优缺点
查看>>
在现有的mysql主从基础上,搭建mycat实现数据的读写分离
查看>>
[Flex] flex手机项目如何限制横竖屏?只允许横屏?
查看>>
tensorflow的graph和session
查看>>
JavaScript动画打开半透明提示层
查看>>
Mybatis生成resulteMap时的注意事项
查看>>
jquery-jqzoom 插件 用例
查看>>
1007. Maximum Subsequence Sum (25)
查看>>
iframe的父子层跨域 用了百度的postMessage()方法
查看>>
图片生成缩略图
查看>>
动态规划 例子与复杂度
查看>>