[Solved] Crowd pattern with Java

Crowd pattern: Let’s develop a feature for the exhibition organizers. Given the number of persons entering the exhibition on an hourly basis from 9 AM, analyze the crowd pattern on different intervals. The exhibition starts from 9 AM ends at 9 PM. Let’s categorize the intervals as Morning(9 AM – 12 PM), Afternoon (12 PM – 3 PM), Evening (3 PM – 6 PM), and Night (6 PM – 9 PM). Determine the crowd pattern in those four intervals using threads. Invoke a thread for each interval which computes the crowd count in those intervals. Consolidate the data and print the details in the main method.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names, and method names should be the same as specified in the problem statement.


Create a class Crowd which extends the Thread class with the following private attributes.

Attribute Datatype
countInteger
hourlyCountInteger[]

Include appropriate getters and setters.
Generate default and parameterized constructors. The format for parameterized constructor is Crowd(Integer[ ] hourlyCount)

The Crowd class includes the following methods

Method Description
void run()This method computes the total count in the particular interval from the hourlyCount attribute.


Create a driver class called Main. In the main method, obtain the crowd count for each hour, and invoke a thread for each interval. Display the consolidated data.

Input and Output format:
The input consists of the integer which corresponds for crowd count for each hour.
The output consists of the consolidated data of the crowd for each interval.

Sample Input and Output:
[All text in bold corresponds to the input and rest corresponds to the output]

Enter the crowd pattern

50

77

98

11

39

56

45

85

112

198

320

10

The Crowd count in the morning is 225

The Crowd count in the afternoon is 106

The Crowd count in the evening is 242

The Crowd count in the night is 528

Solution

import java.util.*;
public class Main {

    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
		System.out.println("Enter the crowd pattern");
        ArrayList<Integer> al=new ArrayList<>();
        for(int i=0;i<12;i++){
            String t=sc.nextLine();
            al.add(Integer.parseInt(t));
        }
        int mor=0,after=0,eve=0,night=0;
        for(int j=0;j<al.size();j++){
            if(j<3){mor+=al.get(j);}
            if(j<6&&j>2){after+=al.get(j);}
            if(j<9&&j>5){eve+=al.get(j);}
            if(j<12&&j>8){night+=al.get(j);}
        }
        System.out.println("The crowd count in the morning is "+mor);
        System.out.println("The crowd count in the afternoon is "+after);
        System.out.println("The crowd count in the evening is "+eve);
        System.out.println("The crowd count in the night is "+night);
	}

}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *