11.使用Object数组存储员工数据
package com.liubj.projectbaizhan;
/**
* 员工属性类
* @author liubaojun
*
*/
public class Employee {
private int id; // 工号
private String name; // 姓名
private int age; // 年龄
private String job; // 岗位
private double salary; // 薪资
private double subsidy; // 补助
// 无参构造方法
public Employee() {
super();
}
// 有参构造方法
public Employee(int id, String name, int age, String job, double salary, double subsidy) {
super();
this.id = id;
this.name = name;
this.age = age;
this.job = job;
this.salary = salary;
this.subsidy = subsidy;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
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 getJob(){
return job;
}
public void setJob(String job){
this.job = job;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSubsidy(){
return subsidy;
}
public void setSubsidy(double subsidy){
this.subsidy = subsidy;
}
//重写toString()
public String toString() {
return id + "\t" + name + "\t" + age + "\t" + job + "\t" + salary + "\t" + subsidy;
}
}
package com.liubj.projectbaizhan;
/**
* 使用Object数组存储员工数据
* @author liubaojun
*
*/
public class TestEmployee {
public static void main(String[] args) {
//创建一个Object数组
Object[] obj = new Object[3];
//创建员工对象
Employee emp1 = new Employee(1001,"张三",18,"辅导员",6000.0,1000.0);
Employee emp2 = new Employee(1002,"李四",20,"讲师",8000.0,2000.0);
Employee emp3 = new Employee(1003,"王五",28,"教授",10000.0,4000.0);
obj[0] = emp1;
obj[1] = emp2;
obj[2] = emp3;
System.out.println("工号\t姓名\t年龄\t职位\t工资\t补助");
System.out.println(obj[0]);
System.out.println(obj[1]);
System.out.println(obj[2]);
}
}
运行结果:
12.写出冒泡排序代码,并用自己的语言描述整个过程。
package com.liubj.projectbaizhan;
import java.util.Arrays;
import java.util.Scanner;
/**
* 写出冒泡排序代码,并用自己的语言描述整个过程。
*
* @author liubaojun
*
*/
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入数组的长度:");
// 创建一个数组
int[] arr = new int[sc.nextInt()];
// 数组初始化
init(arr);
// 初始数组
System.out.println("初始数组: " + Arrays.toString(arr));
// 冒泡排序
bubbleSort(arr);
// 排序后的数组
System.out.println("冒泡排序后的数组:" + Arrays.toString(arr));
}
// 对数组进行随机赋值,初始化
public static void init(int[] arr) {
// 对数组元素循环赋值
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 100) + 1;
}
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
//总比较轮数
for(int i = 1; i < arr.length-1; i++){
//每一轮需要比较的次数
for(int j = 0; j <arr.length -i; j++){
//如果前一个数比后一个数大,则交换位置
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] =temp;
}
}
}
}
}
运行结果: