From 737f692900f2b81d08d6ef512fd3aa75c89377c9 Mon Sep 17 00:00:00 2001 From: jal1077 Date: Wed, 24 Apr 2024 00:44:21 -0400 Subject: [PATCH 1/3] Added an example of queues being used in a game and a quick explanation of queues --- java/queue/README.md | 20 ++++++++++++++++++ java/queue/guess.java | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 java/queue/guess.java diff --git a/java/queue/README.md b/java/queue/README.md index e69de29bb..60ff3c488 100644 --- a/java/queue/README.md +++ b/java/queue/README.md @@ -0,0 +1,20 @@ +# Queues + +## What are queues? +Queues are just like the line you have in a supermarket. You have a line that is First In and First Out. This is called FIFO structure. But what is the hype surrounding queues? + +## What can you use queues for? +Queue data structures are commonly used in various scenarios where items or tasks need to be processed in a first-in, first-out order. Remember these are a few of the many ways that queues can be used! + +1. Job Scheduling: In computer systems and networks, queues are used to handle job scheduling. Jobs are added to a queue and processed in the order they were received. +2. Call Center Phone Systems: In call center phone systems, incoming calls are added to a queue, and customers are served in the order their calls were received. +3. Online Multiplayer Games: In online multiplayer games, queues are often used to manage the order in which players join game sessions or matchmaking queues. + +## What can you do with Queue Data Structure? +- Dequeue: deletes and returns the element that is at the front of the queue +- Enqueue: inserts the element at the back of the queue +- Peek: peeks at the front of the queue and returns the first element +- isEmpty: is the queue empty? It checks it +- isFull: is the queue full? It checks it + + diff --git a/java/queue/guess.java b/java/queue/guess.java new file mode 100644 index 000000000..127533255 --- /dev/null +++ b/java/queue/guess.java @@ -0,0 +1,48 @@ +package java.queue; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; +import java.util.Random; + +public class guess { + public static void main(String[] args) { + guess(); + } + + public static void guess() { + Random random = new Random(); + int secretNumber = random.nextInt(100) + 1; + Queue previousGuesses = new LinkedList<>(); + + System.out.println("We are going to play a quick guessing gam \n"); + System.out.println("Guess a number between 1 and 100 \n"); + + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("Guess the number"); + String input = scanner.nextLine(); + + if (!input.matches("\\d+")) { + System.out.println("Please enter a valid number."); + continue; + } + + int guess = Integer.parseInt(input); + + if (guess < secretNumber) { + System.out.println("Too low! Try again"); + previousGuesses.add(guess); + } else if (guess > secretNumber) { + System.out.println("Too high! Try again"); + previousGuesses.add(guess); + } else { + System.out.println("You got it! It was " + secretNumber); + break; + } + System.out.println("Previous guess" + previousGuesses); + } + scanner.close(); + } +} \ No newline at end of file From 3a0e979c5c2283437881bb3bc1107200efe96d2a Mon Sep 17 00:00:00 2001 From: jal1077 Date: Wed, 24 Apr 2024 00:48:35 -0400 Subject: [PATCH 2/3] Added an example of queues being used in a game and a quick explanation of queues --- java/queue/guess.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/java/queue/guess.java b/java/queue/guess.java index 127533255..8d15adadb 100644 --- a/java/queue/guess.java +++ b/java/queue/guess.java @@ -5,6 +5,8 @@ import java.util.Scanner; import java.util.Random; +// This is a small guessing game that uses linkedlists and queues + public class guess { public static void main(String[] args) { guess(); @@ -18,8 +20,11 @@ public static void guess() { System.out.println("We are going to play a quick guessing gam \n"); System.out.println("Guess a number between 1 and 100 \n"); + // Scanner allows you to add inputs and for the system to recognize it Scanner scanner = new Scanner(System.in); + // uses a while loop to allow you to continue to guess the number until you get + // it while (true) { System.out.println("Guess the number"); String input = scanner.nextLine(); @@ -33,6 +38,7 @@ public static void guess() { if (guess < secretNumber) { System.out.println("Too low! Try again"); + // This adds the guess to the line previousGuesses.add(guess); } else if (guess > secretNumber) { System.out.println("Too high! Try again"); From 0e244db724b9c0f9cc100999647ff7438c7057d1 Mon Sep 17 00:00:00 2001 From: jal1077 Date: Wed, 24 Apr 2024 01:02:02 -0400 Subject: [PATCH 3/3] Added the methods of queue --- java/queue/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/java/queue/README.md b/java/queue/README.md index 60ff3c488..f6def1fdd 100644 --- a/java/queue/README.md +++ b/java/queue/README.md @@ -17,4 +17,12 @@ Queue data structures are commonly used in various scenarios where items or task - isEmpty: is the queue empty? It checks it - isFull: is the queue full? It checks it +## Methods that can be used for Queues +1. add() - adds an element to the queue +2. remove() - removes an element into the queue +3. offer() - inserts an element into the queue and if successful will return true +4. peek() - returns the head of the queue; if empty will return null +5. poll() - removes and returns the head of the queue; if empty will return null +6. element() - returns the head of the queue; if empty will throw an exception +7. peek() - returns the head of the queue; if empty will return null