案例练习

  • 一、选择
  • 二、编程


一、选择

  1. 执行下面代码后,哪几个结论是正确的
    A. f[0]   B. f[0] = 0.0   C. 编译失败   D. 在运行时抛出异常
public class Test {
	private static float[] f = new float[2];
	public static void main(String[] args) {
		System.out.println("f[0]=" + f[0]);
	}
}

B

  1. 执行下面代码后,哪几个结论是正确的(多选)
          String[ ] s = new String[10];
    A. s[9]为null   B. s[10]的内容为空字符串   C. 没有s[0]   D. s.length=10

A D

二、编程

题目要求: 某公司要开发内部的 “办公信息化管理系统”,请使用面向对象的思想描述以下员工信息。
由场景和运行效果,可以分析出项目中可以抽取如下类

  • 部门类:
  • 类型描述:能够描述部门编号、部门名称、员工数组、统计部门中的员工个数
  • 要求:设定方法统计该部门员工个数
  • 提示:部门类的属性有四个,分别是部门编号,部门名称,员工数组 和 统计变量 ,具体是市场 部还是人事部,是通过部门类的对象来表示的,如果是市场部的对象,那么添加并统计的就是市 场部的人数,同样如果是人事部的对象,添加并统计的就是人事部的人数
  • 职务类:
  • 类型描述:能够描述职务编号、职务名称
  • 员工类:
  • 类型描述:能够描述员工姓名、工号、年龄、性别、所属部门、职务信息
  • 要求: 设定方法限定年龄只能是18–65之间,反之则设置默认为18岁 设定方法限定性别只能是“男”或者“女”,反之则设置默认为"男" 设定方法,实现员工自我介绍信息,将员工信息作为字符串返回
  • 测试类: 类型描述:测试程序,并参照效果图输出结果
package com.blog.system.demo;
/**
 * 部门类
 * @author springer
 */
public class Department {
	//私有属性
	private String departmentNO;
	private String departmentName;
	private Employee[] employee; //员工数组
	private int employeenum; //员工人数
	//get/set方法
	public String getDepartmentNO() {
		return departmentNO;
	}
	public void setDepartmentNO(String departmentNO) {
		this.departmentNO = departmentNO;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	public Employee[] getEmployee() {
		if(employee == null) {
			this.employee = new Employee[20];
		}
		return employee;
	}
	public void setEmployee(Employee[] employee) {
		this.employee = employee;
	}
	public int getEmployeenum() {
		return employeenum;
	}
	public void setEmployeenum(int employeenum) {
		this.employeenum = employeenum;
	}
	//有参构造器
	public Department(String departmentNO, String departmentName) {
		super();
		this.setDepartmentNO(departmentNO);
		this.setDepartmentName(departmentName);
		this.setEmployee(employee);
		this.setEmployeenum(employeenum);
	}
	//无参构造器
	public Department() {
		
	}
	/**
	 * 将传入的人员依次保存到成员数组中
	 * 传出员工数量
	 * @param employeeName 传入成员
	 * @return 员工人数
	 */
	public void Employeenum(Employee employee) {
		int i;
		for(i = 0; i < this.getEmployee().length; i++) {
			if(this.getEmployee()[i] == null) {
				this.getEmployee()[i] = employee;
				break;
			}
		}
		this.setEmployeenum(i + 1);
	}
	/**
	 * 描述部门信息的方法
	 * @return 部门信息字符串
	 */
	public String DeInformation() {
		String str = "部门编号:" + this.getDepartmentNO();  
		str += "\n部门名称:" + this.getDepartmentName(); 
		str += "\n员工人数:" + this.getEmployeenum(); 
		str += "\n==============================";
		return str;
	}	
}
package com.blog.system.demo;
/**
 * 职务类
 * @author springer
 *
 */
public class Position {
	//建立属性
	private String positionNo;
	private String positionName;
	//get/set方法
	public String getPositionNo() {
		return positionNo;
	}
	public void setPositionNo(String positionNo) {
		this.positionNo = positionNo;
	}
	public String getPositionName() {
		return positionName;
	}
	public void setPositionName(String positionName) {
		this.positionName = positionName;
	}
	//属性有参构造器
	public Position(String positionNo, String positionName) {
		super();
		this.setPositionNo(positionNo);
		this.setPositionName(positionName);
	}
	//无参构造器
	public Position() {

	}
	/**
	 * 描述职务编号和名称的方法
	 * @return 职务信息字符串
	 */
	public String PoInformation() {
		String str = "职务编号:" + this.getPositionNo();  
		str += "\n职务名称:" + this.getPositionName(); 
		str += "\n==============================";
		return str;
	}
}
package com.blog.system;
/**
 * 员工类
 * @author springer
 *
 */
public class Employee {
	//建立私有属性
	private String employeeName;
	private String employeeNo;
	private String employeeSex;
	private int employeeAge;
	private Department departmentName;
	private Position positionName;
	//get/set方法
	public String getEmployeeName() {
		return employeeName;
	}
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	public String getEmployeeNo() {
		return employeeNo;
	}
	public void setEmployeeNo(String employeeNo) {
		this.employeeNo = employeeNo;
	}
	public String getEmployeeSex() {
		return employeeSex;
	}
	/**
	 * 性别限定为男或女,反之,默认为男
	 * @param employeeSex
	 */
	public void setEmployeeSex(String employeeSex) {
		if(this.equals("男") || this.equals("女")) {
			this.employeeSex = employeeSex;
		} else {
			this.employeeSex = "男";
		}
	}
	public int getEmployeeAge() {
		return employeeAge;
	}
	/**
	 * 规定年龄为18-65之间,反之,默认为18岁
	 * @param employeeAge
	 */
	public void setEmployeeAge(int employeeAge) {
		if(employeeAge > 18 && employeeAge <65) {
			this.employeeAge = employeeAge;
		} else {
			this.employeeAge = 18;
		}	
	}
	public Department getDepartmentName() {
		if(departmentName == null) {
			this.departmentName = new Department();
		}
		return departmentName;
	}
	public void setDepartmentName(Department departmentName) {
		this.departmentName = departmentName;
	}
	public Position getPositionName() {
		if(positionName == null) {
			this.positionName = new Position();
		}
		return positionName;
	}
	public void setPositionName(Position positionName) {
		this.positionName = positionName;
	}
	//属性有参构造器
	/**
	 * 传入员工对象属性
	 * 员工人数计数
	 * @param employeeName  
	 * @param employeeNo
	 * @param employeeSex
	 * @param employeeAge
	 * @param department  传入部门对象,在构造器中获取对应属性
	 * @param position    传入职务对象,在构造器中获取对应属性
	 */
	public Employee(String employeeName, String employeeNo, String employeeSex, int employeeAge,
			Department department, Position position) {
		super();
		this.setEmployeeName(employeeName);
		this.setEmployeeNo(employeeNo);
		this.setEmployeeSex(employeeSex);
		this.setEmployeeAge(employeeAge);
		//获取部门对象、职务对象的名称属性
		this.setDepartmentName(department);
		this.setPositionName(position);
		//每次传入员工,调用计数方法,累计对应部门的人数
		department.Employeenum(this);
	}
	//无参构造器
	public Employee() {

	}
	/**
	 * 存入员工信息的方法
	 * @return 员工信息字符串
	 */
	public String EmInformation() {
		String str = "姓名:" + this.getEmployeeName();  
		str += "\n工号:" + this.getEmployeeNo(); 
		str += "\n性别:" + this.getEmployeeSex();
		str += "\n年龄:" + this.getEmployeeAge();
		//方案三,建立sub对象属性
		str += "\n职务:" + this.getDepartmentName().getDepartmentName() + this.getPositionName().getPositionName(); 
		str += "\n==============================";
		return str;
	}
}
package com.blog.system.demo;
/**
 * 测试类
 * @author springer
 *
 */
public class Test {
	public static void main(String[] args) {
		//初始化职务
		Position p1 = new Position("P001","经理");
		Position p2 = new Position("P002","助理");
		Position p3 = new Position("P003","职员");
		//初始化部门
		Department d1 = new Department("D001","人事部");
		Department d2 = new Department("D002","市场部");
		//传入员工
		Employee e1 = new Employee("张铭","S001","男",29,d1,p1);
		System.out.println(e1.EmInformation());
		Employee e2 = new Employee("李艾爱","S002","女",21,d1,p2);
		System.out.println(e2.EmInformation());
		Employee e3 = new Employee("孙超","S003","男",29,d1,p3);
		System.out.println(e3.EmInformation());
		Employee e4 = new Employee("张美美","S004","女",26,d2,p3);
		System.out.println(e4.EmInformation());
		Employee e5 = new Employee("蓝迪","S005","男",37,d2,p1);
		System.out.println(e5.EmInformation());
		Employee e6 = new Employee("张铭","S006","男",29,d2,p1);
		System.out.println(e6.EmInformation());
		//各部门人数
		System.out.println(d1.getDepartmentName() + "总共有" + d1.getEmployeenum() + "名员工");
		System.out.println(d2.getDepartmentName() + "总共有" + d2.getEmployeenum() + "名员工");
	}
}