Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone add comments and explain this java code for me ? import java.util.Scanner; interface Customer { public boolean isEmpty(); public boolean isFull(); public Object

can someone add comments and explain this java code for me ?

import java.util.Scanner; interface Customer { public boolean isEmpty(); public boolean isFull(); public Object front(); public Object back(); public Object pop(); public void push(Object item); public int size(); class que implements Customer { private static int DEFAULT_CAPACITY = 100;

private Object queue[ ];

private int rear, front;

public que () { this(DEFAULT_CAPACITY); }

public que (int capacity) { if (capacity < 2) throw new IllegalArgumentException ("Capacity must be > 1"); queue = new Object[capacity+1]; rear = front = 0; }

@Override public int size() { if( rear >= front ) return (rear - front); else return (rear - front + queue.length ); } @Override public boolean isEmpty() { return (rear == front); }

@Override public boolean isFull() { return (size() == queue.length-1); } @Override public void push(Object item) { if (isFull()) throw new IllegalStateException ("Queue is full"); queue[rear] = item; rear = (rear + 1)%queue.length; } @Override public Object pop( ) { if (isEmpty()) throw new IllegalStateException ("Queue is empty"); Object frontItem = queue[front]; queue[front] = null; front = (front + 1)%queue.length; return frontItem; } @Override public Object front() { if (isEmpty()) throw new IllegalStateException ("Queue is empty"); return queue[front]; }

@Override public Object back( ) { return (queue[rear-1]) ; } } public class bank { public static void main (String[] args) { Scanner input = new Scanner (System.in); System.out.println("Samba Bank System" + " This system will perform the below for the first 60 minutes: "+ "1.The arrival time for each customer " + "2.The leaving time for each customer " + "3.How many customers are waiting in the queue " + "4.Who is the customer in service now "); int size = 59; que c1 = new que(size); for (int i = 0; i < size; i++) { if (i % 4 == 0) { c1.push(i); System.out.println("In 8:"+i + " minute a customer arrives"); } } System.out.println(" There are " + c1.size() + " customers in the line. " + " ********************************************************"); int count =1; int cos=0; System.out.println("costumers in service Report"); for (int i = 1; i < size; i++) { if (i % 7 == 0) { c1.pop(); count ++; cos++; System.out.println("At 8:"+i+ " minute customer number "+cos+" leaves" + " and there are " + c1.size() + " in the queue. "); }

} System.out.println("********************************************************"); System.out.println("The current serving customer is cutomer number: "+ count); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic 4 Ole Database And Controls Superbible

Authors: Michael Hatmaker, C. Woody Butler, Ibrahim Malluf, Bill Potter

1st Edition

1571690077, 978-1571690074

More Books

Students also viewed these Databases questions