1. Create a folder named LastName_FirstName on your local drive. (ex. Reyes_Mark)
2. Using NetBeans, create a Java project named Greeting. Set the project location to your own folder.
3. Import Scanner and PriorityQueue from the java.util package.
4. Create one (1) PriorityQueue object named nicknames.
5. The output shall do the following:
5.1. Ask the user to input the nicknames of four (4) of his/her classmates.
5.2. Ask the user to press H to say “Hi” to each of them.
5.3. Display Hi and the classmate’s nickname (Ex. Hi Nika!) whenever the user presses H, and
Done saying hi when the queue gets empty.
import java.util.Scanner;
import java.util.PriorityQueue;
class Greeting {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PriorityQueue<String> nicknames = new PriorityQueue<>();
System.out.println("Enter four names of your classmates: ");
for (int i = 0; i < 4; i++) {
nicknames.add(scanner.nextLine());
}
System.out.println("Enter 'H' to say 'Hi' each of them!");
while (!nicknames.isEmpty()) {
scanner.nextLine();
System.out.printf("Hi %s!\n", nicknames.poll());
}
}
}
Comments
Leave a comment