构建之法之软件工程

1随机选取表里的三个姓或三个名组成三个年龄在18~38之间的人。

2

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<>();

        // initialize random generator
        Random random = new Random();
        
        // create random number of students
        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);
        }
        
        
    //print out the students
    for (Student temp : studentList) {
        System.out.println(temp);
        
    }

   }

}

Student.java

public class Student {

    private String lastName;
    private String firstName;
    private int age;

    public Student(String lastName, String firstName, int age) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.age = age;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return lastName + ", " + firstName + ", " + age;
    }

}

 3做软件项目研究,不但要反复验证,而且还要和客户交流 ,不但完善,提升软件的完整性,使之更加普遍使用。

 

 

 

posted @ 2015-05-31 20:51  袁爱  阅读(198)  评论(0编辑  收藏  举报