使用量子和产品表进行循环调度

时间:2018-02-21 08:46:19

标签: java

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

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

这是我的作品,我有点卡在那里。 Idk如何为时间和额外做出生成。

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;
}
}

现在您必须在自动柜员机程序中使用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)密码必须是5位数(您使用的是3位) c)目前允许使用帐号100000(但不应该是6位数) d)否则本节很好!

  1. 提款:
  2. a)如果您创建attemptWithdraw(double amount)之类的新方法,它会对您有所帮助。你有很多重复的代码(比如50美元,100美元,当用户选择其他数量时)。无论用户是想要50,100还是200,都应该调用完全相同的方法(attemptWithdraw)。在这种方法中,你可以: a)确保用户有足够的钱(如果他们没有,则发出警告) b)确保自动柜员机有足够的钱(如果自动柜员机没有,则发出警告) c)借助amount借记/减少人员帐户(如果a和b是好的) d)提醒用户拿钱

    创建此额外方法后,将其调用为50,100和其他方法。您会发现它更容易,因为您只编写一次相同的代码。

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

      我会在那里停下来。你有很多很好的代码。只需再做一些更改,你就会在那里。

1 个答案:

答案 0 :(得分:1)

执行循环调度而没有任何初始优先级(即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        | 

编辑:这是Round的修复:

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