• Day_11【集合】扩展案例3_打印最高分的学员姓名、年龄、成绩,打印10个学生的总成绩和平均分,打印不及格的学员信息及数量


    分析以下需求,并用代码实现

    •  1.定义Student类
       属性:
       	姓名:String name
       	年龄:int age
       	成绩:int score
       行为:
       	空参构造方法
       	有参构造方法
       	set和get方法
       	toString方法
       2.定义测试类,进行测试
       (1)创建10个学生对象存入ArrayList集合中
       (2)打印最高分的学员姓名、年龄、成绩		[要求封装1个方法  参数是集合对象   返回值类型为Student]
       (3)打印10个学生的总成绩和平均分		[要求封装两个方法完成]
       (4)打印不及格的学员信息及数量			[要求封装一个方法完成]
      

    代码

    package com.itheima3;
    
    public class Student {
    	private String name;
    	private int age;
    	private int score;
    
    	public Student() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	public Student(String name, int age, int score) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.score = score;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public int getScore() {
    		return score;
    	}
    
    	public void setScore(int score) {
    		this.score = score;
    	}
    
    	/*
    	 * toString方法:public String toString()
    	 * 作用:返回此对象本身(它已经是一个字符串!)
    	 */
    	@Override
    	public String toString() {
    		return "Student [name:"+name+" age:"+age+" score:"+score+"]";
    	}
    }
    
    
    package com.itheima3;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    public class Test3 {
    	public static void main(String[] args) {
    		//创建集合对象
    		ArrayList<Student> list = new ArrayList<Student>();
    		
    		//创建元素对象
    		Student s1 = new Student("张三",18,30);
    		Student s2 = new Student("李四",19,50);
    		Student s3 = new Student("王五",20,30);
    		Student s4 = new Student("赵六",18,86);
    		Student s5 = new Student("周吴",18,75);
    		Student s6 = new Student("郑王",19,60);
    		Student s7 = new Student("大大",21,70);
    		Student s8 = new Student("老八",19,90);
    		Student s9 = new Student("哈哈",20,99);
    		Student s10 = new Student("钱九",18,80);
    		
    		//将元素添加到集合中去
    		list.add(s1);
    		list.add(s2);
    		list.add(s3);
    		list.add(s4);
    		list.add(s5);
    		list.add(s6);
    		list.add(s7);
    		list.add(s8);
    		list.add(s9);
    		list.add(s10);
    		
    		//调用方法
    		Student maxStu = getMaxStudent(list);
    		System.out.println("最高分的学员信息为:"+maxStu);
    		
    		int sum = getSumScore(list);
    		System.out.println(list.size()+"名学员的总成绩为:"+sum);
    		
    		int avg = getAvgScore(list);
    		System.out.println(list.size()+"名学员的平均成绩为:"+avg);
    		
    		printFailStuInfo(list);
    	}
    	//打印最高分的学员姓名、年龄、成绩		[要求封装1个方法  参数是集合对象   返回值类型为Student]
    	public static Student getMaxStudent(ArrayList<Student> list) {
    		Student s = list.get(0);
    		for(int i = 0;i < list.size();i++) {
    			if(list.get(i).getScore() > s.getScore()) {
    				s = list.get(i);
    			}
    		}
    		return s;
    	}
    	
    	//打印10个学生的总成绩
    	public static int getSumScore(ArrayList<Student> list) {
    		int sum = 0;
    		for(int i = 0;i < list.size();i++) {
    			Student s = list.get(i);
    			sum += s.getScore();
    		}
    		return sum;
    	}
    	
    	//打印10个学生的平均分	
    	public static int getAvgScore(ArrayList<Student> list) {
    		int sum = getSumScore(list);
    		return sum / list.size();
    	}
    	
    	//打印不及格的学员信息及数量			[要求封装一个方法完成]
    	public static void printFailStuInfo(ArrayList<Student> list) {
    		int count = 0;
    		System.out.println("不及格的学生为:");
    		for(int i = 0;i < list.size();i++) {
    			Student s = list.get(i);
    			if(list.get(i).getScore() < 60) {
    				System.out.println(list.get(i));
    				count++;
    			}
    		}
    		System.out.println("不及格的学生数量为:"+count);
    	}
    
    }
    
    

    控制台输出内容
    console

  • 相关阅读:
    箭头函数和普通函数的区别是什么?
    前端如何优化网站性能?
    instanceof原理
    call、apply区别
    函数的节流和防抖
    关于this的指向性问题
    undefined 和null的区别?
    浅谈堆和栈的理解?
    关于vue中watch和computed
    简单说一下什么是回流和重绘
  • 原文地址:https://www.cnblogs.com/zzzsw0412/p/12772529.html
Copyright © 2020-2023  润新知