0

このプログラムは、コール センター、銀行、店舗、空港などの顧客サービス業務をシミュレートし、窓口係が顧客にサービスを提供します。顧客はランダムな時間に到着し、出納係が対応できるようになるまで列に並んで待ちます。待ち行列はキューデータ構造で実装されています。ただし、2 つの小さなエラーが発生します。1.) 私のエンキュー メソッドは引数に適用できず、2.) int から顧客にキャストできません。これがコードです。エラーは最後の太字の行にあります

import java.util.Random;
class MyQueue<E> { 
private int maxSize; 
private int[] queArray;
private int front;              
private int rear;
public MyQueue(int s) // constructor
{
maxSize = s+1; // array is 1 cell larger
queArray = new int[maxSize]; // than requested
front = 0;
rear = -1;
}

public void enqueue(int j) // put item at rear of queue
{
if(rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
}

public int dequeue() // take item from front of queue
{
int temp = queArray[front++];
if(front == maxSize)
front = 0;
return temp;
}

public int peek() // peek at front of queue
{
return queArray[front];
}

public boolean isEmpty() // true if queue is empty
{
return ( rear+1==front || (front+maxSize-1==rear) );
}

public boolean isFull() // true if queue is full
{
return ( rear+2==front || (front+maxSize-2==rear) );
}

public int size() // (assumes queue not empty)
{
if(rear >= front) // contiguous sequence
return rear-front+1;
else // broken sequence
return (maxSize-front) + (rear+1);
}
}
class Customer { int arrive; // Time point that the customer arrived. int processTime; // Time duration that the customer will need to be served.
/**
 * Default constructor
 */
public Customer() {
    arrive = 0;
    processTime = 0;
}

/**
 * Set the arrival time point of the customer.

 * 
 * @param Time
 *            point
 */
public Customer(int arrivalTime) {
    arrive = arrivalTime;

    // We set the processing time as a random integer between 1 and 3.
    processTime = (int) (Math.random() * 3 + 1);
}

/**
 * @return the arrival time point of the customer.
 */
public int getArrivalTime() {
    return arrive;
}

/**
 * @return the processing time of the customer.
 */
public int getProcTime() {
    return processTime;
}
}
public class Simulator { /** * The main method of the class. * * @param args * Command line arguments. */ public static void main(String[] args) {
if (args.length != 3) {
    System.out.println("usage: " + Simulator.class.getSimpleName()
            + " qCapacity simHours customPerHour");
    System.out.println("Example: " + Simulator.class.getSimpleName()
            + " 10 1 30");
    System.exit(-1);
}
// maximum size of queue
int qCapacity = Integer.parseInt(args[0]);

// number of simulation hours
int simHours = Integer.parseInt(args[1]);

// average number of customers per hour
int custPerHour = Integer.parseInt(args[2]);

// Run simulation
simulation(qCapacity, simHours, custPerHour);
}

private static void simulation(int qCapacity, int simHours, int custPerHour) {
    // Constant
    final int MIN_PER_HR = 60;

    // A queue that will hold and manage objects of type Customer.
    MyQueue<Customer> line = new MyQueue<Customer>(qCapacity);

    // For how many cycles should the simulation run. We assume that each
    // cycle takes one minute.
    int cycleLimit = MIN_PER_HR * simHours;

    // The average number of customers can arrive per minute
    float custPerMin = ((float) custPerHour) / MIN_PER_HR;

    // The number of customers that were turned away because the line
    // (queue)
    // was full at the time they arrived.
    int turnAways = 0;

    // Number of customers that arrived.
    int customers = 0;

    // Number of customers that were served.
    int served = 0;

    // Total number of customers that entered the line (queue).
    int sumLine = 0;

    // Waiting time until the next customer is served.
    int waitTime = 0;

    // Total time that all the customers waited in the line.
    int lineWait = 0;

    // Simulation
    for (int cycle = 0; cycle < cycleLimit; cycle++) {
        float j = custPerMin;
        while (j > 0) {
            if (newCustomer(j)) {
                if (line.isFull()) {
                    turnAways++;
                } else {
                    customers++;
                    Customer customer = new Customer(cycle);
                    **line.enqueue(customer);**
                }
            }
            j = j - 1;              
        }

        if (waitTime <= 0 && !line.isEmpty()) 

{
        **Customer customer = (Customer) line.dequeue();**
        waitTime = customer.getProcTime();
        lineWait += cycle - customer.getArrivalTime();
        served++;
    }

    if (waitTime > 0) {
        waitTime--;
    }

    sumLine += line.size();
}

// Print the simulation results.
if (customers > 0) {
    System.out.println("\nCustomers accepted: " + customers);
    System.out.println("  Customers served: " + served);
    System.out.println(" Customers waiting: " + line.size());
    System.out.println("         Turnaways: " + turnAways);
    System.out.println("Average queue size: " + (float) sumLine
            / cycleLimit);
    System.out.println(" Average wait time: " + (float) lineWait
            / served + " minutes");
} else {
    System.out.println("No customers!");
}
}

private static boolean newCustomer(float j) {
    if(j > 1)
    return true;
else
    return (j > Math.random() );
}
4

2 に答える 2

0

問題は次の 2 つの方法にあるようです。

public void enqueue(int j) // put item at rear of queue
{
if(rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
}

public int dequeue() // take item from front of queue
{
int temp = queArray[front++];
if(front == maxSize)
front = 0;
return temp;
}

キューに整数以外のものを保持するつもりだった場合は、それを反映するために引数の型/戻り値の型を変更する必要があります。

于 2013-10-16T01:58:34.160 に答える
0
**line.enqueue(customer);**
    // 1.) my enqueue method is not applicable for the argument 

enqueueメソッドは引数を取りますが、型をint渡そうとしています。Customer多分あなたはこのようなものが欲しいです: line.enqueue(customer.getSomething());. あなたのコードからは本当にわかりません。

**Customer customer = (Customer) line.dequeue();**
    // 2.)cannot cast from int to customers

(Customer) line.dequeue();. ここにキャストCustomerしていますint-メソッドの戻り値は基本的に a が and に等しいと言っています。これはline.dequeue() isintでない限り不可能ですが、そうではありませんdeququeintCustomerintCustomer

あなたはこれを求めている:

Customer customer = new Customer(line.dequeue) 
   // Customer constructor takes an int value
于 2013-10-16T02:23:23.303 に答える