m0_61991343 2021-11-08 12:05 采纳率: 77.8%
浏览 236
已结题

Java定义Student类(包括姓名、学号、籍贯、出生日期、专业等属性,获取姓名、学号、籍贯、年龄和专业的实例方法,增加initStudents(int number)静态方法。

刚开始学,这个怎么用java语言定义Student类(包括姓名、学号、籍贯、出生日期、专业等属性,获取姓名、学号、籍贯、年龄和专业的实例方法,增加initStudents(int number)静态方法,返回指定数量同学的数组,学号随机生成)

  • 写回答

1条回答 默认 最新

  • 王帅朋 2021-11-08 16:48
    关注

    如有帮助,请点击采纳哦

    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    /**
     * 定义Student类(包括姓名、学号、籍贯、出生日期、专业等属性,获取姓名、学号、籍贯、年龄和专业的实例方法,
     * 增加initStudents(int number)静态方法,返回指定数量同学的数组,学号随机生成)
     */
    public class Student {
    
        private String no;
    
        private String name;
    
        private String nativePlace;
    
        private Date birthday;
    
        private String professional;
    
        public String getNo() {
            return no;
        }
    
        public void setNo(String no) {
            this.no = no;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getNativePlace() {
            return nativePlace;
        }
    
        public void setNativePlace(String nativePlace) {
            this.nativePlace = nativePlace;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public String getProfessional() {
            return professional;
        }
    
        public void setProfessional(String professional) {
            this.professional = professional;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "no='" + no + '\'' +
                    ", name='" + name + '\'' +
                    ", nativePlace='" + nativePlace + '\'' +
                    ", birthday=" + birthday +
                    ", professional='" + professional + '\'' +
                    '}';
        }
    
        public static List<Student> initStudents(int number) {
            List<Student> students = new ArrayList<>(number);
            List<String> noList = new ArrayList<>();
            while (students.size() < number) {
                Student student = new Student();
                String no = assembleNo();
                if (!noList.contains(no)) {
                    noList.add(no);
                    student.setNo(no);
                    student.setName("学生" + students.size());
                    student.setProfessional("专业" + students.size());
                    students.add(student);
                }
            }
    
            return students;
        }
    
        public static String assembleNo() {
            int len = 6;
            StringBuilder no = new StringBuilder();
            for (int i = 0; i < len; i++) {
                no.append((int) (Math.random() * 10));
            }
    
            return no.toString();
        }
    
        public static void main(String[] args) {
            List<Student> students = initStudents(6);
            for (Student student : students) {
                System.out.println(student.toString());
            }
        }
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月7日
  • 已采纳回答 4月29日
  • 修改了问题 11月8日
  • 提问应符合社区要求 11月8日
  • 展开全部