Skip to content

Commit a5fd324

Browse files
Merge branch 'mr/322-statements-lab-remove-console-input' into 'master'
Resolve "Update Statements lab to remove console input" Closes #322 See merge request feng/training/material!402
2 parents a3df259 + 85c48e2 commit a5fd324

File tree

4 files changed

+73
-93
lines changed

4 files changed

+73
-93
lines changed

courses/ada_essentials/040_statements/88-statements.lab.rst

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,22 @@ Statements Lab
88

99
* Requirements
1010

11-
- Create a simple algorithm to count number of hours worked in a week
11+
- Create a simple program to build a time tracking sheet for the week
12+
13+
+ For every day of the week, print a line indicating the time of day (in hours)
1214

13-
+ Use `Ada.Text_IO.Get_Line` to ask user for hours worked on each day
14-
+ Any hours over 8 gets counted as 1.5 times number of hours (e.g. 10 hours worked will get counted as 11 hours towards total)
15-
+ Saturday hours get counted at 1.5 times number of hours
16-
+ Sunday hours get counted at 2 times number of hours
15+
- Conditions for printing time
1716

18-
- Print total number of hours "worked"
17+
+ Only print times for "normal" working hours
18+
+ No working on Sunday, so no times printed
19+
+ Saturday is a half-day, so only print every other time slot
1920

2021
* Hints
2122

22-
- Use `for` loop to iterate over days of week
23-
- Use `if` statement to determine overtime hours
24-
- Use `case` statement to determine weekend bonus
25-
26-
-----------------------------
27-
Statements Lab Extra Credit
28-
-----------------------------
29-
30-
* Use an inner loop when getting hours worked to check validity
31-
32-
- Less than 0 should exit outer loop
33-
- More than 24 should not be allowed
23+
- Use a :ada:`for` loop to iterate over days of week and hours
24+
- Use a :ada:`case` statement to determine how start and end time
25+
- Use an :ada:`if` statement to determine if you are printing a time
26+
- For simplicity, feel free to use a 24-hour clock
3427

3528
-------------------------
3629
Statements Lab Solution

