Java 队列模拟器中的错误
该程序模拟呼叫中心、银行、商店、机场等地的客户服务操作,由出纳员为客户提供服务。顾客随机到达,排队等候,直到有出纳员为他们服务。排队采用队列数据结构实现。但是,我有两个小错误1.)我的排队方法不适用于参数,2.)无法从int向客户强制转换。这是代码。错误在靠近末尾的粗体行中Java 队列模拟器中的错误,java,queue,Java,Queue,该程序模拟呼叫中心、银行、商店、机场等地的客户服务操作,由出纳员为客户提供服务。顾客随机到达,排队等候,直到有出纳员为他们服务。排队采用队列数据结构实现。但是,我有两个小错误1.)我的排队方法不适用于参数,2.)无法从int向客户强制转换。这是代码。错误在靠近末尾的粗体行中 import java.util.Random; class MyQueue<E> { private int maxSize; private int[] queArray; private int fro
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() );
}
import java.util.Random;
类MyQueue{
私有int-maxSize;
私有int[]数组;
私人内部阵线;
私家车;
公共MyQueue(int s)//构造函数
{
maxSize=s+1;//数组比数组大1个单元格
queArray=new int[maxSize];//大于请求的值
正面=0;
后部=-1;
}
public void enqueue(int j)//将项目放在队列的后面
{
如果(后==最大尺寸-1)
后部=-1;
数组[++后]=j;
}
public int dequeue()//从队列前面获取项目
{
int temp=queArray[front++];
如果(前==maxSize)
正面=0;
返回温度;
}
public int peek()//在队列前面查看
{
返回数组[前];
}
公共布尔值isEmpty()//如果队列为空,则为true
{
返回(后+1==前| |(前+maxSize-1==后));
}
public boolean isFull()//如果队列已满,则为true
{
返回(后+2==前| |(前+maxSize-2==后));
}
public int size()/(假定队列不为空)
{
如果(后>=前)//连续序列
返回后前+1;
else//断开的序列
返回(前最大尺寸)+(后+1);
}
}
类Customer{int Arrival;//客户到达的时间点。int processTime;//客户需要服务的持续时间。
/**
*默认构造函数
*/
公众客户(){
到达=0;
processTime=0;
}
/**
*设置客户的到达时间点。
*
*@param时间
*点
*/
公共客户(国际到达时间){
到达时间;
//我们将处理时间设置为1到3之间的随机整数。
processTime=(int)(Math.random()*3+1);
}
/**
*@返回客户的到达时间点。
*/
public int getArrivalTime(){
返回到;
}
/**
*@返回客户的处理时间。
*/
public int getProcTime(){
返回处理时间;
}
}
公共类模拟器{/***类的主方法。**@param args*命令行参数。*/public static void main(字符串[]args){
如果(参数长度!=3){
System.out.println(“用法:”+Simulator.class.getSimpleName()
+“qCapacity simHours customPerHour”);
System.out.println(“示例:“+Simulator.class.getSimpleName()
+ " 10 1 30");
系统退出(-1);
}
//队列的最大大小
int qCapacity=Integer.parseInt(args[0]);
//模拟小时数
int-simHours=Integer.parseInt(args[1]);
//每小时平均客户数
int custPerHour=Integer.parseInt(args[2]);
//运行模拟
模拟(qCapacity、simHours、custPerHour);
}
专用静态无效模拟(int qCapacity、int simHours、int custPerHour){
//不变的
最终整数最小值每小时=60;
//将保存和管理Customer类型的对象的队列。
MyQueue line=新的MyQueue(qCapacity);
//模拟应该运行多少个周期。我们假设每个周期
//循环需要一分钟。
int cycleLimit=每小时分钟*分钟小时;
//平均每分钟可以到达的客户数量
浮动客户人数=((浮动)客户/小时)/分钟/小时;
//由于线路中断而被拒绝的客户数
//(排队)
//他们到达的时候已经客满了。
int turnAways=0;
//到达的客户数。
int客户=0;
//已服务的客户数。
int=0;
//进入该行(队列)的客户总数。
int sumLine=0;
//等待下一位客户的时间。
int waitTime=0;
//所有客户排队等候的总时间。
int lineWait=0;
//模拟
对于(int cycle=0;cycle0){
if(新客户(j)){
if(line.isFull()){
turnAways++;
}否则{
客户++;
客户=新客户(周期);
**排队(客户)**
}
}
j=j-1;
}
如果(等待时间0){
等待时间--;
}
sumLine+=line.size();
}
//打印模拟结果。
如果(客户>0){
System.out.println(“\n接受的客户:“+客户”);
System.out.println(“已服务客户:+已服务);
System.out.println(“等待的客户:+line.size());
System.out.println(“Turnaways:+Turnaways”);
System.out.println(“平均队列大小:”+(浮点)sumLine
/环糊精);
System.out.println(“平均等待时间:”+(浮点)lineWait
/已送达+“分钟”);
}否则{
System.out.println(“没有客户!”);
}
}
私有静态布尔newCustomer(float j){
如果(j>1)
返回true;
其他的
返回(j>Math.random());
}
看起来您的问题在于这两种方法:
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;
}
如果您打算在队列中保存除整数以外的任何内容,则需要更改参数类型/返回类型以反映这一点
**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
(客户)line.dequeue();
。您正在将Customer
转换为int
-line.dequeue()
您的deqque
方法返回amint
,因此基本上您是说客户等于和int
,这在
Customer customer = new Customer(line.dequeue)
// Customer constructor takes an int value