Java学习
- 引语
- 案例
- 实现过程
- 思路
- 代码
- 结果
引语
记录学习路程,抛砖引玉。如有更好的算法或者出现错误,欢迎指点。
案例
- 记录学生所在宿舍的楼号、房编号
- 记录学生的名称、学号、性别、年龄以及住宿时间
- 注意:学号、年龄以及住宿时间不可为负,年龄合理区间设为10~100
实现过程
思路
- 首先定义两个类:Room和Student
- 然后分别定义各自的属性。Room的属性有楼栋名称、宿舍房号、住宿时间;Student的属性有名称、学号、性别、年龄
- 接着处理有关的注意事项,完善属性功能
- 最后定义个test类进行功能测试,完成案例实现
代码
Room:
package roomate;
public class Room {
// 成员属性:宿舍名称、宿舍编号、住宿时间、学生信息、学生个数
private String roomName; //private设置属性在此类私有,提高安全合法性
private String roomNo;
private int roomLife;
private Student[] myStudents;
private int studentNum;
// 无参构造方法
public Room() {
}
// 带参构造,带参构造,实现对全部属性的赋值
public Room(String roomName, String roomNo, int roomLife) {
// this.roomName=roomName;安全起见,使用以下的方法赋值
this.setRoomName(roomName);//楼栋名称
this.setRoomNo(roomNo);//宿舍编号
this.setRoomLife(roomLife);//住宿年期
}
public void setRoomName(String roomName) {
this.roomName = roomName;
}
public String getRoomName() {
return this.roomName;
}
public String getRoomNo() {
return roomNo;
}
public void setRoomNo(String roomNo) {
this.roomNo = roomNo;
}
public int getRoomLife() {
return roomLife;
}
// 设置年限,限制必须>0
public void setRoomLife(int roomLife) {
if (roomLife <= 0)
// system.out.println("error!");
return;
this.roomLife = roomLife;
}
/**
* 获取宿舍学生信息 如果保存学生信息的数组未被初始化,则,先初始化长度200
* @return 保存学生信息的数组
*/
public Student[] getMyStudents() {
if(this.myStudents==null)
this.myStudents=new Student[200];
return myStudents;
}
public void setMyStudents(Student[] myStudents) {
this.myStudents = myStudents;
}
public int getStudentNum() {
return studentNum;
}
public void setStudentNum(int studentNum) {
this.studentNum = studentNum;
}
/**
* 宿舍介绍的方法
* @return 介绍宿舍的相关信息,包括楼栋名称、宿舍编号、住宿年期
*/
public String info() {
String str = "宿舍信息如下:\n宿舍名称:" + this.getRoomName() + "\n宿舍编号:" + this.getRoomNo() + "\n住宿年限:"
+ this.getRoomLife() + "年";
return str;
}
public void addStudent(Student stu){
/*
* 1、将学生保存到数组中
* 2、将学生个数保存到studentNum
* */
//1、将学生保存到数组中
for(int i=0;i<this.getMyStudents().length;i++){
if(this.getMyStudents()[i]==null){
stu.setStudentRoom(this);
this.getMyStudents()[i]=stu;
//2、将学生个数保存到studentNum
this.studentNum=i+1;
return;
}
}
}
}
Student:
package roomate;
import roomate.Room;
public class Student {
// 成员属性:学号、姓名、性别、年龄、宿舍
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
private Room studentRoom;
// 无参构造方法
public Student() {
}
//多参构造方法,实现对学号、姓名、性别、年龄的赋值
public Student(String studentNo, String studentName, String studentSex, int studentAge) {
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
}
// 多参构造方法,实现对全部属性的赋值
/* public Student(String studentNo, String studentName, String studentSex, int studentAge,Room studentRoom) {
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
// this.studentAge=studentAge;
this.setStudentRoom(studentRoom);
}
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
// 限制性别只能是“男”或者“女”,反之,强制赋值为“男”
this.studentSex = studentSex;
}
public int getStudentAge() {
return studentAge;
}
/**
* 给年龄赋值,限定必须在10--100之间,反之赋值为0
*
* @param studentAge
* 传入的年龄
*/
public void setStudentAge(int studentAge) {
if (studentAge < 10 || studentAge > 100)
this.studentAge = 0; //年龄超100及低10判定为出错,使其值为0
else
this.studentAge = studentAge;
}
/**
* 获取宿舍对象,如果没有实例化,先实例化后再返回
* @return 宿舍对象信息
*/
public Room getStudentRoom() {
if(this.studentRoom==null)
this.studentRoom=new Room();
return studentRoom;
}
public void setStudentRoom(Room studentRoom) {
this.studentRoom = studentRoom;
}
/**
* 学生自我介绍的方法
*
* @return 自我介绍的信息,包括姓名、学号、性别、年龄
*/
public String introduction() {
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge()+ "\n所在宿舍名称:" + this.getStudentRoom().getRoomName() + "\n住宿年限:"
+ this.getStudentRoom().getRoomLife();
return str;
}
public String introduction(String roomName,String roomNo, int roomLife) {
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所在宿舍名称:" + roomName + "\n宿舍编号:"+roomNo + "\n住宿年限:"
+ roomLife;
return str;
}
/**
* 学生自我介绍的方法
* @param myRoom 所在宿舍的对象
* @return自我介绍的信息,包括姓名、学号、性别、年龄、所在宿舍名称、住宿年限
*/
public String introduction(Room myRoom){
String str = "学生信息如下:\n姓名:" + this.getStudentName() + "\n学号:" + this.getStudentNo() + "\n性别:"
+ this.getStudentSex() + "\n年龄:" + this.getStudentAge() + "\n所在宿舍名称:" + myRoom.getRoomName() + "\n住宿年限:"
+ myRoom.getRoomLife()+"\n宿舍编号:"+myRoom.getRoomNo();
return str;
}
}
StudentTest:
package test;
import roomate.Room;
import roomate.Student;
public class StudentTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//测试Subject
Room sub1=new Room("K","J0001",2);//宿舍类
System.out.println(sub1.info());
System.out.println("================================");
//测试Student,学生类
Student stu1=new Student("S01","张三","男",200); //刻意让年龄不满足10~100区间
System.out.println(stu1.introduction("K","J0003",4));
System.out.println("================================");
Student stu2=new Student("S02","李四","女",17);
System.out.println(stu2.introduction("L", "J0002",3));
System.out.println("================================");
Student stu3=new Student("S03","王五","男",18);
System.out.println(stu3.introduction(sub1));
System.out.println("================================");
//统计住宿学生人数
sub1.addStudent(stu1);
sub1.addStudent(stu2);
sub1.addStudent(stu3);
System.out.println("宿舍中已有"+sub1.getStudentNum()+"学生正在住宿");
}
}
结果
宿舍信息如下:
宿舍名称:K
宿舍编号:J0001
住宿年限:4年
================================
学生信息如下:
姓名:张三
学号:S01
性别:男
年龄:0 //因为年龄200不符合条件所以输出显示值为0
所在宿舍名称:K
宿舍编号:J0003
住宿年限:4
================================
学生信息如下:
姓名:李四
学号:S02
性别:女
年龄:17
所在宿舍名称:L
宿舍编号:J0002
住宿年限:3
================================
学生信息如下:
姓名:王五
学号:S03
性别:男
年龄:18
所在宿舍名称:K
住宿年限:2
宿舍编号:J0001
================================
宿舍中已有3学生正在住宿
Process finished with exit code 0