码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习第二阶段-封装-班级和学生

时间:2016-08-06 15:48:08      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

package p05;

import java.util.Scanner;

public class Program {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SchoolClass J128 = new SchoolClass("J128", "2016年7月");
        J128.addStudent(new Student(201601, "半藏", true, 22));
        J128.addStudent(new Student(201602, "猎空", false, 20));
        J128.addStudent(new Student(201603, "小美", false, 20));
        J128.addStudent(new Student(201604, "源", true, 24));
        J128.addStudent(new Student(201605, "麦克雷", true, 25));
        //用户输入学号查找学生
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个学号,查找学生:");
        int StudentID=input.nextInt();
        boolean isFind=false;
        for (Student item : J128.getStudents()) {
            if (StudentID==item.getStudentID()) {
                item.print();
                System.out.println();
                isFind=true;
                break;
            }
        }
        if (!isFind) {
            System.out.println("不存在该学生!\n");
        }
        //输出该班级的编号以及它所有的学生信息
        J128.print();
        System.out.println();
        //让用户输入一个年龄,查找并输出所有比该年龄大的学生
        System.out.print("请输入一个年龄:");
        int age=input.nextInt();
        boolean isFindAge=false;
        for (Student item : J128.getStudents()) {
            if (item.getAge()>age) {
                System.out.println("学生"+item.getName()+"的年龄大于"+age);
                isFindAge=true;
            }
        }
        if (!isFindAge) {
            System.out.println("没有找到年龄大于"+age+"的学生");
        }
    }

}
package p05;

public class SchoolClass {
    private String number;
    private String classtime;
    private Student[] students;
    
    public Student[] getStudents() {
        return students;
    }
    public void setStudents(Student[] students) {
        this.students = students;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getClasstime() {
        return classtime;
    }
    public void setClasstime(String classtime) {
        this.classtime = classtime;
    }
    
    public SchoolClass(String number,String classtime){
        setNumber(number);
        setClasstime(classtime);
        setStudents(new Student[0]);
    }
    
    public void addStudent(Student newStudent){
        Student[] newStudents=new Student[getStudents().length+1];
        for (int i = 0; i < getStudents().length; i++) {
            newStudents[i]=getStudents()[i];
        }
        newStudents[getStudents().length]=newStudent;
        setStudents(newStudents);
    }
    
    public void print(){
        System.out.println("班级编号:"+getNumber());
        for (Student item : students) {
            item.print();
        }
    }

}
package p05;

public class Student {
    private int studentID;
    private String name;
    private boolean isMale;
    private int age;
    
    public String getIsMale() {
        if (isMale) {
            return "男";
        }
        else {
            return "女";
        }
    }
    public void setMale(boolean isMale) {
        this.isMale = isMale;
    }
    public int getStudentID() {
        return studentID;
    }
    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }
    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 Student(int studentID,String name,boolean isMale,int age){
        setMale(isMale);
        setStudentID(studentID);
        setName(name);
        setAge(age);
    }
    
    public void print(){
        System.out.println("学生学号:"+getStudentID()+",姓名:"+getName()+",性别:"+getIsMale()+",年龄:"+getAge());
    }
    
}

 

Java学习第二阶段-封装-班级和学生

标签:

原文地址:http://www.cnblogs.com/chenxiangyu/p/5744199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!