1、请运行下面code,指出其功能;
(需附运行结果截图,并用简短文字描述其功能)
功能:此code主要用于随机产生三个学生的姓和名字,年龄。
2、请将该code进行代码重构,使之模块化,并易于阅读和维护;
import java.util.ArrayList; import java.util.List; import java.util.Random; public class Driver { private static String[] lastNames = {"Doe", "Smith", "Jones", "Adams", "Marshall", "Thompson", "Bradley", "Brown", "White", "Franklin", "Davis", "Cohn", "Clark"}; private static String[] firstNames = {"Mary", "John", "Susan", "Michael", "David", "Lisa", "Wendy", "Diane", "Kelly", "Claire", "Elizabeth", "Mitchell", "Richard"}; public static void main(String[] args) { // create an empty list List<Student> studentList = new ArrayList<Student>(); // initialize random generator Random random = new Random(); // create random number of students loop(studentList, random); //print out the students for (Student temp : studentList) { printDetails(temp); } } public static void loop(List<Student> studentList, Random random) { for (int i=0; i < 3; i++) { // get random first name String tempFirstName = firstNames[random.nextInt(firstNames.length)]; // get random last name String tempLastName = lastNames[random.nextInt(lastNames.length)]; // get random age int age = 18 + random.nextInt(20); // create student Student tempStudent = new Student(tempLastName, tempFirstName, age); // add them to the list studentList.add(tempStudent); } } public static void printDetails(Student temp) { System.out.println(temp); } }
3、观看视频The Expert (Short Comedy Sketch),写出观后感(内容是什么,说明了什么问题,有什么启示),提交到博客!
时间: 2024-10-22 18:10:09