do I edit the code to correctly display the number of available burgers and the waiting customers?   import java.util.Scanner; import java.util.Random; class Exam implements Runnable {     private String cName = new String("");     public Exam (String cName, int x) {         this.cName = cName;         this.x = x;     }     static int x;          @Override     public void run() {

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

How do I edit the code to correctly display the number of available burgers and the waiting customers?

 

import java.util.Scanner;
import java.util.Random;

class Exam implements Runnable {
    private String cName = new String("");
    public Exam (String cName, int x) {
        this.cName = cName;
        this.x = x;
    }
    static int x;
    
    @Override
    public void run() {
        Random r = new Random();
        int randNum, j=0, avail=0;
        for(int i = 1; i <= this.x; i++) {
            randNum = (int) Math.floor(Math.random() * (this.x - 1 + 1) + 1);
            if (cName == "Consumer #1" || cName == "Consumer #2"){
                avail = i - 1;
                    System.out.println(cName + " of Thread #" + i + " buys 1 burger   # of available burgers [" + avail + "] Customer(s) waiting [" + j++ + "]");    
            }else {
                int num = this.x * 2;
                avail = i - 1;
                for(i = 1; i <= num; i++){
                    System.out.println(cName + " is cooking burger #" + i + "\t\t\t # of available burgers [" + i + "] Customer(s) waiting [" + j + "]");    
                }    
            }    
        }
            
    }
    
}

public class MidtermExam{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        System.out.print("# of burgers to cook per consumer: ");
        int input = sc.nextInt();
        System.out.println();
        
        Exam r1 = new Exam("Producer",input);
        Thread Producer = new Thread(r1);
        Producer.start();
        Exam r2 = new Exam("Consumer #1",input);
        Thread Consumer1 = new Thread(r2);
        Consumer1.start();
        Exam r3 = new Exam("Consumer #2",input);
        Thread Consumer2 = new Thread(r3);
        Consumer2.start();
        
    }
}

Sample output: Assume the system generates 4
Command Prompt
# of burgers to cook per Consumer = 4
Producer is Cooking Burger #1
Producer is Cooking Burger #2
Consumer #1 of Thread# 1 buys 1 burger
Consumer #2 of Thread# 1 buys 1 burger
Producer is Cooking Burger #3
Consumer #1 of Thread# 2 buys 1 burger
Producer is Cooking Burger #4
Producer is Cooking Burger #5
Producer is Cooking Burger #6
Consumer #3 of Thread# 1 buys 1 burger
Producer is Cooking Burger #7
Consumer #2 of Thread# 2 buys 1 burger
Consumer #3 of Thread# 2 buys 1 burger
Producer is Cooking Burger #8
Consumer #4 of Thread# 1 buys 1 burger
Consumer #4 of Thread# 2 buys 1 burger
# of Available burgers [1] Customer(s) Waiting: [0]
# of Available burgers [2] Customer(s) Waiting: [0]
# of Available burgers [1] Customer(s) Waiting: [0]
# of Available burgers [0] Customer(s) Waiting: [0]
# of Available burgers [1] Customer(s) Waiting: [0]
# of Available burgers [0] Customer(s) Waiting: [0]
# of Available burgers [1] Customer(s) Waiting: [0]
# of Available burgers [2] Customer(s) Waiting: [0]
# of Available burgers [3] Customer(s) Waiting: [0]
# of Available burgers [2] Customer(s) Waiting: [0]
# of Available burgers [3] Customer(s) Waiting: [0]
# of Available burgers [2] Customer(s) Waiting: [0]
# of Available burgers [1] Customer(s) Waiting:
# of Available burgers [2] Customer(s) Waiting: [0]
# of Available burgers [1] Customer(s) Waiting: [0]
# of Available burgers [0] Customer(s) Waiting: [0]
Sample output: Assume the
system generates 2
%3D
Command Prompt
# of burgers to cook per Consumer = 2
Producer is Cooking Burger #1
Consumer #1 of Thread# 1 buys 1 burger
Consumer #1 of Thread# 2 buys 1 burger
Consumer #2 of Thread# 1 buys 1 burger
Producer is Cooking Burger #2
Consumer #2 of Thread# 2 buys 1 burger
Producer is Cooking Burger #3
Producer is Cooking Burger #4
# of Available burgers [1] Customer(s) Waiting: [0]
# of Available burgers [0] Customer(s) Waiting: [0]
# of Available burgers [0] Customer(s) Waiting: [1]
# of Available burgers [0] Customer(s) Waiting: [2]
# of Available burgers [0] Customer(s) Waiting: [1]
# of Available burgers [0] Customer(s) Waiting: [2]
# of Available burgers [0] Customer(s) Waiting: [1]
# of Available burgers [0] Customer(s) Waiting: [0]
Note:
|Customer(s) Waiting: [1]
Since There are no more Available burgers created by the Producer Thread, the Customer “waits" !
until the Producer makes a new Burger.
Producer is Cooking Burger #2 # of Available burgers [0] Customer(s) Waiting: [1]
Although the Producer creates a new Burger, It will not be counted on the “Available" because
there were 2 customer(s) waiting. That's why on this line, Waiting was instead decremented
from 2 (see previous line) to 1.
Waiting: [1]
The same concept happened here.
Transcribed Image Text:Sample output: Assume the system generates 4 Command Prompt # of burgers to cook per Consumer = 4 Producer is Cooking Burger #1 Producer is Cooking Burger #2 Consumer #1 of Thread# 1 buys 1 burger Consumer #2 of Thread# 1 buys 1 burger Producer is Cooking Burger #3 Consumer #1 of Thread# 2 buys 1 burger Producer is Cooking Burger #4 Producer is Cooking Burger #5 Producer is Cooking Burger #6 Consumer #3 of Thread# 1 buys 1 burger Producer is Cooking Burger #7 Consumer #2 of Thread# 2 buys 1 burger Consumer #3 of Thread# 2 buys 1 burger Producer is Cooking Burger #8 Consumer #4 of Thread# 1 buys 1 burger Consumer #4 of Thread# 2 buys 1 burger # of Available burgers [1] Customer(s) Waiting: [0] # of Available burgers [2] Customer(s) Waiting: [0] # of Available burgers [1] Customer(s) Waiting: [0] # of Available burgers [0] Customer(s) Waiting: [0] # of Available burgers [1] Customer(s) Waiting: [0] # of Available burgers [0] Customer(s) Waiting: [0] # of Available burgers [1] Customer(s) Waiting: [0] # of Available burgers [2] Customer(s) Waiting: [0] # of Available burgers [3] Customer(s) Waiting: [0] # of Available burgers [2] Customer(s) Waiting: [0] # of Available burgers [3] Customer(s) Waiting: [0] # of Available burgers [2] Customer(s) Waiting: [0] # of Available burgers [1] Customer(s) Waiting: # of Available burgers [2] Customer(s) Waiting: [0] # of Available burgers [1] Customer(s) Waiting: [0] # of Available burgers [0] Customer(s) Waiting: [0] Sample output: Assume the system generates 2 %3D Command Prompt # of burgers to cook per Consumer = 2 Producer is Cooking Burger #1 Consumer #1 of Thread# 1 buys 1 burger Consumer #1 of Thread# 2 buys 1 burger Consumer #2 of Thread# 1 buys 1 burger Producer is Cooking Burger #2 Consumer #2 of Thread# 2 buys 1 burger Producer is Cooking Burger #3 Producer is Cooking Burger #4 # of Available burgers [1] Customer(s) Waiting: [0] # of Available burgers [0] Customer(s) Waiting: [0] # of Available burgers [0] Customer(s) Waiting: [1] # of Available burgers [0] Customer(s) Waiting: [2] # of Available burgers [0] Customer(s) Waiting: [1] # of Available burgers [0] Customer(s) Waiting: [2] # of Available burgers [0] Customer(s) Waiting: [1] # of Available burgers [0] Customer(s) Waiting: [0] Note: |Customer(s) Waiting: [1] Since There are no more Available burgers created by the Producer Thread, the Customer “waits" ! until the Producer makes a new Burger. Producer is Cooking Burger #2 # of Available burgers [0] Customer(s) Waiting: [1] Although the Producer creates a new Burger, It will not be counted on the “Available" because there were 2 customer(s) waiting. That's why on this line, Waiting was instead decremented from 2 (see previous line) to 1. Waiting: [1] The same concept happened here.
General Output
--Configuration: <Default>
# of burgers to cook per consumer
Producer is cooking burger #1
Producer is cooking burger #2
Producer is cooking burger #3
Producer is cooking burger #4
Consumer #2 of Thread #1 buys 1 burger
Consumer
Consumer #1 of Thread #2 buys 1 burger
Consumer #2 of Thread #2 buys 1 burger
# of available burgers [1] Customer (s) waiting [0]
# of available burgers [2] Customer (s) waiting [0]
# of available burgers [3] Customer (s) waiting [0]
# of available burgers
# of available burgers [0] Customer (s) waiting [0]
#
4i Customer(s) waiting [0]
#1 of Thread #1 buys 1 burger
of available burgers [0] Customer (s) waiting [0]
# of available burgers [1] Customer (s) waiting [1]
# of available burgers [1] Customer (s)
waiting [1]
Process completed.
Transcribed Image Text:General Output --Configuration: <Default> # of burgers to cook per consumer Producer is cooking burger #1 Producer is cooking burger #2 Producer is cooking burger #3 Producer is cooking burger #4 Consumer #2 of Thread #1 buys 1 burger Consumer Consumer #1 of Thread #2 buys 1 burger Consumer #2 of Thread #2 buys 1 burger # of available burgers [1] Customer (s) waiting [0] # of available burgers [2] Customer (s) waiting [0] # of available burgers [3] Customer (s) waiting [0] # of available burgers # of available burgers [0] Customer (s) waiting [0] # 4i Customer(s) waiting [0] #1 of Thread #1 buys 1 burger of available burgers [0] Customer (s) waiting [0] # of available burgers [1] Customer (s) waiting [1] # of available burgers [1] Customer (s) waiting [1] Process completed.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY