Android stduio用布局方法实现姓名、年龄、身高
时间: 2024-02-01 13:03:14 浏览: 81
Android 用户个人信息状态布局显示
3星 · 编辑精心推荐
由于本题中涉及多个算法的实现,因此我们需要先定义一个进程类,用于存储进程的相关信息:
```java
public class Process {
private String name; // 进程名称
private int createTime; // 进程创建时间
private int executeTime; // 进程执行时间
private int startTime; // 进程开始执行的时间
private int finishTime; // 进程完成执行的时间
private int turnaroundTime; // 进程周转时间
private double weightedTurnaroundTime; // 进程带权周转时间
public Process(String name, int createTime, int executeTime) {
this.name = name;
this.createTime = createTime;
this.executeTime = executeTime;
}
// 省略getter和setter方法
}
```
接下来,我们可以先手动创建几个进程,方便测试各种算法的实现:
```java
public class Main {
public static void main(String[] args) {
List<Process> processList = new ArrayList<>();
processList.add(new Process("P1", 0, 3));
processList.add(new Process("P2", 2, 6));
processList.add(new Process("P3", 4, 4));
processList.add(new Process("P4", 6, 2));
}
}
```
当然,我们也可以编写一个方法随机生成几个进程:
```java
public static List<Process> generateProcesses(int num) {
List<Process> processList = new ArrayList<>();
Random random = new Random();
for (int i = 1; i <= num; i++) {
processList.add(new Process("P" + i, random.nextInt(10), random.nextInt(10) + 1));
}
return processList;
}
```
接下来,我们可以依次实现各种调度算法的实现。
1. 先来实现先来先服务(FCFS)算法:
```java
public static void fcfs(List<Process> processList) {
int currentTime = 0;
for (Process process : processList) {
if (process.getCreateTime() > currentTime) {
currentTime = process.getCreateTime();
}
process.setStartTime(currentTime);
process.setFinishTime(currentTime + process.getExecuteTime());
process.setTurnaroundTime(process.getFinishTime() - process.getCreateTime());
process.setWeightedTurnaroundTime((double) process.getTurnaroundTime() / process.getExecuteTime());
currentTime = process.getFinishTime();
}
}
```
2. 其次是最短作业优先(SJF)算法:
```java
public static void sjf(List<Process> processList) {
List<Process> sortedList = new ArrayList<>(processList);
sortedList.sort(Comparator.comparing(Process::getExecuteTime));
fcfs(sortedList);
}
```
3. 再然后是优先级调度算法:
```java
public static void priority(List<Process> processList) {
List<Process> sortedList = new ArrayList<>(processList);
sortedList.sort(Comparator.comparing(Process::getCreateTime));
int currentTime = 0;
while (!sortedList.isEmpty()) {
int minPriority = Integer.MAX_VALUE;
Process minProcess = null;
for (Process process : sortedList) {
if (process.getCreateTime() <= currentTime && process.getExecuteTime() > 0 && process.getExecuteTime() < minPriority) {
minPriority = process.getExecuteTime();
minProcess = process;
}
}
if (minProcess == null) {
currentTime++;
} else {
minProcess.setStartTime(currentTime);
minProcess.setFinishTime(currentTime + minProcess.getExecuteTime());
minProcess.setTurnaroundTime(minProcess.getFinishTime() - minProcess.getCreateTime());
minProcess.setWeightedTurnaroundTime((double) minProcess.getTurnaroundTime() / minProcess.getExecuteTime());
currentTime = minProcess.getFinishTime();
sortedList.remove(minProcess);
}
}
}
```
4. 最高响应比优先(HRRN)算法:
```java
public static void hrrn(List<Process> processList) {
List<Process> sortedList = new ArrayList<>(processList);
sortedList.sort(Comparator.comparing(Process::getCreateTime));
int currentTime = 0;
while (!sortedList.isEmpty()) {
double maxRatio = Double.MIN_VALUE;
Process maxProcess = null;
for (Process process : sortedList) {
if (process.getCreateTime() <= currentTime && process.getExecuteTime() > 0) {
double ratio = (currentTime - process.getCreateTime() + process.getExecuteTime()) / (double) process.getExecuteTime();
if (ratio > maxRatio) {
maxRatio = ratio;
maxProcess = process;
}
}
}
if (maxProcess == null) {
currentTime++;
} else {
maxProcess.setStartTime(currentTime);
maxProcess.setFinishTime(currentTime + maxProcess.getExecuteTime());
maxProcess.setTurnaroundTime(maxProcess.getFinishTime() - maxProcess.getCreateTime());
maxProcess.setWeightedTurnaroundTime((double) maxProcess.getTurnaroundTime() / maxProcess.getExecuteTime());
currentTime = maxProcess.getFinishTime();
sortedList.remove(maxProcess);
}
}
}
```
最后,我们可以在main方法中调用各种算法并输出结果:
```java
public static void main(String[] args) {
List<Process> processList = generateProcesses(4);
System.out.println("FCFS:");
fcfs(processList);
printResult(processList);
System.out.println("SJF:");
sjf(processList);
printResult(processList);
System.out.println("Priority:");
priority(processList);
printResult(processList);
System.out.println("HRRN:");
hrrn(processList);
printResult(processList);
}
public static void printResult(List<Process> processList) {
int totalTurnaroundTime = 0;
double totalWeightedTurnaroundTime = 0;
for (Process process : processList) {
totalTurnaroundTime += process.getTurnaroundTime();
totalWeightedTurnaroundTime += process.getWeightedTurnaroundTime();
System.out.println(process);
}
System.out.println("Total turnaround time: " + totalTurnaroundTime);
System.out.println("Average turnaround time: " + (double) totalTurnaroundTime / processList.size());
System.out.println("Average weighted turnaround time: " + totalWeightedTurnaroundTime / processList.size());
}
```
完整代码如下:
阅读全文