Jfa13-Exe-1-4.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
CLAUDIO DE SIO CESARI LEARN JAVA FROM SCRATCH AND BECOME A PRO Exercices and Solutions Java for Aliens - Exercises and Solutions Copyright © 2019 by Claudio De Sio Cesari All rights reserved. No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, without the prior written permission of the author, except in the case of brief quotations permitted by copyright law. For permission requests, write to the author at the address: [email protected] Editor: Emanuele Giuliani ([email protected]) First Edition (November, 2019) Font licenses Libre Baskerville (https://fonts.google.com/specimen/Libre+Baskerville, Impallari Type): OFL Libre Franklin (https://fonts.google.com/specimen/Libre+Franklin, Impallari Type): OFL Cousine (https://fonts.google.com/specimen/Cousine, Steve Matteson): AL Inconsolata (https://fonts.google.com/specimen/Inconsolata, Raph Levien): OFL Roboto (https://fonts.google.com/specimen/Roboto, Christian Robertson): AL Digits (https://www.1001fonts.com/digits-font.html, Dieter Steffmann): FFC Journal Dingbats 3 (https://www.1001fonts.com/journal-dingbats-3-font.html, Dieter Steffmann): FFC Musicals (https://www.1001fonts.com/musicals-font.html, Brain Eaters): FFC Image licenses Curiosity icon (https://www.flaticon.com/free-icon/toyger-cat_107975, www.freepik.com): FBL Alien icon (http://www.iconarchive.com/show/free-space-icons-by-goodstuff-no-nonsense/alien-4-icon.html, goodstuffnononsense.com): CC Trick icon (https://www.flaticon.com/free-icon/magic-wand_1275106, www.flaticon.com/authors/pause08): FBL License specifications Open Free License (OFL): https://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL_web Apache License, Version 2.0 (AL): http://www.apache.org/licenses/LICENSE-2.0 1001Fonts Free For Commercial Use License (FFC): https://www.1001fonts.com/licenses/ffc.html Flaticon Basic License (FBL): https://file000.flaticon.com/downloads/license/license.pdf CC Attribution 4.0 (CC): https://creativecommons.org/licenses/by/4.0/legalcode Any other trademarks, service marks, product names or named features are assumed to be the property of their respective owners, and are used only for reference. There is no implied endorsement if we use one of these terms. Table of Contents Introduction to Exercises IX Chapter 1 Exercises 1 Chapter 1 Exercise Solutions 9 Chapter 2 Exercises 21 Chapter 2 Exercise Solutions 33 Chapter 3 Exercises 47 Chapter 3 Exercise Solutions 61 Chapter 4 Exercises 75 Chapter 4 Exercise Solutions 85 V Java for Aliens - Claudio De Sio Cesari (www.javaforaliens.com) Java for Aliens Exercises Introduction to Exercises This document represents, together with the Appendices document, the natural completion of the book “Java for Aliens”. All the exercises have been moved to this document so as not to take space away from the theory and not to increase the cost of the book. For each chapter of the book (and for each appendix, where applicable) specific exercises have been designed to validate (and expand) what has been learned during the study. The exercises are fundamental, essential. The theory is usually clear, but applying the concepts learned is far from easy. Developing not only includes the implementation of the code, but many other components are part of it, and they influence the final result. The exercises that you will find in this document therefore strive to insist on topics that other books treat superficially, or do not deal with at all, such as analysis, design and object-oriented architecture. There are also exercises for all subjects, even for those that may seem trivial to the more experienced reader, but which may be fundamental for the neophyte. In this book, you will find over 600 exercises! Warning! This document will be updated in the future. Currently there are some exercises not yet published since they need some fur- ther revisions. Updates will allow us to carry out further checks on the quality and correctness of the content, and add more exercises. The author will notify any news or information via his social media channels. IX Java for Aliens - Claudio De Sio Cesari (www.javaforaliens.com) Preface Based on reader feedback, this file has been organized and improved clarity and the style, and created exercises that can satisfy all types of readers. There are exercises in which it is required to add parts of code to fix bugs, code algorithms, design simple applications, or create more complex applications step by step. Dozens of single or multiple-choice exercises have also been introduced. They are similar to the test exercises of the Oracle Java programming certification exams. These exercises help in preparing for certifications, and have been set up in such a way that the programmer gains confidence. They often consist, essentially, of reading the code and understanding its meaning and details, and also represent a remarkable test bench for the most expert readers. I’m sure that the reader will appreciate the effort made to achieve such a large number of heterogeneous and original exercises. Unlike many other texts, we have provided solutions for all the exercises (with few excep- tions). Obviously, when it comes to coding a solution, there are hundreds of valid alterna- tives, so we must not consider the proposed solutions as, objectively, the better ones. You can download all the list of solutions and exercises (together with all the code examples included in the text) at the same address where you have downloaded the file you are reading: http://www.javaforaliens.com. Let’s remember once again that, especially for those who are beginners, it is important to start writing all the code by hand, without using copy-paste or special help from the development tool chosen. It is also very important to use comments for all of our code. This will allow us to learn the definitions better, and to have more security when writing code. So, it is fundamental to write the source code on a text editor such as Windows Notepad (as de- scribed in the first chapter) and compile using the command line (see Appendix A). We do not advise performing the exercises of these very first chapters (let’s say the first 4) using a complex IDE like Eclipse or Netbeans... we could end up studying the IDE rather than Java. It is, instead, advisable (if you do not want to have too much to do with Notepad and the com- mand line) to use EJE (https://sourceforge.net/projects/eje) which offers simple utilities designed for those who start to program. For example, it allows us to compile and execute our files by pressing two distinct buttons. EJE is easy to install ( just unzip the file in any folder) and use. How- ever, Appendix M is dedicated to its description. Finally we suggest, after having done an exercise, to consult the relative solution before moving on to the next one (often an exercise is a prerequisite for the next one). For any type of communication, you can write directly to the author at: [email protected]. You can also contact him via the most important social networks and through his personal X Java for Aliens - Claudio De Sio Cesari (www.javaforaliens.com) Preface website (also to keep up to date with news, the Telegram group will notify you only for news concerning this book): E Telegram: http://t.me/java4aliens E Facebook: http://www.facebook.com/claudiodesiocesari E Twitter: http://twitter.com/cdesio E LinkedIn: http://www.linkedin.com/in/claudiodesio E Internet: http://www.claudiodesio.com E YouTube: http://www.youtube.com/claudiodesiocesari E Instagram: http://www.instagram.com/cdesio Happy working! Claudio De Sio Cesari XI Java for Aliens - Claudio De Sio Cesari (www.javaforaliens.com) Chapter 1 Exercises Introduction to Java The following exercises are designed for those starting from scratch. These exercises have the goal of giving a minimum of confidence with the Java programming environment to the reader. Let’s remember once again that, especially for those who are beginners, it is important to start writing all the code by hand, without copy-paste or special help from the development tool chosen. It is also very important to use comments for every line of code written. This will allow us to learn better the definitions, and to be more aware when writing code. So, it will be fundamental to write the source code on a text editor such as Windows Notepad (as described in the first chapter) and compile using the command line (see Appendix C). We do not recommend performing the exercises of these very first chapters (let’s say the first 4) using a complex IDE like Eclipse or Netbeans... we could end up studying the IDE rather than Java. It is instead advisable (if you do not want to have too much to do with Notepad and the com- mand line) to use EJE (https://sourceforge.net/projects/eje), which offers simple features designed for helping the beginners to start to code. For example, it allows us to compile and execute our files by pressing two distinct buttons. EJE must be downloaded at https://sourceforge.net/projects/eje. It is easy to install ( just unzip the file in any folder) and use. To run it, double- click on the eje.bat file. However, you can find more information in Appendix M. 1 Java for Aliens - Claudio De Sio Cesari (www.javaforaliens.com) Chapter 1 Exercises From Chapter 5 onwards, it will be easier to switch to an IDE, since other development envi- ronments will be explained. Finally we suggest, after having done an exercise, to consult the relative solution before moving on to the next one (often an exercise is a prerequisite for the next one).