1 编写一个学生类,提供name, age, gender, phone, address, email成员变量,并实现以下的功能。 1.1为每个成员变量提供getter和setter方法 1.2为学生类提供默认构造器和带所有成员变量的构造器 1.3为学生类提供描绘吃、喝、玩、睡等行为的方法 1.4在主方法中定义一个学生类的数组,用来保存通讯录数据,寻找本班5-10位同学的相关信息保存进该数组中 定义方法,可以通过name, email或者address来查询学生信息,如果找不到数据,则进行友好的提示
时间: 2023-06-15 22:06:43 浏览: 269
以下是学生类的代码实现:
```java
public class Student {
private String name;
private int age;
private String gender;
private String phone;
private String address;
private String email;
public Student() {
}
public Student(String name, int age, String gender, String phone, String address, String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.address = address;
this.email = email;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void eat() {
System.out.println("我在吃饭");
}
public void drink() {
System.out.println("我在喝水");
}
public void play() {
System.out.println("我在玩游戏");
}
public void sleep() {
System.out.println("我在睡觉");
}
}
```
以下是在主方法中使用学生类的数组保存通讯录数据,并实现通过name、email或者address来查询学生信息的代码实现:
```java
public class Main {
public static void main(String[] args) {
Student[] students = new Student[10];
students[0] = new Student("张三", 18, "男", "123456789", "北京市海淀区", "zhangsan@qq.com");
students[1] = new Student("李四", 19, "女", "987654321", "上海市浦东新区", "lisi@qq.com");
students[2] = new Student("王五", 20, "男", "12348765", "广州市天河区", "wangwu@qq.com");
students[3] = new Student("赵六", 21, "女", "87654321", "深圳市南山区", "zhaoliu@qq.com");
students[4] = new Student("钱七", 22, "男", "123456789", "杭州市西湖区", "qianqi@qq.com");
students[5] = new Student("孙八", 23, "女", "987654321", "成都市锦江区", "sunba@qq.com");
students[6] = new Student("周九", 24, "男", "12348765", "南京市鼓楼区", "zhoujiu@qq.com");
students[7] = new Student("吴十", 25, "女", "87654321", "武汉市洪山区", "wushi@qq.com");
students[8] = new Student("郑十一", 26, "男", "123456789", "重庆市江北区", "zhengshiyi@qq.com");
students[9] = new Student("王十二", 27, "女", "987654321", "苏州市工业园区", "wangshier@qq.com");
Student foundStudent = findStudentByName(students, "张三");
if (foundStudent != null) {
System.out.println("找到了学生:" + foundStudent.getName() + ",年龄:" + foundStudent.getAge() + ",性别:" + foundStudent.getGender());
} else {
System.out.println("未找到该学生");
}
foundStudent = findStudentByEmail(students, "zhaoliu@qq.com");
if (foundStudent != null) {
System.out.println("找到了学生:" + foundStudent.getName() + ",年龄:" + foundStudent.getAge() + ",性别:" + foundStudent.getGender());
} else {
System.out.println("未找到该学生");
}
foundStudent = findStudentByAddress(students, "广州市天河区");
if (foundStudent != null) {
System.out.println("找到了学生:" + foundStudent.getName() + ",年龄:" + foundStudent.getAge() + ",性别:" + foundStudent.getGender());
} else {
System.out.println("未找到该学生");
}
foundStudent = findStudentByName(students, "陈十三");
if (foundStudent != null) {
System.out.println("找到了学生:" + foundStudent.getName() + ",年龄:" + foundStudent.getAge() + ",性别:" + foundStudent.getGender());
} else {
System.out.println("未找到该学生");
}
}
public static Student findStudentByName(Student[] students, String name) {
for (Student student : students) {
if (student.getName().equals(name)) {
return student;
}
}
return null;
}
public static Student findStudentByEmail(Student[] students, String email) {
for (Student student : students) {
if (student.getEmail().equals(email)) {
return student;
}
}
return null;
}
public static Student findStudentByAddress(Student[] students, String address) {
for (Student student : students) {
if (student.getAddress().equals(address)) {
return student;
}
}
return null;
}
}
```
阅读全文