Introduction to Computer Science with Java Programming
Total Page:16
File Type:pdf, Size:1020Kb
Rowan University Rowan Digital Works Open Educational Resources University Libraries 5-1-2017 Introduction to Computer Science with Java Programming Seth D. Bergmann Rowan University Follow this and additional works at: https://rdw.rowan.edu/oer Part of the Computer Sciences Commons DOI: 10.31986/issn.2689-0690_rdw.oer.1000 Let us know how access to this document benefits ouy - share your thoughts on our feedback form. Recommended Citation Bergmann, Seth D., "Introduction to Computer Science with Java Programming" (2017). Open Educational Resources. 2. https://rdw.rowan.edu/oer/2 This Book is brought to you for free and open access by the University Libraries at Rowan Digital Works. It has been accepted for inclusion in Open Educational Resources by an authorized administrator of Rowan Digital Works. Introduction to Computer Science with Java Programming Seth D. Bergmann May 1, 2019 2 Preface This book is intended to be used for a first course in computer programming. No prior experience with programming should be necessary in order to use this book. But this book is intended to be used with a course that teaches more than computer programming; it is intended to be used with a course that teaches Computer Science. The distinction is subtle, but important. The author(s) believe that a breadth-first approach is the best way to intro- duce the concepts of Computer Science to students. Rather than isolate topics in courses (bits and bytes in a computer organization course; formal grammars and languages in a theory course; lists, sets, and maps in a data structurs course; etc) we believe that topics should be introduced in a brief and simple manner at the starting level. Elaboration on these topics should occur in subsequent courses. This breadth-first approach allows the student to build on existing knowledge and retain a greater proportion of the material. Our colleagues in the physical sciences have done this for over a century: Physics I, Physics II, Physics III; Chemistry I, Chemistry II, Chemistry III. Some examples of this breadth-first approach to Computer Science: We teach the rudiments of binary numbers. Is this necessary to learn • to program the solution to a simple problem? Probably not, but it will become necessary at some later time, when studying hardware, or the limitations of software. Every introductory programming book teaches the concept of an arith- • metic expression. We give a formal definition, and show the structure of an expression by placing boxes around sub-expressions. The student thinks we are teaching how to write a correct expression; we are actually teaching recursion, formal grammars, and derivation trees. We advise the student, ”Program to an interface whenever possible”. The • student thinks we are teaching the correct usage of Java interfaces, but we are actually teaching object-oriented design and software engineering. The student thinks we are teaching programming, but we are actually teaching Computer Science. Knowledge is not separated into compartments, and our curriculum should not attempt to do so. i ii PREFACE Several topics were recently addes to this book, in order that it be used as the primary textbook for the Educational Testing Service’s Advanced Placement course in Computer Science (CSA): More extensive discussion of arrays • sorting • binary search • The AP course includes a Marine Biology simulation case study which is not included in this version of the book.1 The reader will notice an empty section for this case study in several chapters of this book. The author invites current instructors to participate in this project by providing these sections; those who do so would be listed as a secondary author or contributor on the title page and/or the preface, depending on the level of contribution. Those who are in- terested in contributing should contact the primary author at the email address shown below. This book is an open source book. That means that not only is the pdf version available (to potential students and teachers) for free download, but that the original (LaTeX) source files are also available (to potential authors and contributors). Based on the model of open source software, open source for textbooks is a relatively new paradigm in which many authors and contributors can cooperate to produce a high quality product, for no compensation. For details on the rationale of this new paradigm, and citations for other open source textbooks, see the journal Publishing Research Quarterly, Vol. 30, No. 1, March 2014. The source materials and pdf files of this book are licensed with the Creative Commons NonCommercial license, which means that they may be freely used, copied, or modified, but not for financial gain. This book is available in pdf at rdw.rowan.edu /oer. The source files are available at cs.rowan.edu/ bergmann/books. The author may be reached at [email protected]∼ Secondary Authors Contributors 1The Marine Biology case study is no longer included on the AP exam. Thus, students using this book are not penalized on the exam for not having seen the case study. Contents Preface i 0 ComputersandComputerPrograms 1 0.1 TheCPUandmachinelanguage . 1 0.2 High level languages and compilers . 2 0.3 Datarepresentation: bitsandbytes. 3 0.3.1 Wholenumbers........................ 3 0.3.2 Othernumbers ........................ 3 0.3.3 Characters .......................... 3 0.3.4 Images............................. 4 0.3.5 Sound............................. 4 0.3.6 Exercises ........................... 4 1 Java classes,objects, object diagramsand methods 7 1.1 Classesandobjects.......................... 7 1.1.1 Exercises ........................... 8 1.2 Variablesandreferences . .. .. .. .. .. .. .. .. .. 8 1.2.1 Exercises ........................... 9 1.3 DefiningaJavaclass......................... 9 1.3.1 Exercises ........................... 10 1.4 Objectdiagrams ........................... 11 1.5 Methods................................ 11 1.5.1 Exercises ........................... 13 1.6 Constructorsandobjectcreation . 14 1.6.1 Exercises ........................... 15 1.7 Gettingstarted: IDEorcommandline . 16 1.7.1 UsinganIDE......................... 16 1.7.2 TheBlueJIDE........................ 17 1.7.3 Exercises ........................... 20 1.8 Constructors and objects in the GridWorld case study . 20 1.9 Projects................................ 20 iii iv CONTENTS 2 ProgramElementsandMethods(revisited) 22 2.1 Datatypes .............................. 22 2.1.1 Wholenumbers:int ..................... 23 2.1.2 Othernumbers: floatanddouble . 23 2.1.3 Logical values: boolean . 24 2.1.4 Charactervalues:char. 25 2.1.5 Stringsofcharacters:String . 25 2.1.6 Otherreferencetypes . 26 2.1.7 Exercises ........................... 27 2.2 Operationsandexpressions . 28 2.2.1 Arithmetic operations . 28 2.2.2 Stringoperations....................... 28 2.2.3 Arithmetic Expressions . 30 2.2.4 Exercises ........................... 33 2.3 Declaration and initialization of variables . 34 2.3.1 Declaration of variables . 35 2.3.2 Iniitialization of variables . 35 2.3.3 Exercises ........................... 35 2.4 Assignmentofvaluestovariables . 35 2.4.1 Type conversion in assignments . 37 2.4.2 Type conversions and initializations . 37 2.4.3 Assignmentofreferences. 38 2.4.4 Exercises ........................... 39 2.5 Method definitions, signatures, and invocation . 40 2.5.1 Methoddefinition ...................... 40 2.5.2 Methodsignatureandbody . 41 2.5.3 Methodinvocation ...................... 42 2.5.4 MethodsFromtheJavaClassLibrary . 44 2.5.5 Exercises ........................... 44 2.6 Recursivemethods ......................... 46 2.6.1 Exercises ........................... 46 2.7 Printingtheoutput.......................... 46 2.7.1 Exercises ........................... 47 2.8 Constantsandclassvariables . 47 2.8.1 Constants........................... 47 2.8.2 Class variables . 48 2.8.3 Classconstants........................ 48 2.8.4 Exercises ........................... 49 2.9 Commentsandreadability. 49 2.9.1 Formattingaprogram . 49 2.9.2 Comments........................... 50 2.9.3 Exercises ........................... 50 2.10 Program elements and methods in the GridWorld case study . 51 CONTENTS v 3 Selection Structures 52 3.1 Comparisonoperators .. .. .. .. .. .. .. .. .. .. 52 3.1.1 Exercises ........................... 53 3.2 Booleanoperators .......................... 53 3.2.1 AND,OR,NOT ....................... 53 3.2.2 Short circuit evaluation . 56 3.2.3 DeMorgan’sLaws ...................... 56 3.2.4 Exercises ........................... 57 3.3 One-wayselections .......................... 58 3.3.1 Exercises ........................... 59 3.4 Two-wayselections ......................... 60 3.4.1 Exercises ........................... 64 3.5 Compoundstatementsandscope . 66 3.5.1 Compoundstatements . 66 3.5.2 Scopeofvariables ...................... 67 3.5.3 Java statements - revisiting a formal definition . 68 3.5.4 Exercises ........................... 68 3.6 Recursivemethodsrevisited . 70 3.6.1 Exercises ........................... 71 3.7 ComparingStringsandotherreferencetypes . 73 3.7.1 Comparison for equality or inequality . 73 3.7.2 Orderedcomparisons. 73 3.7.3 Exercises ........................... 75 3.8 Selection structures in the GridWorld case study . 76 3.9 Projects................................ 76 4 Iteration Structures 80 4.1 Looping with while–pre-testloops.. .. .. .. .. .. .. 80 4.1.1 Infiniteloops ......................... 83 4.1.2 Exercises ........................... 83 4.2 Looping