Question 1:
- Algorithm 1 (Difference in times program)
1. Create a Scanner for user input.
2. Declares four integer variables: “hour1”, “minute1”, “ hour2”, and “minute2.”
3. Declare an integer variable called “difference”.
4. Display a welcome message to the user.
5. Prompt the user to enter the first time:
a. Asks for the hours (0-23) and stores it in “hour1”.
b. Asks for the minutes (0-59) and stores it in “minute1”.
6. Prompt the user to enter the second time:
a. Asks for the hours (0-23) and stores it in “hour2”.
b. Asks for the minutes (0-59) and stores it in “minute2”.
7. Convert each time into total minutes:
a. totMinutes1 = hour1*60 + minute1
b. totMinutes2 = hour2*60 + minute2
8. Display both entered times.
9. Calculate the difference in minutes.
a. Difference = | formula2 – formula1 |
10. Display the difference in minutes.
11. Display a goodbye message
12. Close the Scanner
Question 2:
- Algorithm 2 (Screen Name and Secret Code Generator
1. Create a Scanner for user input and a Random generator for random numbers.
2. Declare three string variables: “color”, “animal”, and “birth”
3. Display a welcome message.
4. Prompt the user to enter their favorite color and store it in color variable.
5. Prompt the user to enter their favorite animal and store it in animal variable.
6. Prompt the user to enter the city where they were born and store it in birth variable.
7. Create the screen name by:
a. Taking the last three letters of the color.
b. Adding the first three letters of the animal.
c. Adding the length of the string birth.
Store the result into a variable called “screen_name”
8. Concatenate the three inputs put in by the user, as in color, animal and birth. Store it
in a variable called “conc”
9. Swap the first and the last characters of “conc” while keeping the middle characters
unchanged. Store this in a variable called “swapped”
10. Convert the first character of “swapped” to uppercase.
11. Take the first 5 letters of swapped, and store it in a variable called “five”.
12. Replace the third letter “five” with “ * “. Store it in “five”. Update the value of “five”
13. Generate a random digit between 0 and 9 using the Random function
14. Form the secret code by concatenating “five”, and the remainder of “swapped” after
the first 5 characters, and the random digit.
15. Display the generated screen name and the secret code.
16. Display a goodbye message.
17. Close the Scanner.