第五次作业
题目:
1、请运行下面code,指出其功能;(需附运行结果截图,并用简短文字描述其功能)
2、请将该code进行代码重构,使之模块化,并易于阅读和维护;
3、观看视频The Expert (Short Comedy Sketch),写出观后感(内容是什么,说明了什么问题,有什么启示),提交到博客!
4、学习在项目中使用 jar 文件:
(1)在下列code中导入jar文件“commons-lang3-3.3.2.jar”,并运行,将运行结果截图提交到博客:
答题:
1、请运行下面code,指出其功能;(需附运行结果截图,并用简短文字描述其功能)
功能:第一个是姓氏, 第二个是名,随机生成一个姓氏和一个名,
组成一个人的名字以及随机生成一个的年龄。
2、请将该code进行代码重构,使之模块化,并易于阅读和维护;
printDetails(studentList, random);
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Random; 4 5 6 public class driver { 7 8 private static String[] lastNames = {"Doe", "Smith", "Jones", "Adams", "Marshall", "Thompson", "Bradley", "Brown", "White", "Franklin", "Davis", "Cohn", "Clark"}; 9 private static String[] firstNames = {"Mary", "John", "Susan", "Michael", "David", "Lisa", "Wendy", "Diane", "Kelly", "Claire", "Elizabeth", "Mitchell", "Richard"}; 10 11 public static void main(String[] args) { 12 13 // create an empty list 14 List<Student> studentList = new ArrayList<Student>(); 15 16 // initialize random generator 17 Random random = new Random(); 18 19 // create random number of students 20 printDetails(studentList, random); 21 22 } 23 24 public static void printDetails(List<Student> studentList, Random random) { 25 for (int i=0; i < 3; i++) { 26 27 // get random first name 28 String tempFirstName = firstNames[random.nextInt(firstNames.length)]; 29 30 // get random last name 31 String tempLastName = lastNames[random.nextInt(lastNames.length)]; 32 33 // get random age 34 int age = 18 + random.nextInt(20); 35 36 // create student 37 Student tempStudent = new Student(tempLastName, tempFirstName, age); 38 39 // add them to the list 40 studentList.add(tempStudent); 41 } 42 43 44 //print out the students 45 driver(studentList); 46 } 47 48 private static void driver(List<Student> studentList) { 49 for (Student temp : studentList) { 50 System.out.println(temp); 51 52 } 53 } 54 55 }
3、观看视频The Expert (Short Comedy Sketch),写出观后感(内容是什么,说明了什么问题,有什么启示),提交到博客!
答: 这个视频讲的是 :首先画七根红线,有的用绿色墨水画,有的用透明墨水画,这七根线
要两两垂直。其次;大家在分享专家的意见,在交流解决尽量满足用户的需求问题,
4、学习在项目中使用 jar 文件:
(1)在下列code中导入jar文件“commons-lang3-3.3.2.jar”,并运行,将运行结果截图提交到博客:
导入jar文件“commons-lang3-3.3.2.jar”:
将运行结果:
导入jar文件“后的代码:
1 import org.apache.commons.lang3.time.StopWatch; 2 3 public class Drivers { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 System.out.println("Running..."); 8 StopWatch myStopWatch = new StopWatch(); 9 10 myStopWatch.start(); 11 12 performLengthyProcess(); 13 14 myStopWatch.stop(); 15 16 System.out.println("Time is:" + myStopWatch.getTime() + " millis"); 17 18 System.out.println("Finished..."); 19 20 } 21 22 private static void performLengthyProcess(){ 23 try{ 24 Thread.sleep(3000); //3 second delay 25 26 }catch(InterruptedException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 }