-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam-qa-lab.html
908 lines (791 loc) · 62.8 KB
/
exam-qa-lab.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<title>Module 13, Part 2: Exam Q&A, Review, and Final Lab — 02312 Introductory Programming — Fall 2024</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "light";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "light";
</script>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/styles/bootstrap.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/vendor/fontawesome/6.1.2/css/all.min.css?digest=e353d410970836974a52" rel="stylesheet" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" href="_static/styles/sphinx-book-theme.css?digest=14f4ca6b54d191a8c7657f6c759bf11a5fb86285" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/exercise.css" />
<link rel="stylesheet" type="text/css" href="_static/proof.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/tabs.css" />
<link rel="stylesheet" type="text/css" href="_static/custom.css" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=e353d410970836974a52" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=e353d410970836974a52" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/sphinx_highlight.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script src="_static/scripts/sphinx-book-theme.js?digest=5a5c038af52cf7bc1a1ec88eea08e6366ee68824"></script>
<script src="_static/proof/proof.js"></script>
<script>window.MathJax = {"loader": {"load": ["[tex]/bussproofs"]}, "tex": {"packages": {"[+]": ["bussproofs"]}, "macros": {}}, "options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="_static/mathjax/es5/tex-svg.js"></script>
<link rel="icon" href="_static/favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="ChangeLog" href="changelog.html" />
<link rel="prev" title="Module 13, Part 1: Lab Day — Exam Simulation" href="lab-day-6.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="light">
<a class="skip-link" href="#main-content">Skip to main content</a>
<input type="checkbox"
class="sidebar-toggle"
name="__primary"
id="__primary"/>
<label class="overlay overlay-primary" for="__primary"></label>
<input type="checkbox"
class="sidebar-toggle"
name="__secondary"
id="__secondary"/>
<label class="overlay overlay-secondary" for="__secondary"></label>
<div class="search-button__wrapper">
<div class="search-button__overlay"></div>
<div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
id="search-input"
placeholder="Search..."
aria-label="Search..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
</div>
<nav class="bd-header navbar navbar-expand-lg bd-navbar">
</nav>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<div class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<a class="navbar-brand logo" href="index.html">
<img src="_static/dtu.png" class="logo__image only-light" alt="Logo image"/>
<script>document.write(`<img src="_static/dtu.png" class="logo__image only-dark" alt="Logo image"/>`);</script>
</a></div>
<div class="sidebar-primary-item"><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
<div class="bd-toc-item navbar-nav active">
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="overview.html">Module 0: Overview of the Course and Assessment</a></li>
<li class="toctree-l1"><a class="reference internal" href="basic-programming.html">Module 1: Basic Notions of Computing and Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="java-first-taste.html">Module 2, Part 1: a First Taste of Java</a></li>
<li class="toctree-l1"><a class="reference internal" href="io-conditionals-strings.html">Module 2, Part 2: Console I/O, Conditionals, Strings</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-1.html">Module 3, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="loops.html">Module 3, Part 2: Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="loops2.html">Module 4, Part 1: More About Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="structured.html">Module 4, Part 2: Structured Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="arrays.html">Module 5, Part 1: Arrays</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-2.html">Module 5, Part 2: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="simple-classes.html">Module 6, Part 1: Simple Classes and Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="arrays-simple-classes-2.html">Module 6, Part 2: More About Arrays and Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-3.html">Module 7, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="references-null.html">Module 7, Part 2: References, <code class="docutils literal notranslate"><span class="pre">null</span></code> values, and the <code class="docutils literal notranslate"><span class="pre">NullPointerException</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="prog-interf-encapsulation.html">Module 8, Part 1: Programming Interfaces and Encapsulation</a></li>
<li class="toctree-l1"><a class="reference internal" href="interfaces.html">Module 8, Part 2: Java <code class="docutils literal notranslate"><span class="pre">interface</span></code>s</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-4.html">Module 9, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="inheritance-oo.html">Module 9, Part 2: Class Inheritance and Principles of Object-Oriented Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="inheritance-2.html">Module 10, part 1: More on Class Inheritance and <code class="docutils literal notranslate"><span class="pre">abstract</span> <span class="pre">class</span></code>es</a></li>
<li class="toctree-l1"><a class="reference internal" href="classes-polymorphism.html">Module 10, Part 2: More on Java Classes and Polymorphism</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-5.html">Module 11, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="errors.html">Module 11, Part 2: Error Handling with Exceptions</a></li>
<li class="toctree-l1"><a class="reference internal" href="files.html">Module 12, Part 1: File I/O</a></li>
<li class="toctree-l1"><a class="reference internal" href="projects.html">Module 12, Part 2: Managing Java Projects: Packages, JAR Files, Build Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-6.html">Module 13, Part 1: Lab Day — Exam Simulation</a></li>
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Module 13, Part 2: Exam Q&A, Review, and Final Lab</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">ChangeLog</a></li>
<li class="toctree-l1"><a class="reference internal" href="jgrader.html">Appendix: Information About JGrader</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item"><nav class="sidebar-indices-items">
<p class="sidebar-indices-items__title" role="heading" aria-level="1">Indices</p>
<ul class="indices-link">
<li class="toctree-l1">
<a class="reference internal"
href="genindex.html"
accesskey="I">General Index</a>
</li>
</ul>
</nav></div>
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main">
<div class="sbt-scroll-pixel-helper"></div>
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item"><label class="sidebar-toggle primary-toggle btn btn-sm" for="__primary" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</label></div>
</div>
<div class="header-article-items__end">
<div class="header-article-item">
<div class="article-header-buttons">
<button onclick="toggleFullScreen()"
class="btn btn-sm btn-fullscreen-button"
title="Fullscreen mode"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<script>
document.write(`
<button class="btn btn-sm navbar-btn search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
`);
</script>
<label class="sidebar-toggle secondary-toggle btn btn-sm" for="__secondary"title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</label>
</div></div>
</div>
</div>
</div>
<div id="jb-print-docs-body" class="onlyprint">
<h1>Module 13, Part 2: Exam Q&A, Review, and Final Lab</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#game-of-life">01 - Game of Life</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#dtu-christmas-party-management-with-file-i-o">02 - DTU Christmas Party Management with File I/O</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#step-1-loading-from-csv-file">Step 1 - Loading from CSV File</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#step-2-saving-to-csv-file">Step 2 - Saving to CSV File</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#arithmetic-expressions-version-4">03 - Arithmetic Expressions, Version 4</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#video-game-monsters-part-6">04 - Video Game Monsters, Part 6</a></li>
</ul>
</nav>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article" role="main">
<section class="tex2jax_ignore mathjax_ignore" id="module-13-part-2-exam-q-a-review-and-final-lab">
<span id="mod-exam-qa-lab-day-7"></span><h1>Module 13, Part 2: Exam Q&A, Review, and Final Lab<a class="headerlink" href="#module-13-part-2-exam-q-a-review-and-final-lab" title="Permalink to this heading">#</a></h1>
<p>This module does not introduce new contents: instead, the lecture will provide a
<strong>brief overview of the upcoming final exam</strong>, with time for questions and
discussion. Then, there will be the final lab, and you can work on the following
assessments.</p>
<ul class="simple">
<li><p><a class="reference internal" href="#lab-day-7-game-of-life"><span class="std std-ref">01 - Game of Life</span></a></p></li>
<li><p><a class="reference internal" href="#lab-day-7-party"><span class="std std-ref">02 - DTU Christmas Party Management with File I/O</span></a></p></li>
<li><p><a class="reference internal" href="#lab-day-7-expressions4"><span class="std std-ref">03 - Arithmetic Expressions, Version 4</span></a></p></li>
<li><p><a class="reference internal" href="#lab-day-7-monsters-6"><span class="std std-ref">04 - Video Game Monsters, Part 6</span></a></p></li>
</ul>
<div class="admonition important">
<p class="admonition-title">Important</p>
<ul class="simple">
<li><p>For each assessment, you can download the corresponding handout and submit
your solution on <strong>DTU Autolab: <a title="Autolab assessments" href="https://autolab.compute.dtu.dk/courses/02312-E24">https://autolab.compute.dtu.dk/courses/02312-E24</a></strong>.</p></li>
<li><p>For details on how to use Autolab and the assessment handouts, and how to
submit your solutions, please read <a class="reference internal" href="overview.html#sec-autolab"><span class="std std-ref">these instructions</span></a>.</p></li>
<li><p>If you have troubles, you can
<a class="reference internal" href="overview.html#sec-help"><span class="std std-ref">get help from the teacher and TAs</span></a>.</p></li>
</ul>
</div>
<section id="game-of-life">
<span id="lab-day-7-game-of-life"></span><h2>01 - Game of Life<a class="headerlink" href="#game-of-life" title="Permalink to this heading">#</a></h2>
<p>The goal of this assessment is to implement a version of
<a class="reference external" href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">John Horton Conway’s Game of Life</a>.
<em><strong>BONUS:</strong> if you solve this assessment, you can experiment with the
<a class="reference internal" href="#tip-life-animation"><span class="std std-ref">Game of Life animations described below</span></a>!</em></p>
<p>The game takes place in a <em>finite</em> grid of cells arranged in rows and columns.
Each cell can be either alive or dead. The game simulates the evolution of the
grid in steps: at every step, the current grid of cells is analysed to compute
the next state of the grid. More specifically, the next state of each cell
(dead or alive) depends on its current state and the state of its 8 neighbouring
cells (horizontally, vertically, and diagonally), according to the following <strong>4
rules</strong>:</p>
<ol class="arabic simple">
<li><p>if a cell is currently alive and has less than two live neighbours, then the
cell dies <em>(“underpopulation”)</em>;</p></li>
<li><p>if a cell is currently alive and has two or three live neighbours, then the
cell remains alive;</p></li>
<li><p>if a cell is currently alive and has more than three live neighbours, then
the cell dies <em>(“overpopulation”)</em>;</p></li>
<li><p>if a cell is currently dead and has exactly three live neighbours, then the
cell becomes alive <em>(“reproduction”)</em>.</p></li>
</ol>
<p>For example, consider the following patterns of cells (where live cells are
black, and dead cells are white).</p>
<img alt="Game of life cell pattern - Tub" class="align-center" src="_images/game-of-life-tub.png" />
<p>The cell pattern above (called “Tub”) remains constant at each step of the game,
because:</p>
<ul class="simple">
<li><p>each live cell has exactly two live neighbours (so it remains alive), and</p></li>
<li><p>each dead cell has less than three live neighbours (so it remains dead).</p></li>
</ul>
<img alt="Game of life cell pattern - Blinker" class="align-center" src="_images/game-of-life-blinker.gif" />
<p>The cell pattern above (called “Blinker”) evolves by alternating between the
horizontal 3-cell pattern at one step, and the vertical 3-cell pattern at the
next step. This happens because, at each step:</p>
<ul class="simple">
<li><p>the live cell in the centre stays alive, since it has two live neighbours;</p></li>
<li><p>the two live cells at the extremities of the pattern die, since each one of
them only has one live neighbour;</p></li>
<li><p>the two dead cells closest to the centre of the pattern become alive, since
they have three live neighbours;</p></li>
<li><p>all the other dead cells remain dead, because they have less than three live
neighbours.</p></li>
</ul>
<p><em>(You can find more interesting patterns and references in the
<a class="reference external" href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway’s Game of Life page on Wikipedia</a>.)</em></p>
<p>Your task is to edit the file <code class="docutils literal notranslate"><span class="pre">Life.java</span></code> provided in the handout, and implement
a class called <code class="docutils literal notranslate"><span class="pre">Life</span></code> with the following requirements. <em>(Besides, feel free to
define additional <code class="docutils literal notranslate"><span class="pre">private</span></code> fields and method of the class <code class="docutils literal notranslate"><span class="pre">Life</span></code> however you
like.)</em></p>
<ul>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Life</span></code> must provide the constructor:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="nf">Life</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">rows</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">columns</span><span class="p">)</span>
</pre></div>
</div>
<p>which creates a new game of life with a grid of cells having the given number
of <code class="docutils literal notranslate"><span class="pre">rows</span></code> and <code class="docutils literal notranslate"><span class="pre">columns</span></code>. Each cell in the new grid must be dead.</p>
</li>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Life</span></code> must provide the method:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">setCell</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">row</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">col</span><span class="p">,</span><span class="w"> </span><span class="kt">boolean</span><span class="w"> </span><span class="n">alive</span><span class="p">)</span>
</pre></div>
</div>
<p>which sets the cell at the given <code class="docutils literal notranslate"><span class="pre">row</span></code> and <code class="docutils literal notranslate"><span class="pre">col</span></code>umn as alive or dead,
depending on whether the argument <code class="docutils literal notranslate"><span class="pre">alive</span></code> is true or false. You can assume
that the given <code class="docutils literal notranslate"><span class="pre">row</span></code> and <code class="docutils literal notranslate"><span class="pre">col</span></code>umn are within the boundaries of the grid.</p>
</li>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Life</span></code> must provide the method:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">display</span><span class="p">()</span>
</pre></div>
</div>
<p>which prints on screen the current grid, showing each dead cell as <code class="docutils literal notranslate"><span class="pre">.</span></code> (dot)
and each live cell as <code class="docutils literal notranslate"><span class="pre">#</span></code>. For example, the “Blinker” pattern above in a 5
<span class="math notranslate nohighlight">\(\times\)</span> 5 grid may look like:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>.....
.....
.###.
.....
.....
</pre></div>
</div>
</li>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Life</span></code> must provide the method:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">step</span><span class="p">()</span>
</pre></div>
</div>
<p>which updates <code class="docutils literal notranslate"><span class="pre">this</span></code> object by evolving the grid of cells to their next state,
according to the 4 rules discussed above. For instance, if <code class="docutils literal notranslate"><span class="pre">life</span></code> is an
object of type <code class="docutils literal notranslate"><span class="pre">Life</span></code> and <code class="docutils literal notranslate"><span class="pre">life.display()</span></code> prints the “Blinker” pattern above,
then calling <code class="docutils literal notranslate"><span class="pre">life.next()</span></code> followed by <code class="docutils literal notranslate"><span class="pre">life.display()</span></code> should produce the
output:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>.....
..#..
..#..
..#..
.....
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Every cell in the grid has 8 neighbours — <em>except</em>:</p>
<ul class="simple">
<li><p>each cell located at a corner of the grid only has 3 neighbours;</p></li>
<li><p>each cell located along a border of the grid (but not in a corner) has 5
neighbours.</p></li>
</ul>
</div>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>Keep in mind that, when the method <code class="docutils literal notranslate"><span class="pre">step()</span></code> computes the next state of the
grid of cells, it needs to analyse the state of each cell in the <em>current</em>
grid. This means that you should <em>not</em> change the current grid before the
next grid has been computed. When the next grid is fully computed, then it
can replace the current grid…</p>
</div>
</li>
</ul>
<p>The handout includes some Java files called <code class="docutils literal notranslate"><span class="pre">Test01.java</span></code>, <code class="docutils literal notranslate"><span class="pre">Test02.java</span></code>, …
and also <code class="docutils literal notranslate"><span class="pre">TestTub.java</span></code>, <code class="docutils literal notranslate"><span class="pre">TestGlider.java</span></code>, <code class="docutils literal notranslate"><span class="pre">TestPentomino.java</span></code>: they are test
programs that use the code you should write in <code class="docutils literal notranslate"><span class="pre">Life.java</span></code>, and they might not
compile or work correctly until you complete your work. You should read those
test programs, try to run them, and also run <code class="docutils literal notranslate"><span class="pre">./grade</span></code> to see their expected
outputs — but you must <em>not</em> modify those files.</p>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Life.java</span></code> on DTU Autolab.</p>
<div class="admonition tip" id="tip-life-animation">
<p class="admonition-title">Tip</p>
<p>Each one of the test programs in the handout can also show an animation of the
game of life based on your implementation of the class <code class="docutils literal notranslate"><span class="pre">Life</span></code>. To see the
animations, you will need to open a terminal in the directory of the handout and
compile all Java files:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>javac *.java
</pre></div>
</div>
<p>Then you can execute each one of the test programs by providing the command-line
argument <code class="docutils literal notranslate"><span class="pre">animate</span></code>, e.g.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>java TestPentomino animate 10
</pre></div>
</div>
<p>where 10 is the number of steps per second (you can try different numbers). The
animation should look as follows:</p>
<img alt="Game of life animation" class="align-center" src="_images/game-of-life-animation.gif" />
<p>To terminate the program, press <code class="docutils literal notranslate"><span class="pre">Ctrl</span></code>+<code class="docutils literal notranslate"><span class="pre">C</span></code>.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><strong>For Windows users:</strong> if the animation is not displayed correctly, make
sure that the console program you are using is Git Bash
(<a class="reference internal" href="overview.html#sec-autolab-demo-assessment"><span class="std std-ref">as recommended</span></a>) or
<a class="reference external" href="https://adamtheautomator.com/windows-open-powershell-in-a-folder/">Powershell</a>.</p>
</div>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The automatic grading on DTU Autolab includes some additional secret checks that
test your submission with various cell patterns. After you submit, double-check
your grading result on DTU Autolab: if the secret checks fail, then your
solution is not correct, and you should fix it and resubmit.</p>
</div>
</section>
<section id="dtu-christmas-party-management-with-file-i-o">
<span id="lab-day-7-party"></span><h2>02 - DTU Christmas Party Management with File I/O<a class="headerlink" href="#dtu-christmas-party-management-with-file-i-o" title="Permalink to this heading">#</a></h2>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>For this assessment you must submit <strong>two files: <code class="docutils literal notranslate"><span class="pre">Person.java</span></code> and
<code class="docutils literal notranslate"><span class="pre">FileUtils.java</span></code></strong>. For the submission instructions, see the
<a class="reference internal" href="#note-christmas-grading-zipfile"><span class="std std-ref">note at the end of this assessment</span></a>.</p>
</div>
<p>The goal of this assessment is to improve the DTU Christmas party management
software in <a class="reference internal" href="inheritance-2.html#eg-dtu-christmas-party-inheritance">Example 60</a>, adding the
capability of loading and saving lists of people (employees and guests) from/to
a file in <a class="reference external" href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV format</a>.</p>
<p>The contents of a CSV file with a few employees and guests should look as
follows:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Name,Type,Department,Ticket
Laura Lund,guest,,123
Alceste Scalas,employee,DTU Compute,
Oliver Olesen,employee,DTU Construct,
Emma Ebbesen,guest,,75
Ida Iversen,employee,DTU Aqua,
</pre></div>
</div>
<p>The first row contains the column headers, while each one of the remaining rows
represents a person at the party; each row contains 4 fields (possibly empty)
separated by a comma <code class="docutils literal notranslate"><span class="pre">,</span></code>.</p>
<p>If the rows above are saved in a file called e.g. <code class="docutils literal notranslate"><span class="pre">people.csv</span></code>, it is possible
to open the file with a spreadsheet program like <a class="reference external" href="https://www.libreoffice.org/discover/calc/">LibreOffice
Calc</a> or <a class="reference external" href="https://www.microsoft.com/en-us/microsoft-365/excel">Microsoft
Excel</a>, and its contents
would look as follows:</p>
<table class="table">
<thead>
<tr class="row-odd"><th class="head"><p>Name</p></th>
<th class="head"><p>Type</p></th>
<th class="head"><p>Department</p></th>
<th class="head"><p>Ticket</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Laura Lund</p></td>
<td><p>guest</p></td>
<td></td>
<td><p>123</p></td>
</tr>
<tr class="row-odd"><td><p>Alceste Scalas</p></td>
<td><p>employee</p></td>
<td><p>DTU Compute</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p>Oliver Olesen</p></td>
<td><p>employee</p></td>
<td><p>DTU Construct</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p>Emma Ebbesen</p></td>
<td><p>guest</p></td>
<td></td>
<td><p>75</p></td>
</tr>
<tr class="row-even"><td><p>Ida Iversen</p></td>
<td><p>employee</p></td>
<td><p>DTU Aqua</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>Observe that:</p>
<ul class="simple">
<li><p>when the “Type” of the row is “employee”, then the “Ticket” is empty;</p></li>
<li><p>when the “Type” of the row is “guest”, then the “Department” is empty.</p></li>
</ul>
<p>Your task is to implement the CSV load and save functionality, in two steps:</p>
<ul class="simple">
<li><p><a class="reference internal" href="#lab-day-7-christmas-load"><span class="std std-ref">Step 1 - Loading from CSV File</span></a></p></li>
<li><p><a class="reference internal" href="#lab-day-7-christmas-save"><span class="std std-ref">Step 2 - Saving to CSV File</span></a></p></li>
</ul>
<section id="step-1-loading-from-csv-file">
<span id="lab-day-7-christmas-load"></span><h3>Step 1 - Loading from CSV File<a class="headerlink" href="#step-1-loading-from-csv-file" title="Permalink to this heading">#</a></h3>
<p>Edit the class <code class="docutils literal notranslate"><span class="pre">FileUtils</span></code> in the file <code class="docutils literal notranslate"><span class="pre">FileUtils.java</span></code> and implement the
following <code class="docutils literal notranslate"><span class="pre">static</span></code> method:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="nf">load</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">fname</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">java</span><span class="p">.</span><span class="na">io</span><span class="p">.</span><span class="na">IOException</span>
</pre></div>
</div>
<p>This static method must read a CSV file (called <code class="docutils literal notranslate"><span class="pre">fname</span></code>) with the format
outlined above, and return a list of corresponding objects — which may be
either of the class <code class="docutils literal notranslate"><span class="pre">Employee</span></code> or <code class="docutils literal notranslate"><span class="pre">Guest</span></code> (hence such objects can be contained
in an array of <code class="docutils literal notranslate"><span class="pre">Person</span></code>s). You can assume that the file respects the CSV format
described above.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<ul class="simple">
<li><p>The class <code class="docutils literal notranslate"><span class="pre">FileUtils</span></code> provided in the handout contains a <code class="docutils literal notranslate"><span class="pre">private</span></code> static
method called <code class="docutils literal notranslate"><span class="pre">countFileLines(fname)</span></code> that returns the number of lines
contained in the file called <code class="docutils literal notranslate"><span class="pre">fname</span></code> (or throws an <code class="docutils literal notranslate"><span class="pre">IOException</span></code> if the file
cannot be opened). This can be useful to determine the size of the array that
needs to be created by the static method <code class="docutils literal notranslate"><span class="pre">FileUtils.readFile(...)</span></code>…</p></li>
<li><p>Remember that you need to skip the first line of the CSV file, since it
contains the column headers.</p></li>
<li><p>To extract the values from a line read from the CSV file, you might split a
string around the separator <code class="docutils literal notranslate"><span class="pre">","</span></code>. For more details, see
<a class="reference internal" href="arrays.html#eg-splitting-string-into-array">Example 35</a>…</p></li>
<li><p>To convert the content of a <code class="docutils literal notranslate"><span class="pre">String</span></code> object into an <code class="docutils literal notranslate"><span class="pre">int</span></code>eger value, you can
use the static method <code class="docutils literal notranslate"><span class="pre">Integer.parseInt(...)</span></code>. For instance,
<code class="docutils literal notranslate"><span class="pre">Integer.parseInt("42")</span></code> returns the <code class="docutils literal notranslate"><span class="pre">int</span></code> value 42. This can be useful to
convert a string read from a CSV file into the ticket number (of type <code class="docutils literal notranslate"><span class="pre">int</span></code>)
required by the constructor of the class <code class="docutils literal notranslate"><span class="pre">Guest</span></code>.</p></li>
</ul>
</div>
</section>
<section id="step-2-saving-to-csv-file">
<span id="lab-day-7-christmas-save"></span><h3>Step 2 - Saving to CSV File<a class="headerlink" href="#step-2-saving-to-csv-file" title="Permalink to this heading">#</a></h3>
<p>First, you need to edit the file <code class="docutils literal notranslate"><span class="pre">Person.java</span></code> and add the following <code class="docutils literal notranslate"><span class="pre">abstract</span></code>
method to the class <code class="docutils literal notranslate"><span class="pre">Person</span></code>:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kd">abstract</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">toCSV</span><span class="p">()</span>
</pre></div>
</div>
<p>The method must return a CSV line describing <code class="docutils literal notranslate"><span class="pre">this</span></code> object. You will need to
implement the method for both classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code>, according to the
CSV specification discussed above.</p>
<p>Then, you need to edit the class <code class="docutils literal notranslate"><span class="pre">FileUtils</span></code> in the file <code class="docutils literal notranslate"><span class="pre">FileUtils.java</span></code> and
implement the following <code class="docutils literal notranslate"><span class="pre">static</span></code> method:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">save</span><span class="p">(</span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="n">people</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">fname</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">java</span><span class="p">.</span><span class="na">io</span><span class="p">.</span><span class="na">IOException</span>
</pre></div>
</div>
<p>This static method must save the given array of <code class="docutils literal notranslate"><span class="pre">people</span></code> in the CSV file called
<code class="docutils literal notranslate"><span class="pre">fname</span></code>, according to the CSV format described above. If the file already
exists, this static method must overwrite it.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<ul class="simple">
<li><p>Do not forget to add the headers at the beginning of the CSV file.</p></li>
<li><p>To turn each element of the array <code class="docutils literal notranslate"><span class="pre">people</span></code> into a line in the CSV file, you
can call the method <code class="docutils literal notranslate"><span class="pre">.toCSV()</span></code> implemented above.</p></li>
</ul>
</div>
<div class="admonition note" id="note-christmas-grading-zipfile">
<p class="admonition-title">Note</p>
<p>For this assessment you need to prepare and submit a ZIP file containing your
modified versions of <code class="docutils literal notranslate"><span class="pre">Person.java</span></code> and <code class="docutils literal notranslate"><span class="pre">FileUtils.java</span></code>. To prepare that ZIP
file, you can simply execute from the terminal (inside the handout directory):</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>./grade -z
</pre></div>
</div>
<p>This command will grade your work <em>and</em> prepare a ZIP file that you can then
submit on DTU Autolab.</p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The automatic grading on DTU Autolab includes some additional secret checks that
test your submission by loading and saving various lists of people. After you
submit, double-check your grading result on DTU Autolab: if the secret checks
fail, then your solution is not correct, and you should fix it and resubmit.</p>
</div>
</section>
</section>
<section id="arithmetic-expressions-version-4">
<span id="lab-day-7-expressions4"></span><h2>03 - Arithmetic Expressions, Version 4<a class="headerlink" href="#arithmetic-expressions-version-4" title="Permalink to this heading">#</a></h2>
<p>This is a follow-up to the assessment <a class="reference internal" href="lab-day-5.html#lab-day-5-expressions3"><span class="std std-ref">03 - Arithmetic Expressions, Version 3</span></a>,
and the starting point is its solution, i.e. the file <code class="docutils literal notranslate"><span class="pre">Expression.java</span></code> (you can
use either your own file, or the one provided by the teacher as a solution to
<a class="reference internal" href="lab-day-5.html#lab-day-5-expressions3"><span class="std std-ref">03 - Arithmetic Expressions, Version 3</span></a>).</p>
<p>Your task is to improve the solution to
<a class="reference internal" href="lab-day-5.html#lab-day-5-expressions3"><span class="std std-ref">03 - Arithmetic Expressions, Version 3</span></a> by adding support for variables
that can be substituted with other expressions. After this task is completed,
it should become possible to represent an expression <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">+</span> <span class="pre">x</span></code> as follows:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Addition</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">Constant</span><span class="p">(</span><span class="mf">1.0</span><span class="p">),</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Variable</span><span class="p">(</span><span class="s">"x"</span><span class="p">));</span>
</pre></div>
</div>
<p>Then, calling <code class="docutils literal notranslate"><span class="pre">e.result()</span></code> should cause an error, because the value of the
variable <code class="docutils literal notranslate"><span class="pre">x</span></code> is undefined. However, we should be able to create a new
expression from <code class="docutils literal notranslate"><span class="pre">e</span></code> where the variable <code class="docutils literal notranslate"><span class="pre">x</span></code> is substituted with the constant
expression <code class="docutils literal notranslate"><span class="pre">5.0</span></code>, as follows:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">e2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">substitute</span><span class="p">(</span><span class="s">"x"</span><span class="p">,</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Constant</span><span class="p">(</span><span class="mf">5.0</span><span class="p">));</span>
</pre></div>
</div>
<p>and now, <code class="docutils literal notranslate"><span class="pre">e2</span></code> should correspond to the expression <code class="docutils literal notranslate"><span class="pre">1.0</span> <span class="pre">+</span> <span class="pre">5.0</span></code>, and calling
<code class="docutils literal notranslate"><span class="pre">e2.result()</span></code> should return 6.0. Meanwhile, the original expression <code class="docutils literal notranslate"><span class="pre">e</span></code> remains
unchanged.</p>
<p>To achieve this, your task is to extend the solution to
<a class="reference internal" href="lab-day-5.html#lab-day-5-expressions3"><span class="std std-ref">03 - Arithmetic Expressions, Version 3</span></a> as follows.</p>
<ul>
<li><p>The interface <code class="docutils literal notranslate"><span class="pre">Expression</span></code> must include the following new method:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="n">Expression</span><span class="w"> </span><span class="nf">substitute</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">varName</span><span class="p">,</span><span class="w"> </span><span class="n">Expression</span><span class="w"> </span><span class="n">eSubst</span><span class="p">);</span>
</pre></div>
</div>
<p>The idea is that calling this method returns an expression that is identical
to <code class="docutils literal notranslate"><span class="pre">this</span></code>, except that any occurrence of the variable <code class="docutils literal notranslate"><span class="pre">varName</span></code> is substituted
with the expression <code class="docutils literal notranslate"><span class="pre">eSubst</span></code>.</p>
</li>
<li><p>You need to implement a new class called <code class="docutils literal notranslate"><span class="pre">Variable</span></code> with a constructor
<code class="docutils literal notranslate"><span class="pre">Variable(name)</span></code>, where <code class="docutils literal notranslate"><span class="pre">name</span></code> is a <code class="docutils literal notranslate"><span class="pre">String</span></code> representing the name of the
variable. Then, if we define e.g.:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">expr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Variable</span><span class="p">(</span><span class="s">"x"</span><span class="p">);</span>
</pre></div>
</div>
<p>then the object <code class="docutils literal notranslate"><span class="pre">expr</span></code> must behave as follows:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">expr.format()</span></code> must return the variable name <code class="docutils literal notranslate"><span class="pre">x</span></code>;</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">expr.result()</span></code> must <a class="reference internal" href="errors.html#sec-errors-exceptions-throw"><span class="std std-ref">throw</span></a> an
<code class="docutils literal notranslate"><span class="pre">ArithmeticException</span></code> carrying the error message <code class="docutils literal notranslate"><span class="pre">Undefined</span> <span class="pre">variable:</span> <span class="pre">x</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">expr.substitute(varName,</span> <span class="pre">eSubst)</span></code> must work as follows:</p>
<ul>
<li><p>if <code class="docutils literal notranslate"><span class="pre">varName</span></code> is equal to the variable name of <code class="docutils literal notranslate"><span class="pre">expr</span></code> (i.e. <code class="docutils literal notranslate"><span class="pre">"x"</span></code>), then
the method must return the expression <code class="docutils literal notranslate"><span class="pre">eSubst</span></code>;</p></li>
<li><p>otherwise, the method must return the expression <code class="docutils literal notranslate"><span class="pre">expr</span></code> as it is.</p></li>
</ul>
</li>
</ul>
</li>
<li><p>You also need to implement the method <code class="docutils literal notranslate"><span class="pre">substitute(varName,</span> <span class="pre">eSubst)</span></code> for all
the other classes that implement the interface <code class="docutils literal notranslate"><span class="pre">Expression</span></code>. So, if <code class="docutils literal notranslate"><span class="pre">expr</span></code> is
an object of type <code class="docutils literal notranslate"><span class="pre">Expression</span></code>, <code class="docutils literal notranslate"><span class="pre">expr.substitute(varName,</span> <span class="pre">eSubst)</span></code> must work
as follows:</p>
<ul class="simple">
<li><p>if <code class="docutils literal notranslate"><span class="pre">expr</span></code> is a <code class="docutils literal notranslate"><span class="pre">Constant</span></code>, the method must return <code class="docutils literal notranslate"><span class="pre">expr</span></code> itself (because
there are no variables to substitute inside a constant);</p></li>
<li><p>if <code class="docutils literal notranslate"><span class="pre">expr</span></code> is an <code class="docutils literal notranslate"><span class="pre">Addition</span></code> between two expressions <code class="docutils literal notranslate"><span class="pre">e1</span></code> and <code class="docutils literal notranslate"><span class="pre">e2</span></code>, then the
method must create and return a new object of type <code class="docutils literal notranslate"><span class="pre">Addition</span></code> constructed
with the expressions obtained by substituting <code class="docutils literal notranslate"><span class="pre">varName</span></code> with <code class="docutils literal notranslate"><span class="pre">eSubst</span></code> in
both <code class="docutils literal notranslate"><span class="pre">e1</span></code> and <code class="docutils literal notranslate"><span class="pre">e2</span></code>;</p></li>
<li><p>if <code class="docutils literal notranslate"><span class="pre">expr</span></code> is a <code class="docutils literal notranslate"><span class="pre">Subtraction</span></code> or <code class="docutils literal notranslate"><span class="pre">Division</span></code> or <code class="docutils literal notranslate"><span class="pre">Multiplication</span></code>, the
behaviour of the method is similar;</p></li>
<li><p>if <code class="docutils literal notranslate"><span class="pre">expr</span></code> is a <code class="docutils literal notranslate"><span class="pre">Minus</span></code> of an expression <code class="docutils literal notranslate"><span class="pre">e</span></code>, then the method must create and
return a new object of type <code class="docutils literal notranslate"><span class="pre">Minus</span></code> constructed with the expression obtained
by substituting <code class="docutils literal notranslate"><span class="pre">varName</span></code> with <code class="docutils literal notranslate"><span class="pre">eSubst</span></code> in <code class="docutils literal notranslate"><span class="pre">e</span></code>;</p></li>
<li><p>if <code class="docutils literal notranslate"><span class="pre">expr</span></code> is an <code class="docutils literal notranslate"><span class="pre">AbsoluteValue</span></code> or <code class="docutils literal notranslate"><span class="pre">SquareRoot</span></code>, the behaviour of the method
is similar;</p></li>
</ul>
</li>
</ul>
<p>After your modifications, the classes in <code class="docutils literal notranslate"><span class="pre">Expression.java</span></code> must match the UML
diagram in <a class="reference internal" href="#fig-uml-expressions-inheritance-unary-variable"><span class="std std-numref">Fig. 34</span></a> below —
where the differences with <a class="reference internal" href="lab-day-5.html#lab-day-5-expressions3"><span class="std std-ref">03 - Arithmetic Expressions, Version 3</span></a> are the
new method <code class="docutils literal notranslate"><span class="pre">substitute(...)</span></code> in the interface <code class="docutils literal notranslate"><span class="pre">Expression</span></code>, and the new class
<code class="docutils literal notranslate"><span class="pre">Variable</span></code>.</p>
<figure class="align-default" id="fig-uml-expressions-inheritance-unary-variable">
<img alt="UML class diagram for "Arithmetic expressions, version 4" assessment" src="_images/expression-abstract-class-unary-variable.svg" /><figcaption>
<p><span class="caption-number">Fig. 34 </span><span class="caption-text">UML class diagram for <a class="reference internal" href="#lab-day-7-expressions4"><span class="std std-ref">03 - Arithmetic Expressions, Version 4</span></a>.</span><a class="headerlink" href="#fig-uml-expressions-inheritance-unary-variable" title="Permalink to this image">#</a></p>
</figcaption>
</figure>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Expression.java</span></code> on DTU Autolab.</p>
<p>The handout includes some Java files called <code class="docutils literal notranslate"><span class="pre">Test01.java</span></code>, <code class="docutils literal notranslate"><span class="pre">Test02.java</span></code>, etc.:
they are test programs that use the code you should write in <code class="docutils literal notranslate"><span class="pre">Expression.java</span></code>,
and they might not compile or work correctly until you complete your work. You
should read those test programs, try to run them, and also run <code class="docutils literal notranslate"><span class="pre">./grade</span></code> to see
their expected outputs — but you must <em>not</em> modify those files.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The automatic grading on DTU Autolab includes some additional secret checks that
test your submission with more expressions. After you submit, double-check your
grading result on DTU Autolab: if the secret checks fail, then your solution is
not correct, and you should fix it and resubmit.</p>
</div>
</section>
<section id="video-game-monsters-part-6">
<span id="lab-day-7-monsters-6"></span><h2>04 - Video Game Monsters, Part 6<a class="headerlink" href="#video-game-monsters-part-6" title="Permalink to this heading">#</a></h2>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>For this assessment you must submit <strong>two files: <code class="docutils literal notranslate"><span class="pre">Monster.java</span></code> and
<code class="docutils literal notranslate"><span class="pre">GameUtils.java</span></code></strong>. For the submission instructions, see the
<a class="reference internal" href="#note-monsters6-grading-zipfile"><span class="std std-ref">note at the end of this assessment</span></a>.</p>
</div>
<p>This is a follow-up to the assessment
<a class="reference internal" href="lab-day-5.html#lab-day-5-monsters-5"><span class="std std-ref">04 - Video Game Monsters, Part 5</span></a>, and the starting point is its
solution, i.e. the files <code class="docutils literal notranslate"><span class="pre">Monster.java</span></code> and <code class="docutils literal notranslate"><span class="pre">GameUtils.java</span></code> (you can use either
your own files, or the ones provided by the teacher as a solution to
<a class="reference internal" href="lab-day-5.html#lab-day-5-monsters-5"><span class="std std-ref">04 - Video Game Monsters, Part 5</span></a>).</p>
<p>The development of the video game has a new requirement: the player must be able
to cast and remove a curse on <em>all</em> the monsters (dead or alive) in the game,
all at the same time. When a monster is cursed, the damage it takes when hit or
burnt is multiplied by 3, <em>after</em> applying the monster’s damage factors.</p>
<p>For example, suppose that a monster called Slimer has burning damage factor 0.5
(i.e. it takes half the damage when burnt); also suppose that the player burns
Slimer, applying 10 points of burning damage:</p>
<ul class="simple">
<li><p>when the curse is <em>not</em> active, Slimer takes 10 <span class="math notranslate nohighlight">\(\times\)</span> 0.5 = 5
points of burning damage, as usual;</p></li>
<li><p>when the curse is active, Slimer takes 3 times that amount of damage — i.e.
Slimer takes 10 <span class="math notranslate nohighlight">\(\times\)</span> 0.5 <span class="math notranslate nohighlight">\(\times\)</span> 3 = 15 points of burning damage.</p></li>
</ul>
<p>Your task is to add support for the curse feature to the class <code class="docutils literal notranslate"><span class="pre">Monster</span></code>, and to
the static method <code class="docutils literal notranslate"><span class="pre">GameUtils.playGame()</span></code>, as follows.</p>
<ul>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Monster</span></code> must support two new static methods:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">curseAll</span><span class="p">()</span>
<span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">uncurseAll</span><span class="p">()</span>
</pre></div>
</div>
<p>These two static methods activate and deactivate the curse, respectively. The
curse is initially <em>not</em> active. When the curse is active, if <code class="docutils literal notranslate"><span class="pre">m</span></code> is an
object of the class <code class="docutils literal notranslate"><span class="pre">Monster</span></code>, then the behaviour of some methods changes
as follows:</p>
<ul>
<li><p>the method <code class="docutils literal notranslate"><span class="pre">m.getDescription()</span></code> must report that the monster is cursed —
so, the returned string might look like:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Brainy (CURSED Mindflayer; health: 300)
</pre></div>
</div>
</li>
<li><p>the methods <code class="docutils literal notranslate"><span class="pre">m.hit(...)</span></code> and <code class="docutils literal notranslate"><span class="pre">m.burn(...)</span></code> must triple their damage, as
described above;</p></li>
<li><p>the method <code class="docutils literal notranslate"><span class="pre">m.getSymbol()</span></code> must return the <em>lowercase</em> initial character of
the monster’s species (instead of the uppercase initial character).</p></li>
</ul>
</li>
<li><p>The method <code class="docutils literal notranslate"><span class="pre">GameUtils.playGame()</span></code> must support two new commands, which may
be written by the user who plays the game:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">curse</span></code> - this command causes <code class="docutils literal notranslate"><span class="pre">playGame(....)</span></code> to activate the curse (by
calling <code class="docutils literal notranslate"><span class="pre">Monster.curseAll()</span></code>) and then continue the game loop;</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">uncurse</span></code> - this command causes <code class="docutils literal notranslate"><span class="pre">playGame(....)</span></code> to deactivate the curse (by
calling <code class="docutils literal notranslate"><span class="pre">Monster.uncurseAll()</span></code>) and then continue the game loop.</p></li>
</ul>
</li>
</ul>
<div class="admonition note" id="note-monsters6-grading-zipfile">
<p class="admonition-title">Note</p>
<p>For this assessment you need to prepare and submit a ZIP file containing your
modified versions of <code class="docutils literal notranslate"><span class="pre">Monster.java</span></code> and <code class="docutils literal notranslate"><span class="pre">GameUtils.java</span></code>. To prepare that ZIP
file, you can simply execute from the terminal (inside the handout directory):</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>./grade -z
</pre></div>
</div>
<p>This command will grade your work <em>and</em> prepare a ZIP file that you can then
submit on DTU Autolab.</p>
</div>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>To store the activation status of the curse, you could add a <code class="docutils literal notranslate"><span class="pre">private</span> <span class="pre">static</span></code>
field of the class <code class="docutils literal notranslate"><span class="pre">Monster</span></code>…</p>
</div>
</section>
</section>
</article>
<footer class="bd-footer-article">
<div class="footer-article-items footer-article__inner">
<div class="footer-article-item"><!-- Previous / next buttons -->
<div class="prev-next-area">
<a class="left-prev"
href="lab-day-6.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Module 13, Part 1: Lab Day — Exam Simulation</p>
</div>
</a>
<a class="right-next"
href="changelog.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">ChangeLog</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>
</div></div>
</div>
</footer>
</div>
<div class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> Contents
</div>
<nav class="bd-toc-nav page-toc">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#game-of-life">01 - Game of Life</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#dtu-christmas-party-management-with-file-i-o">02 - DTU Christmas Party Management with File I/O</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#step-1-loading-from-csv-file">Step 1 - Loading from CSV File</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#step-2-saving-to-csv-file">Step 2 - Saving to CSV File</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#arithmetic-expressions-version-4">03 - Arithmetic Expressions, Version 4</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#video-game-monsters-part-6">04 - Video Game Monsters, Part 6</a></li>
</ul>
</nav></div>
</div></div>
</div>
<footer class="bd-footer-content">
<div class="bd-footer-content__inner container">
<div class="footer-item">
</div>
<div class="footer-item">
<p class="copyright">
© Copyright 2024 by Alceste Scalas.
<br/>
</p>
</div>
<div class="footer-item">
</div>
<div class="footer-item">
<div class="extra_footer">
<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons Licence" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png" /></a> This work is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License</a>.
</div>
</div>
</div>
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script src="_static/scripts/bootstrap.js?digest=e353d410970836974a52"></script>
<script src="_static/scripts/pydata-sphinx-theme.js?digest=e353d410970836974a52"></script>
<footer class="bd-footer">
</footer>
</body>
</html>