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
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
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert...
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert an array of N element (Please do not use the default function “reverse()). 1.2 - There is a famous programming (algorithm) concept that can vastly reduce the complexity of Recursive Fibonacci Algorithm. 2 .a) What is the name of this concept 2.b) Write the execution time and memory complexity of Fibonacci sequences, once this concept is applied 2.c) Explain the main idea behind this...
Write a java code snippet that allows a teacher to track her students’ grades. Use a...
Write a java code snippet that allows a teacher to track her students’ grades. Use a loop to prompt the user for each student’s name and the grade they received. Add these values to two separate parallel ArrayLists. Use a sentinel to stop. Output a table listing each of the student’s names in one column and their associated grades in the second column.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT