gpt4 book ai didi

java - 使用 Quantum 和 Produce Table 进行循环调度

转载 作者:行者123 更新时间:2023-11-30 06:17:30 25 4
gpt4 key购买 nike

我想进行循环调度。我已经设置了数量量子 = 2。它将生成“时间”和“额外”

时间和额外时间取决于突发时间。量子 = 2。突发时间 = 10。因此,时间为 2,额外为 8。如果突发时间 = 1,则时间为 1,额外为 0。如果突发时间 = 2,则时间 = 2,额外 = 0。

这是我的作品,我有点卡在那里。我不知道如何生成时间和额外的内容。

Figure 1: Table Should Produce Like This

package roundrobinscheduling;
import java.util.Scanner;
public class RoundRobinScheduling {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int noOfProcess = 0;
int quantum = 2;

System.out.print("Number of Process: ");
noOfProcess = input.nextInt();

int[] burstTime = new int[noOfProcess];
int[] priority = new int[noOfProcess];
String[] process = new String[noOfProcess];
int[] waitingTime = new int[noOfProcess];

for (int i = 0; i < noOfProcess; i++) {
System.out.print("Name Process: ");
process[i] = input.next();

System.out.print("Burst Time: ");
burstTime[i] = input.nextInt();

System.out.print("Priority: ");
priority[i] = input.nextInt();
}

int[] temp = new int[0];
for (int k = 0; k < noOfProcess; k++) {

}

int[] baki = new int[0];
int bakitemp = 0;
System.out.println("|--Process--|--Burst Time--|--Priority--|--Time--|--Extra--|");

for (int j = 0; j < noOfProcess; j++) {

if (burstTime[j] == quantum) {
baki[j] = bakitemp;
time[j] = burstTime[j];

} else if (burstTime[j] > quantum) {
//burstTime[j] = burstTime[j] - quantum;
baki[j] = burstTime[j] - quantum;
time[j] = quantum;
} else {
baki[j] = bakitemp;
time[j] = burstTime[j];

}

if (j > 0) {
waitingTime[j] = waitingTime[j - 1] + burstTime[j-1];
}

System.out.println("| " + process[j] + " | " + burstTime[j] + " | " + priority[j] + " | " + time[j] + " | " + baki[j] + " | " + " A " + waitingTime[j]);

}

}}
<小时/>

新任务:我有一项作业需要在本周日之前提交。如果您想要完整的问题,没问题。但我想一一弄清楚,如果我能做到的话。 “如果用户输入有效的帐号和正确的 PIN帐户后,屏幕显示主菜单。如果用户输入无效帐号或 PIN 码不正确,屏幕会显示相应的信息消息,则ATM返回步骤1重新开始认证过程。“那么,如何让数组存储系统中的账号和密码呢?当我们在保存时输入正确的帐号和密码后,我们就可以继续。我想创建至少 3 个帐号。我希望你不介意帮助我。

帐户类别:将其添加到与其他 ATM 类别相同的包中

class Account {

private final int accountNumber; // A 5 digit number
private final int pinNumber; // A 5 digit number
private final String accountName; // The first Name
private double accountBalance; // The money in the account

public Account(int accountNumber, int pinNumber, String accountName) {
super();
this.accountNumber = accountNumber;
this.pinNumber = pinNumber;
this.accountName = accountName;
accountBalance = 0.0;
}

public double getAccountBalance() {
return accountBalance;
}

public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}

public int getAccountNumber() {
return accountNumber;
}

public int getPinNumber() {
return pinNumber;
}

public String getAccountName() {
return accountName;
}

public void debitAccount(double amount) {
accountBalance -= amount;
}

public void creditAccount(double amount) {
accountBalance += amount;
}

}

AccountService 类:

class AccountService {
Account[] accounts;

public AccountService() {
accounts = new Account[3];
accounts[0] = new Account(12345, 123, "Eisom");
accounts[1] = new Account(98765, 321, "Hazman");
accounts[2] = new Account(14123, 456, "Aina");
accounts[0].setAccountBalance(800.00);
accounts[1].setAccountBalance(650.00);
accounts[2].setAccountBalance(500.00);
}

public boolean isValidAccount(int accountNumber, int pinNumber) {

for (int j = 0; j < accounts.length; j++) {
if ((accountNumber == accounts[j].getAccountNumber()) && (pinNumber == accounts[j].getPinNumber())) {
return true;
}
}
return false;

}

public Account getAccount(int accountNumber, int pinNumber) {

for (int j = 0; j < accounts.length; j++) {
if ((accountNumber == accounts[j].getAccountNumber()) && (pinNumber == accounts[j].getPinNumber())) {
return accounts[j];
}
}
return null;
}
}

现在您必须在 ATM 程序中使用 AccountService 类:

// Now the ATM has an account
private Account account;

// The service to lookup the account based on account number and pin
AccountService service = new AccountService();

// New loop using account service
while (true) {
MenuDisplay();
InputAccountNumber();

boolean isValid = service.isValidAccount(accountNumber, pinNumber);
if (isValid) {
account = service.getAccount(accountNumber, pinNumber);
Menu();
} else
System.out.println("Wrong Account Number/PIN Number. Please Try Again.");
}

现在您的其余代码必须使用account。帐户获取者和设置者可以访问该人的姓名、余额。示例:

public void BalanceEnquiries() {
System.out.println("");
System.out.println("-------BALANCE ENQUIRIES-------");
System.out.println("User Account Balance: RM" + account.getAccountBalance());
System.out.println("-------------------------------");
System.out.println("");
System.out.println("Proceeding back to Main Menu...");
System.out.println("");

}

请告诉我到目前为止这是否有意义。

以下是提款示例:

public void attemptWithdrawal(double amount) {

if (account.getAccountBalance() < amount) {
System.out.println("Not Insuffient...");
System.out.println("You put exceed from your account balance. Please try again");
System.out.println("Please put lower amount.");
return;
}

System.out.printf("Withdrawal RM%.2f?\n", amount);
moneyAfterWithdrawal = account.getAccountBalance()-amount;
System.out.printf("psst: your remaining account will be RM%.2f\n", moneyAfterWithdrawal);
System.out.println("[Y]Yes [N]No");
confirmWithdrawal = input.next().charAt(0);

if ((confirmWithdrawal == 'Y') || (confirmWithdrawal == 'y')) {
System.out.println("Congrate! You successfully withdrawal the money");
realMoney -= moneyAfterWithdrawal;
account.debitAccount(amount); // Take the money out
} else if ((confirmWithdrawal == 'N') || (confirmWithdrawal == 'n')) {
// Going back to this method
System.out.println("");
System.out.println("Proceeding to Others again...");
} else {
System.out.println("");
System.out.println("Wrong Character...");
}

}

public void Withdrawal() {
System.out.println("");
System.out.println("-------WITHDRAWAL-------");
System.out.println(" [1]RM50.00 [2]RM100.00");
System.out.println(" [3]Others [4]Exit");
System.out.println("------------------------");
System.out.print("Enter the Code: ");
chooseOptionWithdrawalString = input.next();
chooseOptionWithdrawal = Integer.parseInt(chooseOptionWithdrawalString);

switch (chooseOptionWithdrawal) {
case 1:
attemptWithdrawal(50.0);
break;
case 2:
attemptWithdrawal(100.0);
break;
case 3:
System.out.println("How many money you want to withdrawal: ");
moneyWithdrawal = input.nextDouble();
attemptWithdrawal(moneyWithdrawal);
break;
case 4:
break;
default:
System.out.println("");
System.out.println("You enter wrong input. Please try again.");
System.out.println("");
break;
}
}
<小时/>

注释:1. 帐号和密码。

