Java 疯狂java第五章习题
Java 疯狂java第五章习题
1、编写一个学生类,提供name、age、gender、phone、address、email成员变量,且为每个成员变量提供setter、getter方法。为学生类提供默认的构造器和带所有成员变量的构造器。为学生类提供方法,用于描绘吃、喝、玩、乐。
2、利用第一题定义的Student类,定义一个Student[]数组保存多个Student对象作为通讯录数据。程序可以通过name、email、address查询,如查不到数据,则进行友好提示。
package five;
import java.util.Scanner;
import static java.lang.System.*;
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 void eating(String food){
out.println(name+"正在吃"+food);
}
//喝
public void drinking(String drink){
out.println(name+"正在喝"+drink);
}
//玩
public void playing(String game){
out.println(name+"正在玩"+game);
}
//睡
public void sleeping(){
out.println(name+"正在睡觉");
}
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 static void main(String[] args) {
boolean isfind = false;
Student[] students = new Student[]{new Student("张三",12,"男","123","月之酒店","123"),
new Student("李四",21,"男","1234","月之酒店","1234"),
new Student("赵五",31,"男","12347","月之酒店","12347")};
out.println("请输入“姓名”、“邮箱”、“地址”来进行查询");
Scanner scanner = new Scanner(in);
String line = scanner.nextLine();
out.println(line);
for (var i = 0; i < students.length; i++){
if (line.equals(students[i].name)||line.equals(students[i].email)||line.equals(students[i].address)){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("姓名:");
stringBuilder.append(students[i].name);
stringBuilder.append("\n姓别:");
stringBuilder.append(students[i].gender);
stringBuilder.append("\n电话:");
stringBuilder.append(students[i].phone);
stringBuilder.append("\n邮箱:");
stringBuilder.append(students[i].email);
stringBuilder.append("\n地址:");
stringBuilder.append(students[i].address);
out.println(stringBuilder.toString());
isfind = true;
}
}
if (!isfind){
out.println("查询不到此用户!");
}
}
}
3、定义普通人、老师、班主任、学生、学校这些类,提供适当的成员变量、方法用于描述其内部数据和行为特征,并提供主类使之运行。要求有良好的封装性,将不同的类放在不同的包下面。
package five.schools;
import static java.lang.System.out;
public class Person {
protected String name;
protected int age;
protected String gender;
protected float height;
protected float weight;
protected String occopation;
public Person(){}
public Person(String name, String gender, float height, float weight, String occopation,int age) {
this.name = name;
this.gender = gender;
this.height = height;
this.weight = weight;
this.occopation = occopation;
this.age = age;
}
public void eating(String food){
out.println(this.getName()+"正在吃"+food);
}
//喝
public void drinking(String drink){
out.println(this.getName()+"正在喝"+drink);
}
//玩
public void playing(String game){
out.println(this.getName()+"正在玩"+game);
}
//睡
public void sleeping(){
out.println(this.getName()+"正在睡觉");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public String getOccopation() {
return occopation;
}
public void setOccopation(String occopation) {
this.occopation = occopation;
}
}
package five.schools;
import static java.lang.System.*;
public class School {
private String name;
private String address;
private String level;
public School(){}
public School(String name, String address, String level) {
this.name = name;
this.address = address;
this.level = level;
}
public void recruitingTeacher(String name){
out.println("招募老师"+name);
}
}
package five.schools.classes;
import static java.lang.System.*;
import five.schools.Person;
public class Headmaster extends Teacher{
private String classId;
public Headmaster(){};
public Headmaster(String name, String gender, float height, float weight, String occopation, int age, String id, String curriculumName, String classId) {
super(name, gender, height, weight, occopation, age, id, curriculumName);
this.classId = classId;
}
public void talking(String studentName){
out.println("班主任"+this.getName()+"跟学生"+studentName+"谈话");
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
}
```java
package five.schools.classes;
import static java.lang.System.*;
import five.schools.Person;
public class Student extends Person {
private String id;//学号
private String classId;//班级
public Student(){
}
public Student(String name, String gender, float height, float weight, String occopation,int age,String id, String classId){
super(name, gender, height, weight, occopation,age);
this.id = id;
this.classId = classId;
}
//上课
public void goToClass(String className){
out.println(this.getName()+"去上"+className+"课");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
}
package five.schools.classes;
import static java.lang.System.*;
import five.schools.Person;
public class Teacher extends Person {
private String id;
private String curriculumName;
public Teacher(){};
public Teacher(String name, String gender, float height, float weight, String occopation, int age, String id, String curriculumName) {
super(name, gender, height, weight, occopation, age);
this.id = id;
this.curriculumName = curriculumName;
}
//教书
public void teaching(String classId,String curriculumName){
out.println(this.getName()+"在"+classId+"班上"+curriculumName+"课");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCurriculumName() {
return curriculumName;
}
public void setCurriculumName(String curriculumName) {
this.curriculumName = curriculumName;
}
}
4、改写第一题程序利用组合实现复用
package five;
import static java.lang.System.out;
/**
* 使用组合来复用Student类
*/
public class ReuseStudent {
private Student student;
public ReuseStudent(Student student){
this.student = student;
}
//吃
public void eating(String food){
student.eating(food);
}
//喝
public void drinking(String drink){
out.println(student.getName()+"正在喝"+drink);
}
//玩
public void playing(String game){
out.println(student.getName()+"正在玩"+game);
}
//睡
public void sleeping(){
out.println(student.getName()+"正在睡觉");
}
public static void main(String[] args) {
var stu1 = new Student("张三",12,"男","123","月之酒店","123");
ReuseStudent reuseStudent = new ReuseStudent(stu1);
reuseStudent.drinking("美年达");
}
}