courses/ada_essentials/040_statements/lab/statements/ada95/main.adb

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,34 @@ with Ada.Text_IO; use Ada.Text_IO;
22
procedure Main is
33
type Days_Of_Week_T is
44
(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
5-
type Hours_Worked is digits 6;
6-
7-
Total_Worked : Hours_Worked := 0.0;
8-
Hours_Today : Hours_Worked;
9-
Overtime : Hours_Worked;
10-
S : String(1..10);
11-
L : Integer;
5+
type Hours is mod 24;
6+
Start : Hours;
7+
Finish : Hours;
8+
Big_Block : Boolean;
129
begin
1310
Day_Loop :
1411
for Day in Days_Of_Week_T loop
12+
case Day is
13+
when Sunday =>
14+
Start := 1;
15+
Finish := 0;
16+
when Saturday =>
17+
Start := 9;
18+
Finish := 13;
19+
Big_Block := True;
20+
when Monday .. Friday =>
21+
Start := 9;
22+
Finish := 17;
23+
Big_Block := False;
24+
end case;
1525
Put_Line (Days_Of_Week_T'Image (Day));
16-
Input_Loop :
17-
loop
18-
Get_Line (S, L);
19-
Hours_Today := Hours_Worked'Value (S(1..L));
20-
exit Day_Loop when Hours_Today < 0.0;
21-
if Hours_Today > 24.0 then
22-
Put_Line ("I don't believe you");
26+
Put_Line ("======");
27+
for Hour in Start .. Finish loop
28+
if Big_Block and then (2 * (Hour / 2) = Hour) then
29+
New_Line;
2330
else
24-
exit Input_Loop;
31+
Put_Line (" " & Hours'Image (Hour) & "00");
2532
end if;
26-
end loop Input_Loop;
27-
if Hours_Today > 8.0 then
28-
Overtime := Hours_Today - 8.0;
29-
Hours_Today := Hours_Today + 1.5 * Overtime;
30-
end if;
31-
case Day is
32-
when Monday .. Friday => Total_Worked := Total_Worked + Hours_Today;
33-
when Saturday => Total_Worked := Total_Worked + Hours_Today * 1.5;
34-
when Sunday => Total_Worked := Total_Worked + Hours_Today * 2.0;
35-
end case;
33+
end loop;
3634
end loop Day_Loop;
37-
38-
Put_Line (Hours_Worked'Image (Total_Worked));
3935
end Main;

courses/ada_essentials/040_statements/lab/statements/answer/main.adb

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,34 @@ with Ada.Text_IO; use Ada.Text_IO;
22
procedure Main is
33
type Days_Of_Week_T is
44
(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
5-
type Hours_Worked is digits 6;
6-
7-
Total_Worked : Hours_Worked := 0.0;
8-
Hours_Today : Hours_Worked;
9-
Overtime : Hours_Worked;
5+
type Hours is mod 24;
6+
Start : Hours;
7+
Finish : Hours;
8+
Big_Block : Boolean;
109
begin
1110
Day_Loop :
1211
for Day in Days_Of_Week_T loop
12+
case Day is
13+
when Sunday =>
14+
Start := 1;
15+
Finish := 0;
16+
when Saturday =>
17+
Start := 9;
18+
Finish := 13;
19+
Big_Block := True;
20+
when Monday .. Friday =>
21+
Start := 9;
22+
Finish := 17;
23+
Big_Block := False;
24+
end case;
1325
Put_Line (Day'Image);
14-
Input_Loop :
15-
loop
16-
Hours_Today := Hours_Worked'Value (Get_Line);
17-
exit Day_Loop when Hours_Today < 0.0;
18-
if Hours_Today > 24.0 then
19-
Put_Line ("I don't believe you");
26+
Put_Line ("======");
27+
for Hour in Start .. Finish loop
28+
if Big_Block and then (2 * (Hour / 2) = Hour) then
29+
New_Line;
2030
else
21-
exit Input_Loop;
31+
Put_Line (" " & Hour'Image & "00");
2232
end if;
23-
end loop Input_Loop;
24-
if Hours_Today > 8.0 then
25-
Overtime := Hours_Today - 8.0;
26-
Hours_Today := Hours_Today + 0.5 * Overtime;
27-
end if;
28-
case Day is
29-
when Monday .. Friday => Total_Worked := Total_Worked + Hours_Today;
30-
when Saturday => Total_Worked := Total_Worked + Hours_Today * 1.5;
31-
when Sunday => Total_Worked := Total_Worked + Hours_Today * 2.0;
32-
end case;
33+
end loop;
3334
end loop Day_Loop;
34-
35-
Put_Line (Total_Worked'Image);
3635
end Main;

courses/ada_essentials/040_statements/lab/statements/prompt/main.adb

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,22 @@ with Ada.Text_IO; use Ada.Text_IO;
22
procedure Main is
33
type Days_Of_Week_T is
44
(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
5-
type Hours_Worked is digits 6;
6-
7-
Total_Worked : Hours_Worked := 0.0;
8-
Hours_Today : Hours_Worked;
9-
Overtime : Hours_Worked;
5+
type Hours is mod 24;
6+
Start : Hours;
7+
Finish : Hours;
108
begin
11-
-- For each day in the Days_Of_Week_T loop
12-
-- Print prompt indicating which day we're asking for
13-
-- Loop "forever"
14-
Hours_Today := Hours_Worked'Value (Get_Line);
15-
-- exit the main loop if hours worked < 0
16-
if Hours_Today > 24.0 then
17-
Put_Line ("I don't believe you");
18-
else
19-
-- exit the input loop if a reasonable number is reached
20-
null;
21-
end if;
22-
-- if hours worked > 8 then
23-
Overtime := Hours_Today - 8.0;
24-
Hours_Today := Hours_Today + 1.5 * Overtime;
25-
-- Calculate actual hours paid for based on day of the week
26-
-- Monday - Friday, just add hours worked
27-
-- Saturday - hours worked * 1.5
28-
-- Sunday - hours worked * 2
9+
-- Loop over Days_Of_Week_T
10+
11+
-- Use a "case" statement to determine when the workday
12+
-- starts and ends based on the day of the week
13+
Start := 0;
14+
Finish := 23;
15+
16+
-- Print a header indicating what day of the week it is
17+
Put_Line ("Day of Week"); -- (example)
18+
-- For Saturday, print a line for every other hour (e.g. 3, 5, 7)
19+
Put_Line (Hours'Image (Start)); -- (example)
20+
-- For other days, print a line for every hour
21+
Put_Line (Hours'Image (Finish)); -- (example)
2922

30-
Put_Line (Hours_Worked'Image (Total_Worked));
3123
end Main;

0 commit comments

Comments
 (0)