a) 使用字符串会更容易。但整数没问题。b) PIN 码必须为 5 位数字(您正在使用 3 位数字)c) 目前允许账号100000(但不应该是6位)d) 除此之外,这部分很好!

  • 提款:
  • a) 如果您创建一个像 attemptWithdraw(double amount) 这样的新方法,将会对您有很大帮助。您有很多重复的代码(例如 50 美元、100 美元以及当用户选择其他金额时)。无论用户想要 50、100 还是 200,都应该调用完全相同的方法 (attemptWithdraw)。通过这种方法,您可以:a) 确保用户有足够的钱(如果没有,则警告他们)b) 确保 ATM 机上有足够的钱(如果 ATM 机上没有足够的钱,则警告他们)c) 借记/减少个人账户金额(如果a和b良好)d) 提醒用户取钱

    创建此额外方法后,请调用它以获取 50、100 等。您会发现它更容易,因为您只需编写一次相同的代码。

  • 存款
  • a) 我认为您不会检查 0 来退出。b) 该人的余额不会被扣除,因为您使用了新变量 MoneyAfterDeposit。您需要像使用 realMoney 一样使用 accountBalanceTemp。c) 我认为你需要告诉他们插入存款单。d) 有一个两分钟定时器;如果他们不这样做,您必须中止交易。

    我就到此为止。你有很多好的代码。只需再进行一些更改即可实现。

    最佳答案

    执行循环调度,没有任何初始优先级(即先 A,然后 B)。处理将继续,直到所有进程都没有剩余的突发。

    public class RoundRobinScheduling {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int noOfProcess = 0;
    int quantum = 2;

    System.out.print("Number of Process: ");
    noOfProcess = input.nextInt();

    int[] burstTime = new int[noOfProcess];
    int[] priority = new int[noOfProcess];
    String[] process = new String[noOfProcess];
    int waitingTime = 0;

    int processesComplete = 0;

    for (int i = 0; i < noOfProcess; i++) {
    System.out.print("Name Process: ");
    process[i] = input.next();

    System.out.print("Burst Time: ");
    burstTime[i] = input.nextInt();
    if (burstTime[i] == 0)
    processesComplete++;

    System.out.print("Priority: ");
    priority[i] = input.nextInt();
    }
    input.close();

    int roundRobinIndex = 0;

    System.out.println(" | Process | CPU Burst | Priority | Time | Waiting Time | ");
    while (processesComplete < noOfProcess) {
    if (burstTime[roundRobinIndex] > 0) {
    // Here we want to subtract the full quantum (2)
    // But what if burst time left = 1? Then we can't subtract 2
    // Math.min takes 2 arguments, and returns the smaller of the two
    // Math.min(2, 1) = 1
    // Math.min(2, 2) = 2
    // Math.min(2, 3) = 2
    int time = Math.min(quantum, burstTime[roundRobinIndex]);
    burstTime[roundRobinIndex] -= time;
    // Determine if this process has finished
    // It is finished when bursttime has reduced to zero
    // processComplete = processComplete + 1
    if (burstTime[roundRobinIndex] == 0)
    processesComplete++;

    System.out.println(" | " + process[roundRobinIndex] + " | " + burstTime[roundRobinIndex]
    + " | " + priority[roundRobinIndex] + " | " + time + " | " + waitingTime
    + " | ");
    // waitingTime += time;
    waitingTime += quantum; // I think this is correct (CPU will take full quantum each time

    }
    roundRobinIndex = (roundRobinIndex + 1) % noOfProcess;
    }

    }
    }

    对于提供的数据(处理 A-E),产生以下结果。

    | Process | CPU Burst | Priority | Time  |  Waiting Time | 
    | A | 8 | 3 | 2 | 0 |
    | B | 0 | 1 | 1 | 2 |
    | C | 0 | 3 | 2 | 4 |
    | D | 0 | 4 | 1 | 6 |
    | E | 3 | 2 | 2 | 8 |
    | A | 6 | 3 | 2 | 10 |
    | E | 1 | 2 | 2 | 12 |
    | A | 4 | 3 | 2 | 14 |
    | E | 0 | 2 | 1 | 16 |
    | A | 2 | 3 | 2 | 18 |
    | A | 0 | 3 | 2 | 20 |

    编辑:这是回合的修复:

    public class Round {

    int process = 0;
    // DON'T create the array yet!
    // You don't know how big to make it (yet).
    // Until you ask how many processes there are
    int[] processvalue;
    Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
    Round r = new Round();
    r.input();
    r.loop();
    r.print();
    }

    void input() {

    System.out.print("how many process");
    // n=Integer.parseInt(br.readLine());
    process = sc.nextInt();
    // HERE is where you now must create the array
    // Now that you know how large to make it
    processvalue = new int[process];
    }

    void loop() {

    for (int i = 0; i < process; i++) {
    System.out.println("please input the process " + (i + 1));
    processvalue[i] = sc.nextInt();
    }
    }

    void print() {

    for (int k = 0; k < process; k++) {
    System.out.println("value" + processvalue[k]);
    }

    }

    }

    编辑正确的等待时间

    public class RoundRobinScheduling {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int noOfProcess = 0;
    int quantum = 2;

    System.out.print("Number of Process: ");
    noOfProcess = input.nextInt();

    int[] burstTime = new int[noOfProcess];
    int[] priority = new int[noOfProcess];
    String[] process = new String[noOfProcess];
    int[] nextTime = new int[noOfProcess];
    int clockTime = 0;
    double totalWaitTime = 0;

    int processesComplete = 0;

    for (int i = 0; i < noOfProcess; i++) {
    System.out.print("Name Process: ");
    process[i] = input.next();

    System.out.print("Burst Time: ");
    burstTime[i] = input.nextInt();
    if (burstTime[i] == 0)
    processesComplete++;

    System.out.print("Priority: ");
    priority[i] = input.nextInt();
    nextTime[i] = 0;
    }
    input.close();

    int roundRobinIndex = 0;

    System.out.println(" | Process | CPU Burst | Priority | Time | Clock Time | Wait Time |");
    while (processesComplete < noOfProcess) {
    if (burstTime[roundRobinIndex] > 0) {
    int time = Math.min(quantum, burstTime[roundRobinIndex]);
    burstTime[roundRobinIndex] -= time;
    if (burstTime[roundRobinIndex] == 0)
    processesComplete++;
    int waitTime = clockTime - nextTime[roundRobinIndex];
    totalWaitTime += waitTime;

    System.out.println(" | " + process[roundRobinIndex] + " | " + burstTime[roundRobinIndex]
    + " | " + priority[roundRobinIndex] + " | " + time + " | " + clockTime
    + " | " + waitTime + " |");
    //clockTime += quantum;
    clockTime += time;
    nextTime[roundRobinIndex] = clockTime;
    }
    roundRobinIndex = (roundRobinIndex + 1) % noOfProcess;
    }
    System.out.println("Average wait time="+totalWaitTime/noOfProcess);

    }
    }

    创建输出

     | Process | CPU Burst | Priority | Time  |  Clock Time |  Wait Time |
    | A | 8 | 3 | 2 | 0 | 0 |
    | B | 0 | 1 | 1 | 2 | 2 |
    | C | 0 | 3 | 2 | 3 | 3 |
    | D | 0 | 4 | 1 | 5 | 5 |
    | E | 3 | 2 | 2 | 6 | 6 |
    | A | 6 | 3 | 2 | 8 | 6 |
    | E | 1 | 2 | 2 | 10 | 2 |
    | A | 4 | 3 | 2 | 12 | 2 |
    | E | 0 | 2 | 1 | 14 | 2 |
    | A | 2 | 3 | 2 | 15 | 1 |
    | A | 0 | 3 | 2 | 17 | 0 |
    Average wait time=5.8

    关于java - 使用 Quantum 和 Produce Table 进行循环调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901474/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com