Java

TOPIC 20 – JAVA COLLECTIONS II - SETS

 

LESSON WORK

 

 

QUESTION 1

Use either HashSet or TreeSet to create your own Set data structure.  Try the different methods to make sure you know how it works. 

 

QUESTION 2 (SCENARIO)

 

You work for Donald Trump.  You are one of 152 employees.  Mr. Trump is so happy with all of the workers' production that he is giving away a car to a lucky worker.  He has decided to pick a number between 1 and 152 and get each employee to guess a number.  The employee with the correct number wins.

 

Mr. Trump has asked you to write a program that will simply ask each employee for a number.  It will output "YOU WIN" if the correct number is guessed or "YOU LOSE" if the wrong number is guessed.  However, the program also needs to make sure that a user doesn't guess a previously guessed number and should ouput "NUMBER NOT AVAILABLE, PLEASE GUESS ANOTHER".

 

Furthermore, the Vice President of the company secretly approached you to make sure your program can work for a different number of employees in case Mr. Trump goes on a firing spree before he gives the car away.  (This will also allow us to test for a small number.)

 

Note: You must use a set.

 

SAMPLE OUTPUT FOR TOTAL OF 5 EMPLOYEES

 

Employee 1, here are the numbers that you can choose from:

1 2 3 4 5

Please choose a number.

2

 

YOU LOSE

==========

Employee 2, here are the numbers that you can choose from:

1 3 4 5

Please choose a number.

5

 

YOU LOSE

==========

Employee 3, here are the numbers that you can choose from:

1 3 4

Please choose a number.

5

 

NUMBER NOT AVAILABLE

Please choose a number.

3

 

YOU LOSE

==========

Employee 4, here are the numbers that you can choose from:

1 4

Please choose a number.

4

 

YOU WIN