-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab-day-1.html
650 lines (533 loc) · 36.3 KB
/
lab-day-1.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
<!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 3, Part 1: Lab Day — 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="Module 3, Part 2: Loops" href="loops.html" />
<link rel="prev" title="Module 2, Part 2: Console I/O, Conditionals, Strings" href="io-conditionals-strings.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 current active"><a class="current reference internal" href="#">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"><a class="reference internal" href="exam-qa-lab.html">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 3, Part 1: Lab Day</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="#guess-the-animal">01 - Guess the Animal</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#guess-the-animal-2">02 - Guess the Animal 2</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#leap-year">03 - Leap Year</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#days-in-a-month">04 - Days in a Month</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#sales-discount">05 - Sales Discount</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#quadratic-equation">06 - Quadratic Equation</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-3-part-1-lab-day">
<span id="mod-lab-day-1"></span><h1>Module 3, Part 1: Lab Day<a class="headerlink" href="#module-3-part-1-lab-day" title="Permalink to this heading">#</a></h1>
<p>This first part of Module 3 does not introduce new contents: instead, it
provides a series of assignments that focus on console I/O and conditionals, to
improve your Java programming skills. All the following assignments can be
solved with the Java programming notions introduced in Module 2.
(New contents are introduced in <a class="reference internal" href="loops.html#mod-loops"><span class="std std-ref">Module 3, Part 2: Loops</span></a>.)</p>
<ul class="simple">
<li><p><a class="reference internal" href="#lab-lab-day-1-animal"><span class="std std-ref">01 - Guess the Animal</span></a></p></li>
<li><p><a class="reference internal" href="#lab-lab-day-1-animal-2"><span class="std std-ref">02 - Guess the Animal 2</span></a></p></li>
<li><p><a class="reference internal" href="#lab-lab-day-1-leap-year"><span class="std std-ref">03 - Leap Year</span></a></p></li>
<li><p><a class="reference internal" href="#lab-lab-day-1-month-days"><span class="std std-ref">04 - Days in a Month</span></a></p></li>
<li><p><a class="reference internal" href="#lab-lab-day-1-discount"><span class="std std-ref">05 - Sales Discount</span></a></p></li>
<li><p><a class="reference internal" href="#lab-lab-day-1-quadratic"><span class="std std-ref">06 - Quadratic Equation</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="guess-the-animal">
<span id="lab-lab-day-1-animal"></span><h2>01 - Guess the Animal<a class="headerlink" href="#guess-the-animal" title="Permalink to this heading">#</a></h2>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Your solution to <a class="reference internal" href="#lab-lab-day-1-animal-2"><span class="std std-ref">02 - Guess the Animal 2</span></a> below can also be submitted as a
solution to this assignment. However, <a class="reference internal" href="#lab-lab-day-1-animal-2"><span class="std std-ref">02 - Guess the Animal 2</span></a> is more
complex — so it may be easier for you to first solve this assignment, and then
improve your solution to solve the next one.</p>
</div>
<p>Edit the file <code class="docutils literal notranslate"><span class="pre">Animal.java</span></code> provided in the handout, and write a program that
guesses what animal the user is thinking of. The animal can be either penguin,
sparrow, camel, or kangaroo. The program must work as follows.</p>
<ul class="simple">
<li><p>When the program starts, it prints “Is the animal a bird?” and reads an answer
from the console (as a <code class="docutils literal notranslate"><span class="pre">String</span></code>).</p>
<ul>
<li><p>If the answer is “Yes”, then the program prints “Does it fly?” and reads an
answer from the console (as a <code class="docutils literal notranslate"><span class="pre">String</span></code>).</p>
<ul>
<li><p>If the answer is “Yes”, then the program prints “The animal is a sparrow”;</p></li>
<li><p>Otherwise, the program prints “The animal is a penguin”.</p></li>
</ul>
</li>
<li><p>Otherwise, the program prints “Does it jump?” and reads an answer from the
console (as a <code class="docutils literal notranslate"><span class="pre">String</span></code>).</p>
<ul>
<li><p>If the answer is “Yes”, then the program prints “The animal is a kangaroo”;</p></li>
<li><p>Otherwise, the program prints “The animal is a camel”.</p></li>
</ul>
</li>
</ul>
</li>
</ul>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Animal.java</span></code> on DTU Autolab.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>Remember that, to check whether two strings <code class="docutils literal notranslate"><span class="pre">s1</span></code> and <code class="docutils literal notranslate"><span class="pre">s2</span></code> are equal to each other,
you will need to use <code class="docutils literal notranslate"><span class="pre">s1.equals(s2)</span></code> (see <a class="reference internal" href="io-conditionals-strings.html#sec-io-cond-strings-strings-comparison"><span class="std std-ref">how to compare strings</span></a>).</p>
</div>
</section>
<section id="guess-the-animal-2">
<span id="lab-lab-day-1-animal-2"></span><h2>02 - Guess the Animal 2<a class="headerlink" href="#guess-the-animal-2" title="Permalink to this heading">#</a></h2>
<p>This is a follow-up to <a class="reference internal" href="#lab-lab-day-1-animal"><span class="std std-ref">01 - Guess the Animal</span></a>: the task is the same, but
the program <code class="docutils literal notranslate"><span class="pre">Animal.java</span></code> must now perform more accurate checks on the user’s
answers. More specifically:</p>
<ul>
<li><p>After each questions, the user is allowed to answer either “yes” or “no” using
uppercase or lowercase letters. For instance, the program must accept both
“YeS” and “nO” as valid answers.</p></li>
<li><p>If the user answers something else, then the program must print “Invalid
answer!” and end. For instance, here is a possible execution of the program:
(the highlighted lines are the user’s answers)</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Is the animal a bird?
<span class="hll">yES
</span>Does it fly?
<span class="hll">Dunno
</span>Invalid answer!
</pre></div>
</div>
</li>
</ul>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Animal.java</span></code> on DTU Autolab.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<ul class="simple">
<li><p>To solve this assignment, you could reuse and improve your solution to
<a class="reference internal" href="#lab-lab-day-1-animal"><span class="std std-ref">01 - Guess the Animal</span></a>.</p></li>
<li><p>Before comparing the user’s answer with “yes” or “no”, it may be handy to
<a class="reference internal" href="io-conditionals-strings.html#sec-io-cond-strings-strings-methods"><span class="std std-ref">convert the answer to uppercase or lowercase</span></a>…</p></li>
</ul>
</div>
</section>
<section id="leap-year">
<span id="lab-lab-day-1-leap-year"></span><h2>03 - Leap Year<a class="headerlink" href="#leap-year" title="Permalink to this heading">#</a></h2>
<p>Edit the file <code class="docutils literal notranslate"><span class="pre">LeapYear.java</span></code> provided in the handout, and write a program that
reads an <code class="docutils literal notranslate"><span class="pre">int</span></code>eger value (representing a year) and prints whether the given year
is a leap year or not.</p>
<p>For instance, here are two possible executions of the program: (in both cases,
the highlighted line is the user’s input)</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll">2024
</span>The year 2024 is a leap year
</pre></div>
</div>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll">1900
</span>The year 1900 is not a leap year
</pre></div>
</div>
<p>In order to determine whether a given year <span class="math notranslate nohighlight">\(n\)</span> is a leap year, you can follow
this flowchart.</p>
<p><img alt="Leap year flowchart" src="_images/leap-year.drawio.svg" /></p>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">LeapYear.java</span></code> on DTU Autolab.</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>To determine whether a number <span class="math notranslate nohighlight">\(n\)</span> is divisible by <span class="math notranslate nohighlight">\(m\)</span>, you can use the
<a class="reference internal" href="java-first-taste.html#table-numerical-ops"><span class="std std-ref">modulo (a.k.a. remainder) operator ‘%’ in Java</span></a>: e.g.,
<code class="docutils literal notranslate"><span class="pre">15</span> <span class="pre">%</span> <span class="pre">3</span></code> gives the result 0, hence 15 is divisible by 3; instead, <code class="docutils literal notranslate"><span class="pre">15</span> <span class="pre">%</span> <span class="pre">2</span></code>
gives a result that is not equal to 0, hence 15 is not divisible by 2.
<em>(Try it on the Java shell!)</em></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 with various years. 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="days-in-a-month">
<span id="lab-lab-day-1-month-days"></span><h2>04 - Days in a Month<a class="headerlink" href="#days-in-a-month" title="Permalink to this heading">#</a></h2>
<p>Edit the file <code class="docutils literal notranslate"><span class="pre">MonthDays.java</span></code> provided in the handout, and write a program that
reads a <code class="docutils literal notranslate"><span class="pre">String</span></code> (representing the name of a month) followed by an <code class="docutils literal notranslate"><span class="pre">int</span></code>eger
value (representing a year), and prints on the console how many days are in that
month, in that year.</p>
<p>For instance, here is a possible execution of the program:
(the highlighted lines are the user’s input)</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll">aPRil
</span><span class="hll">2000
</span>The month of aPRil 2000 has 30 days
</pre></div>
</div>
<p>Observe that the user could write the name of the month using any combination of
uppercase and lowercase letters; also, the message printed by the program must
show the name of the month exactly as it was written by the user.</p>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">MonthDays.java</span></code> on DTU Autolab.</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<ul class="simple">
<li><p>You will need to compare the name of the month provided by the user with
the possible month names. To this purpose, it may be handy to
<a class="reference internal" href="io-conditionals-strings.html#sec-io-cond-strings-strings-methods"><span class="std std-ref">convert the name of the month to uppercase or lowercase</span></a>…</p></li>
<li><p>To correctly compute the number of days for the month of February, you will
need to check whether the user wrote a leap year: to this purpose, you can
reuse and adapt the code from your solution to <a class="reference internal" href="#lab-lab-day-1-leap-year"><span class="std std-ref">03 - Leap Year</span></a>.</p></li>
<li><p>You could write a more compact solution by using
a <a class="reference internal" href="io-conditionals-strings.html#sec-io-cond-strings-conditions-switch-expr"><span class="std std-ref">Switch Expression</span></a> and
a <a class="reference internal" href="io-conditionals-strings.html#sec-io-cond-strings-conditions-conditional-expr"><span class="std std-ref">Conditional Expression</span></a>.</p></li>
</ul>
</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 combinations of months and years. 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="sales-discount">
<span id="lab-lab-day-1-discount"></span><h2>05 - Sales Discount<a class="headerlink" href="#sales-discount" title="Permalink to this heading">#</a></h2>
<p>Your task is to write a program that computes discounted prices for a shop. Edit
the file <code class="docutils literal notranslate"><span class="pre">Discount.java</span></code> provided in the handout, and write a program that reads
a <code class="docutils literal notranslate"><span class="pre">double</span></code> value from the console (representing a price) and computes and prints
a corresponding discounted price, with the applied discount percentage.</p>
<p>The program must apply the following rules:</p>
<ul class="simple">
<li><p>if the price is 1000 or more, then the applied discount must be 20%;</p></li>
<li><p>otherwise, if the price 500 or more, then the applied discount must be 10%;</p></li>
<li><p>otherwise, if the price 250 or more, then the applied discount must be 5%;</p></li>
<li><p>otherwise, the applied discount must be 0% (i.e., no discount).</p></li>
</ul>
<p>For instance, here are two possible executions of the program: (in both cases,
the highlighted line is the user’s input)</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll">2500.10
</span>The discounted price is 2000.08 (discount: 20%)
</pre></div>
</div>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><span class="hll">300
</span>The discounted price is 285.0 (discount: 5%)
</pre></div>
</div>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Discount.java</span></code> on DTU Autolab.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<ul class="simple">
<li><p>After you create a <code class="docutils literal notranslate"><span class="pre">Scanner</span></code> object, remember to
<a class="reference internal" href="io-conditionals-strings.html#remark-scanner-localisation">set its localisation to English</a>.</p></li>
<li><p>Remember that <a class="reference internal" href="java-first-taste.html#sec-basic-programming-java-jshell-fractional"><span class="std std-ref">numerical operations in Java maintain the type of the operands</span></a>:
e.g., a division between <code class="docutils literal notranslate"><span class="pre">int</span></code>eger values gives an <code class="docutils literal notranslate"><span class="pre">int</span></code>eger value as a result.
Therefore, if your program needs e.g. to represent the discount percentage of
20%, and it tries to achieve it by computing <code class="docutils literal notranslate"><span class="pre">20</span> <span class="pre">/</span> <span class="pre">100</span></code>, then the result of
the division will be <code class="docutils literal notranslate"><span class="pre">0</span></code> <em>(try it on the Java shell!)</em>: this might not be what
you want! Instead, you may want to take
advantage of <a class="reference internal" href="java-first-taste.html#remark-type-promotion">type promotion</a>, i.e., make
sure that at least one of the operands in a numerical operation has type <code class="docutils literal notranslate"><span class="pre">double</span></code>,
to perform the computation in the realm of <code class="docutils literal notranslate"><span class="pre">double</span></code>s and return a result of
type <code class="docutils literal notranslate"><span class="pre">double</span></code> (thus including fractional parts). For instance, the division
<code class="docutils literal notranslate"><span class="pre">20</span> <span class="pre">/</span> <span class="pre">100.0</span></code> gives the result <code class="docutils literal notranslate"><span class="pre">0.2</span></code>, as you might expect
<em>(try it on the Java shell!)</em>.</p></li>
</ul>
</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 prices. 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="quadratic-equation">
<span id="lab-lab-day-1-quadratic"></span><h2>06 - Quadratic Equation<a class="headerlink" href="#quadratic-equation" title="Permalink to this heading">#</a></h2>
<p>Edit the file <code class="docutils literal notranslate"><span class="pre">Quadratic.java</span></code> provided in the handout, and write a program that
computes and displays the real roots (i.e. the real solutions) of the quadratic
equation <span class="math notranslate nohighlight">\(a x^2 + bx + c = 0\)</span>.</p>
<p>The program must read from the console three consecutive values for the
coefficients <span class="math notranslate nohighlight">\(a\)</span>, <span class="math notranslate nohighlight">\(b\)</span>, and <span class="math notranslate nohighlight">\(c\)</span> (of type <code class="docutils literal notranslate"><span class="pre">double</span></code>). Then:</p>
<ul class="simple">
<li><p>if the equation has two real roots, the program must print them with a space
in between — e.g. <code class="docutils literal notranslate"><span class="pre">1.5</span> <span class="pre">6.7</span></code></p></li>
<li><p>if the equation has only one real root, the program must print it <em>only once</em>
— e.g. <code class="docutils literal notranslate"><span class="pre">1.9</span></code></p></li>
<li><p>if the equation has no real roots, the program must print the text <code class="docutils literal notranslate"><span class="pre">No</span> <span class="pre">roots</span></code></p></li>
<li><p>if the equation has infinitely many roots, the program must print the text
<code class="docutils literal notranslate"><span class="pre">Infinitely</span> <span class="pre">many</span> <span class="pre">roots</span></code></p></li>
</ul>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Quadratic.java</span></code> on DTU Autolab.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<ul class="simple">
<li><p>You will need to compute a square root using <code class="docutils literal notranslate"><span class="pre">Math.sqrt(...)</span></code> (see the hints
for <a class="reference internal" href="io-conditionals-strings.html#lab-io-cond-strings-distance"><span class="std std-ref">06 - Line-Point Distance</span></a>).</p></li>
<li><p>Remember to take into account that <span class="math notranslate nohighlight">\(a\)</span>, <span class="math notranslate nohighlight">\(b\)</span>, and <span class="math notranslate nohighlight">\(c\)</span> can be zero!</p>
<ul>
<li><p>If <span class="math notranslate nohighlight">\(a = 0\)</span>, the quadratic equation becomes a linear equation: how is its
intersection with the <span class="math notranslate nohighlight">\(x\)</span>-axis computed? What happens if you try to
compute the discriminant?</p></li>
<li><p>What happens when both <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> are zero, but <span class="math notranslate nohighlight">\(c\)</span> is not zero? Does the
equation have any real roots?</p></li>
<li><p>What happens when <span class="math notranslate nohighlight">\(a\)</span>, <span class="math notranslate nohighlight">\(b\)</span>, and <span class="math notranslate nohighlight">\(c\)</span> are all zero? How many roots does the
equation have?</p></li>
</ul>
</li>
</ul>
</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="io-conditionals-strings.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 2, Part 2: Console I/O, Conditionals, Strings</p>
</div>
</a>
<a class="right-next"
href="loops.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Module 3, Part 2: Loops</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="#guess-the-animal">01 - Guess the Animal</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#guess-the-animal-2">02 - Guess the Animal 2</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#leap-year">03 - Leap Year</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#days-in-a-month">04 - Days in a Month</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#sales-discount">05 - Sales Discount</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#quadratic-equation">06 - Quadratic Equation</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>