Lists

icon picker
security lights

smart devices can store lists to help control home appliances

security lights: update values in a list of numbers

some smart-home systems can turn on the lights at a pre-planned time as a security measure in order to give the impression of someone at home when a house is empty

requirement

Allow a user to enter and store times for a lights-on security feature. Then display the values for checking. Finally allow the user to change the hour for switching on. There should be a time for each day of the week.
sample output
Lights ON settings
enter a lights-on time for day 1: 7
..
enter a lights-on time for day 7: 9
day 1 current time 7
..
day 7 current time 9
update the time for day 1: 8
..
update the time for day 7: 9

design

store
a list of times (hour) for switching lights on each day of the week, input by the user
input
the initial list of times (hour) for switching lights on each day of the week
the upated list of times (hour) for switching lights on each day of the week
output
the stored list of times for switching lights on for each day of the week, initial time and updated time
process
no calculations required
algorithm
1. declare an array for 7 integers (default values)
2. loop for 7 days with index = 0 to 6
3. ask user to enter the time (hour) for lights switched on, store the value
4. end loop
5. loop for 7 days with index = 0 to 6
6. display the stored time for each day
7. end loop
8. loop for 7 days with index = 0 to 6
9. ask user to update the time (hour) for lights switched on, store the value
10. end loop

code features

set up a list using an array

int[] lightsOn = new int[7];
Declare an integer array called lightsOn and initialise it to store seven number values
Notice the [ ] brackets after the term int, this show that it is an array and not an ordinary single value string.
Notice the code after the = , instead of listing the values the code sets up an integer array with 7 default values (0).
This is the syntax for declaring and initialising an array without listing values.

enter and update a value in an array

lightsOn[i] = Integer.parseInt(keyboard.nextLine());
lightsOn[i] identifies the array and the position of the element to be used,
Integer.parseInt(keyboard.nextLine()) is a value that is entered at the keyboard, it is typed and read as a string and then converted to integer before storing

display a value from an array

System.out.println("day " + (i+1) + " time " + lightsOn[i]);
lightsOn[i] identifies the array and the position of the element to be used, i is the position of the next element, starting at 0 and finishing at 6
(i+1) will display day 1 when i is 0, the first element of the array, this makes the numbering display more meaningful to the user

program code

Loading…

PRIMM

predict

what will be displayed when the variable **i** is **2** in each of the loops

investigate

what happens if you do not enter a value?

modify

mod1
Add code to display the updated values after the user has had the chance to enter new times, the output should look like this:
Lights ON settings
enter a lights-on time for day 1: 7
..
enter a lights-on time for day 7: 9
day 1 current time 7
..
day 7 current time 9
update the time for day 1: 8
..
update the time for day 7: 9
day 1 updated time 8
..
day 7 updated time 9

mod2
Can you edit your code to use a single loop instead of the three at present for the display, update and final display? The output should then look like this:
Lights ON settings
enter a lights-on time for day 1: 7
..
enter a lights-on time for day 7: 9
day 1 current time 7
update a time for day 1: 8
day 1 new time 8
day 2 current time 7
update a time for day 2: 7
day 2 new time 7

make

a digital assistant can help keep track of meetings planned each day

requirement

Allow a user to enter up to three meetings at any time of day from 8 hours until 17 hours. The user will enter the name of the meeting and the hour at which it starts. The program should be able to display the meetings set up as shown below.
sample output
Enter meeting 1: interview
Enter time 1: 10
Enter meeting 2: team planning
Enter time 2: 13
Enter meeting 3: training session
Enter time 3: 16
8
9
10 : interview
11
12
13 : team planning
14
15
16 : training session
17

design

store
a list of 8 time slots, storing 3 meetings input by the user
input
the names and times of three meetings
output
the timetable of meetings from 8 until 17 showing the three meetings at the appropriate time
process
no calculations required
algorithm
1. declare an array for 10 strings (default values)
2. loop for 3 meetings with index = 0 to 2
3. ask user to enter the name of the meeting
4. ask the user to enter the time of the meeting
5. store the meeting name in the position (time - 8)
6. end loop
7. loop for 8 hours with index = 0 to 9
8. display the value of (index+8)
9. display the value stored in array at position(index)
10. end loop
although there are only 3 meetings, they could be at any time 8-17
remember array positions start at 0 and so a calculation is needed to adjust between the hour and the index / position
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.