0

I am working on assigning random priorities (i.e. high, medium, low) to a list for a ServiceDesk assignment.

Before that, I was wondering how to go about storing (and printing) an array in said priority queue. This is currently what I have.

*UPDATED CODE

import java.util.*;
import java.util.Random;

public class Demo {

      public static void main (String[] args) {
          
      String [] names = {
         "Tanya Turner","Juana Downs","Mac Bautista","Leanne Donaldson",
         "Jere Romero","Autumn Hayden","Vincenzo Mckee","Penelope Stanley",
         "Rose Solis","Randal Savage","Delia Hardy","Alisha Hebert","Johnson Jefferson",
         "Gregorio Richard","Jana Simmons","Marian Shepherd","Lynn Pugh","Christine Newman",
         "Essie Frederick","Jewel Oneill","Raul Coleman","Lou Glover","Cora Rush",
         "Damien Norris","Omer Parsons","Adolph Petersen","Dane Proctor","Norbert Ritter",
         "Gloria Dickerson","Ella Morton","Madeline Mccullough","Patsy Berger","Tory Hardin",
         "Sonny Guzman","Kathrine Bond","Teodoro Bolton","Aimee Moran","Jerry Rhodes",
         "Palmer Golden","Zelma Hobbs","Marcella Patel","Freddy Lucas","Ladonna Hutchinson",
         "Devon Boone","Sue Graves","Chadwick Mcpherson","Antonia Rocha","Roseann Peters",
         "Leif Riggs","Judith Mcbride","Frances Simon","Nora Cervantes","Alba Hickman",
         "Concetta Wu","Chelsea Eaton","Dana Rocha","Hubert Kaiser","Phillip Stephenson",
         "Estela Kent","Rene Hughes","Clement Gilmore","Arlie Fernandez","Teodoro Buckley",
         "Daniel Chavez","Jeffry Shepherd","Devin Case","Eric Cooley","Dina Duncan","Teddy Price",
         "Matthew Cooke","Andres Dalton","Clayton Fields","Vito Lara","Lynette Mccann","Greta Choi",
         "Santo Noble","Thurman Douglas","Therese Norton","Juliette Graves","Fran Vang",
         "Forrest Gibbs","Cameron Bernard","Tracy Zhang","Hugh Huerta","Jaime Huynh","Tami Cordova",
         "Jami Mcpherson","Melissa Stein","Rayford Brewer","Tammie Lucero","Marcia Velez","Jasper Watkins",
         "Cora Chapman","Vickie Mccarthy","Gino Pena","Chadwick Hutchinson","Antonio Bryan",
         "Zachery Barnett","Randy Crawford","Laura Barton","Nolan Leach","Deborah Perry",
         "Georgina Sanford","Heidi Anthony","Leah Hester","Dong Swanson","Genevieve Wagner",
         "Russell Todd","Sherman Wolfe","Bo Schultz","Rosalyn Stevens","Brooke Moses","Jasmine Brock",
         "Guadalupe Andersen","Emilio Horne","Clara Spencer","Raul Levine","Colton Adams","Eve Avila",
         "Donny Murray","Laverne Valentine","Wilbert Gilbert","Justine Terrell","Waldo Nielsen",
         "Erma Mason","Brandie Sullivan","Murray Torres","Angelique Whitney","Shanna Humphrey",
         "Graig Farley","Lindsay Hines","Susanne Compton","Frankie Frank","Saundra Marks","Lorna Skinner",
         "Josephine Boyle","Maynard Wagner","Ronda Potts","Elias French","Gilberto Nguyen"
         };
      
         String[] priority = {"High", "Medium", "Low"};
         Random r = new Random();
         
         for (String i : names) {
            System.out.println("Names are: " + i);
            int randomPriority = r.nextInt(priority.length);
            System.out.println("Priority: " + priority[randomPriority]);
         }
            

       
               
   }
}
Adrian
  • 13
  • 3
  • Tell us what research you did, and why it didn't seem appropriate. – markspace Apr 18 '21 at 02:00
  • What search terms did you try? – John Kugelman Apr 18 '21 at 02:04
  • I searched various terms/phrases, i.e "assign random priorities in priority queue" or "random priorities in queue", thought I could find something through google or youtube. I think the closest thing I found was assign random integers in queues and I tried to make a variation of the code there but I kept getting errors. – Adrian Apr 18 '21 at 03:38

2 Answers2

1

Sounds like you are asking for help on how to get started. You are asking for help on learning to learn. Here is how I would approach your problem:

Apparently you are supposed to use a priority queue.

  1. Write a tiny program that makes a priority queue and stores strings into it, then prints them out.
  2. Define a class and store instances of that class into the priority queue instead of strings.
  3. Modify the sort criteria on the priority queue and notice that the printed sequence changes according to the sort criteria.
  4. Write a function that creates one class instance with random values.
  5. Write a function that creates all 100 class instances.
  6. Declare victory.
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
  • Priority Queues can store any [type of object](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/PriorityQueue.html). Unfortunately comments have very little formatting ability. Append updates to your question. – Mike Slinn Apr 18 '21 at 03:17
  • Good start! Now [read about the Java for-each loop](https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) and use one in the final paragraph of your code to populate the priority queue. – Mike Slinn Apr 18 '21 at 03:35
  • For-each loop worked great thanks for the assistance! – Adrian Apr 18 '21 at 04:27
  • Why don't you update your question with your latest code? Others might learn from your learning experience. – Mike Slinn Apr 18 '21 at 11:42
0

A priority queue doesn't store priorities itself. Instead, it provides a hook where it asks "what's the priority of this item?" and your code responds with the priority. Consequently, it's on you to augment the items you're storing with their priorities.

In other words, you'll need to use a richer data format than just plain strings. You should probably store the names and their priorities side by side. It calls for a custom class with fields such as author and priority.

enum Priority {
    LOW,
    MEDIUM,
    HIGH
}

class Ticket {
    String author;
    String description;
    Priority priority;
}

You can assign random priorities when you create each Ticket.

The glue that'll bind the priority queue and the Ticket class is a custom comparator. Writing one is pretty straightforward using -> lambda syntax:

PriorityQueue<Ticket> queue = new PriorityQueue<>(
    Comparator.comparing(ticket -> ticket.priority)
);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578