每周测验

来源:互联网 发布:歌曲消音伴奏软件 编辑:程序博客网 时间:2024/05/01 15:53

每周测验

1.学生信息录入系统

要求可以录入多个学生的信息(包括学号\姓名\性别),录入结果存入Student对象中。录入完毕,找到所有包含“张”的学生,并打印学生对象的信息(可能有多个)

学生管理类:
import java.util.Scanner;/** * 思路:定义  * 1. 学生类:学号,姓名,性别   * 2. 工具类:查找学生信息  * 3. 在StudentInfo中建立student数组存储学生,进行测试 */public class StudentInfo {    public static Student[] stuArr = new Student[100];    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        while(true){// 提示输入学生信息            System.out.println("请录入学生学号:");            int id = scan.nextInt();            if(id == 0){                break;            }            System.out.println("请录入学生姓名:");            String name = scan.next();            System.out.println("请录入学生性别:");            String sex = scan.next();            Student stu = new Student(id,name,sex);            StudentUtil.enterInfo(stu); // 加入到学生信息管理系统            System.out.print("输入0结束录入!");        }        System.out.println("输入要查询的学生姓氏:");        String las = scan.next();        StudentUtil.queryInfo(las);        scan.close();    }}
学生类:
public class Student {    public static int num;    private int stuId;    private String name;    private String sex;    public Student(){        num++;    }    public Student(int stuId,String name,String sex){        this.stuId = stuId;        this.name = name;        this.sex = sex;        num++;    }    public int getStuId() {        return stuId;    }    public void setStuId(int stuId) {        this.stuId = stuId;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }}
工具类:
/** * 查询学生信息的方法,录入学生信息方法 */public class StudentUtil {    public static String queryInfo(String lastName){// 查询信息        String result = "";        for(int i=0;i<Student.num;i++){            char lName = StudentInfo.stuArr[i].getName().charAt(0);            if(lName == lastName.charAt(0)){                printInfo(StudentInfo.stuArr[i]);            }        }        return result;    }    public static void printInfo(Student student){// 打印学生信息        System.out.println("Student " + "[Id:" + student.getStuId() + " name:" + student.getName() + " sex:" + student.getSex() + " ]");    }    public static void enterInfo(Student student){// 录入学生信息        int i=0;        while(StudentInfo.stuArr[i] != null){            i++;        }        StudentInfo.stuArr[i] = student;    }}

2、写一个员工管理系统

员工包括:
行政(name、id、sal、car)、
技术(name、id、sal、team项目组、comm项目奖金)、
管理(name、id、sal、position、bounds分红)
写一个EmpUtil工具类,提供如下方法:
开除员工
计算员工年薪(包括技术人员的项目奖金,管理人员的分红)

公司类:
/** * 思路:定义 1.Company公司管理员工(测试类),员工数组 * 2.员工类Employee  * 3.行政类Administration继承Employee * 4.技术类Science继承Employee  * 5.管理类Manager继承Employee  * 6.工具类EmpUtil */public class Company {    public static Employee[] employees = new Employee[100];    public static void main(String[] args) {        Employee admin1 = new Administration("行政员",1001,3000);        Employee science = new Science("技术员", 1002, 5000, 10000);        Employee manager = new Manager("管理员",1003,8000,15000);        Employee admin2 = new Administration("行政员2",1004,3500);        EmpUtil.hire(admin);// 雇佣行政人员        EmpUtil.hire(science);// 雇佣技术人员        EmpUtil.hire(manager);// 雇佣管理人员//        EmpUtil.hire(adEmployee);// 测试选项        System.out.println(EmpUtil.getSal(admin));// 计算行政年薪        System.out.println(EmpUtil.getSal(science));// 计算技术年薪        System.out.println(EmpUtil.getSal(manager));// 计算管理年薪        System.out.println("员工有:");        for(int i=0;i<Employee.num;i++){            if(employees[i] == null){                continue;            }            System.out.print(employees[i].getName() + " ");        }        System.out.println();        EmpUtil.fire(manager);// 开除管理人员        System.out.println("开除了管理人员后员工有:");        for(int i=0;i<Employee.num;i++){            if(employees[i] == null){                continue;            }            System.out.print(employees[i].getName() + " ");        }    }}
员工类:
public class Employee {    public static int num;// 计算员工数量    protected String name;// 员工姓名    protected int id;// 员工id    protected double sal;// 员工月薪    public Employee(){    }    // 全参构造    public Employee(String name,int id,double sal){        this.name = name;        this.id = id;        this.sal = sal;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public double getSal() {        return sal;    }    public void setSal(double sal) {        this.sal = sal;    }}
行政人员类:
public class Administration extends Employee{// 继承员工类    private String car;// 私有属性    public Administration(){    }    public Administration(String name,int id,double sal){        super(name,id,sal);    }    public String getCar() {        return car;    }    public void setCar(String car) {        this.car = car;    }}
技术人员类:
/** * 技术(name、id、sal、team项目组、comm项目奖金)、 */public class Science extends Employee{// 继承员工类    private String team;// 私有属性    private double comm;    public Science(){    }    public Science(String name,int id,double sal,double comm){        super(name,id,sal);        this.comm = comm;    }    public String getTeam() {        return team;    }    public void setTeam(String team) {        this.team = team;    }    public double getComm() {        return comm;    }    public void setComm(double comm) {        this.comm = comm;    }}
管理人员类:
/** * 管理(name、id、sal、position、bounds分红) */public class Manager extends Employee{// 继承员工类    private String position;// 私有属性    private double bounds;    public Manager(){    }    public Manager(String name,int id,double sal,double bounds){        super(name,id,sal);        this.bounds = bounds;    }    public String getPosition() {        return position;    }    public void setPosition(String position) {        this.position = position;    }    public double getBounds() {        return bounds;    }    public void setBounds(double bounds) {        this.bounds = bounds;    }}
工具类:
/** *  写一个EmpUtil工具类,提供如下方法: * 开除员工 * 计算员工年薪(包括技术人员的项目奖金,管理人员的分红) */public class EmpUtil {    public static void fire(Employee employee){// 开除员工        for(int i=0;i<Employee.num;i++){            if(Company.employees[i].getId() == employee.getId()){                Company.employees[i] = null;            }        }    }    public static void hire(Employee employee){// 雇佣员工        int i=0;        while(Company.employees[i] != null){            i++;        }        Company.employees[i] = employee;        Employee.num++;    }    public static double getSal(Employee employee){// 计算年薪        // 判断计算的员工的工作        if(employee instanceof Administration){            Administration admin = (Administration) employee;            return 12*admin.getSal();// 年薪=12*月薪        }else if(employee instanceof Science){            Science sci = (Science) employee;            return 12*sci.getSal()+sci.getComm();// 年薪=12*月薪+奖金        }else if(employee instanceof Manager){            Manager mang = (Manager) employee;            return 12*mang.getSal()+mang.getBounds();// 年薪=12*月薪+分红        }        return -1;// 数据错误!    }}

3、请说明重写和重载的区别

重载:发生在一个类中不同的方法之间,要求方法名相同,参数类型或参数顺序或参数个数不同
重写:发生在父类与子类方法之间,要求方法名相同,返回值相同,参数相同


4、请说明static和final的用法

**static :静态的类的,可以修饰方法或变量。
static修饰的方法或变量可以使用类名.来调用,受权限修饰符的限制,
static修饰的变量只在第一次创建对象是初始化一次,所有的对象共享一个变量**

**final:最终的,可以修饰类、方法或变量。final修饰的类不能被继承,final修饰的方法不能被重写,final修饰的变量不可改变,必须赋值,有三种赋值方法:
1)创建变量时直接赋值
2)在构造器中赋值
3)在一般代码块中赋值**

0 0
原创粉丝点击