1.编写一个Worker类,为Worker类添加相应的代码,使得Worker对象能正确放入TreeSet中。并编写相应的测试代码。
/**
* Worker类
*/
public class Worker implements Comparable<Worker>{
/**
* 私有属性
*/
private String name;
private int age;
private double salary;
/**
* 构造方法
*/
public Worker() {
} public Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
/**
* getter setter方法
*/
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 double getSalary() {
return salary;
} public void setSalary(double salary) {
this.salary = salary;
} /**
* 重写toString方法
*/
public String toString(){
return "工人信息 : { "+"姓名 = "+name+","+"年龄 = "+age+","+"薪水 = "+salary+" }";
} /**
* 重写compareTo方法:按年龄从小到大排序,年龄相同按名字的字典顺序排列
*/
@Override
public int compareTo(Worker other) {
if (this.age != other.age){
return this.age - other.age;
}else {
return this.name.compareTo(other.getName());
}
}
}
/**
* 测试类
*/
public class TestWorker {
public static void main(String[] args) {
/**
* 创建Worker对象,通过构造方法完成赋值
*/
Worker w1 = new Worker("F",27,5500);
Worker w2 = new Worker("E",28,6500);
Worker w3 = new Worker("B",26,5800);
Worker w4 = new Worker("A",26,5300);
/**
* 创建TreeSet集合存放工人数据信息
*/
Set<Worker> set = new TreeSet<Worker>();
set.add(w1);
set.add(w2);
set.add(w3);
set.add(w4);
/**
* 遍历Set,输出结果
*/
for(Worker w : set){
System.out.println(w);
}
}
}
运行结果:
2.设计一个多线程的程序如下:设计一个火车售票模拟程序。假如火车站要有100张火车票要卖出,现在有10个售票窗口同时售票,用10个线程模拟这10个售票窗口的售票情况。
/**
* 定义售票类实现Runnable接口
*/
public class SaleTicket implements Runnable {
/**
* 定义初始变量
* total--总票数
* count--票号(从0开始)
*/
public int total = 100;
public int count = 0; /**
* 重写run()方法
*/
@Override
public void run() {
while (total > 0){
synchronized (this){
if (total > 0){
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 票号++
* 总票数--
*/
count++;
total--;
System.out.println(Thread.currentThread().getName()+"售出票号为:"+count+
" 的票"+"当前余票为:"+total);
}
}
}
}
}
/**
* 测试类
*/
public class TestSaleTicket {
public static void main(String[] args) {
/**
* 创建线程对象,启动线程
*/
SaleTicket st = new SaleTicket();
/**
* 使用循环创建启动线程
*/
for (int i = 1;i <= 5;i++){
Thread t = new Thread(st,i+"号售票窗口");
t.start();
}
/**
* 也可以手动创建5个线程并启动,代码稍显复杂
*/
SaleTicket st1 = new SaleTicket();
Thread t1 = new Thread(st1);
Thread t2 = new Thread(st1);
Thread t3 = new Thread(st1);
Thread t4 = new Thread(st1);
Thread t5 = new Thread(st1);
t1.setName("窗口6");
t2.setName("窗口7");
t3.setName("窗口8");
t4.setName("窗口9");
t5.setName("窗口10");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
运行结果:
**各线程随机执行售票功能
3.对一个数组进行折半查找,使用递归和非递归实现。
/**
* 递归实现数组的折半查找
*/
public class TestRecursionBinarySearch {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
int key = 1;
int index = recursionBinarySearch(array,key);
System.out.println(key+"的索引是"+index);
}
public static int recursionBinarySearch(int[] arr,int key){
int start = 0;
int end = arr.length - 1;
return recursionBinarySearch(arr,start,end,key);
} private static int recursionBinarySearch(int[] arr, int start, int end, int key) {
/**
* 递归的结束条件
*/
if (start > end){
return -1;
}
/**
* 计算中间索引并获得其值
*/
int mid = (start + end) / 2;
int value = arr[mid];
/**
* 进行比较查询
*/
if (key > value){
start = mid + 1;
return recursionBinarySearch(arr,start,end,key);
}else if (key < value){
end = mid - 1;
return recursionBinarySearch(arr,start,end,key);
}else {
return mid;
}
}
}
运行结果:
/**
* 非递归实现数组的折半查找
*/
public class TestBinarySearch {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
int key = 7;
int index = binarySearch(array,key);
System.out.println(key+"的索引是"+index);
}
public static int binarySearch(int[] arr,int key){
/**
* 定义起始位置和结束位置索引号,第一次查找从两头开始(即start和end)
*/
int start = 0;
int end = arr.length - 1;
//System.out.println("-1 代表要查的元素不在此数组中");
while (start <= end){
/**
* 计算中间索引号,得到对应元素值
*/
int mid = (start + end) / 2;
int value = arr[mid];
/**
* 将要查找的元素key值和中间元素比较
*/
if (key == value){
return mid;
}else if (key < value){
end = mid - 1;
}else {
start = mid + 1;
}
}
return -1;
}
}
运行结果: