Question

In: Computer Science

Write a code snippet for the following:   You need to control the maximum number of people...

Write a code snippet for the following:

  You need to control the maximum number of people who can be in a

  restaurant at any given time. A group cannot enter the restaurant if they

  would make the number of people exceed 100 occupants. Use random numbers

  between 1 and 20 to simulate groups arriving to the restaurant. After each

  random number, display the size of the group trying to enter and the number

  of current occupants. As soon as the restaurant holds the maximum number, output

  that the restaurant is full and quit.

*********IN JAVA************

Solutions

Expert Solution

In the code, we have generated the random numbers between 1 and 20 with the help of Random class. Secondly, a while loop checks for the condition for threshold and continuously adds the incoming group to the total number of people inside the restaurant. The complete code is given below:

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

public class Restaurant {
    private static int peopleInRestaurant;
    private static int threshold = 100;
    private static int group;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("People in restaurant: 0");
        Random num = new Random();
        while(peopleInRestaurant <= threshold){
            group =  1+num.nextInt(20);
            System.out.println("Size of the group trying to enter: "+ group);
            if(peopleInRestaurant+group > 100){
                System.out.println("Restaurant is full");
                return;
            }
            peopleInRestaurant = peopleInRestaurant + group;
            System.out.println("Total number of occupants inside restaurant: "+peopleInRestaurant);
        }
        System.out.println("Restaurant is full");
    }
}

Related Solutions

Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
PYTHON You need to control the number of people who can be in an oyster bar...
PYTHON You need to control the number of people who can be in an oyster bar at the same time. Groups of people can always leave the bar, but a group cannot enter the bar if they would make the number of people in the bar exceed the maximum of 100 occupants. Write a program that reads the sizes of the groups that arrive or depart. Use negative numbers for departures. After each input, display the current number of occupants....
Write a code snippet to pre-sell a limited number of movie tickets. Each   buyer can buy...
Write a code snippet to pre-sell a limited number of movie tickets. Each   buyer can buy as many as 6 tickets. No more than 20 tickets can be sold.   Prompt the user for the desired number of tickets and then display the   number of remaining tickets. Repeat until all tickets have been sold,   and then display the total number of buyers. **********IN JAVA**********
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
What is the output of the following code snippet? (If there is some kind of syntax...
What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1.        int laps = 8;        if (laps++ > --laps)               laps += 2;        else if (--laps > (laps - 1))               laps += 4;        else if (++laps)               laps -= 3;        cout << laps << endl; 2. What is the output of the following code snippet?     int j = 47;     int i = 8;     j =...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I need to write a c/c++ code to find the maximum sum in those possible combinations...
I need to write a c/c++ code to find the maximum sum in those possible combinations of arrays. There are N arrays with S elements. ex2: 4 arrays with 3 elements.We want to find the greater number in the same column of two arrays(may possible be three or four...arrays), and sum those greater number to find the greatest sum in all of the combinations in those 4 arrays, like: A: [50, 60, 70] B: [80, 40, 20] C: [30, 100,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT