Java with BlueJ - University of Winnipeg

Java with BlueJ

Ron McFadyen Jeanette Bautista September 3, 2019

?2019 Ron McFadyen, Jeanette Bautista Department of Applied Computer Science University of Winnipeg 515 Portage Avenue Winnipeg, Manitoba, Canada R3B 2E9

je.bautista@uwinnipeg.ca r.mcfadyen@uwinnipeg.ca

This work is licensed under Creative Commons Attribution NonCommercial ShareAlike 4.0 International Public License. To view a copy of this license visit



This work can be distributed in unmodified form for non-commercial purposes. Modified versions can be made and distributed for non-commercial purposes provided they are distributed under the same license as the original. Other uses require permission of the author. The website for this book is

acs.uwinnipeg.ca/rmcfadyen/CreativeCommons/

2

To Callum To Linden & Bryn

3

Contents

1. Introduction............................................................................................................................................ 9 1.1 Java, the beginning ..................................................................................................................... 9 1.2 The Java Compiler and the Java Virtual Machine ........................................................10 1.3 BlueJ ...............................................................................................................................................11 1.4 A First Program .........................................................................................................................12 1.5 Using BlueJ to Run HelloWorld ............................................................................................13

2 Basics.......................................................................................................................................................17 2.1 Literals ..........................................................................................................................................17 2.2 Variables.......................................................................................................................................17 2.3 Primitive Data Types...............................................................................................................22 2.3.1 Numeric Data Types: byte, short, int, long .............................................................22 2.3.2 Integer Arithmetic ..........................................................................................................22 2.3.3 Numeric Data Types: float, double ...........................................................................26 2.3.4 Numeric Expressions.....................................................................................................29 2.3.5 boolean Data Type ..........................................................................................................36 2.3.6 char Data Type..................................................................................................................41 2.4 Conditional Operator ?:........................................................................................................44 2.5 Operator Priorities...................................................................................................................45 2.6 The String Class..........................................................................................................................48 2.7 Implied declarations using var ...........................................................................................58 2.8 Output............................................................................................................................................59 2.8.1 System.out..........................................................................................................................59 2.8.2 JOptionPane.......................................................................................................................63 2.9 Input...............................................................................................................................................65 2.9.1 The Scanner Class ...........................................................................................................65 2.9.2 The JOptionPane Class ..................................................................................................68

3 Control Structures..............................................................................................................................71 3.1 Compound Statements ...........................................................................................................71 3.2 if .......................................................................................................................................................72 4

3.3 while ...............................................................................................................................................85 3.4 for ....................................................................................................................................................93 3.5 switch ......................................................................................................................................... 107 3.6 do ...while ................................................................................................................................... 112 4 Introduction to Methods .............................................................................................................. 117 4.1 Void Methods........................................................................................................................... 118 4.2 Value-returning Methods ................................................................................................... 120 4.3 Parameters ............................................................................................................................... 122 4.4 Summary ................................................................................................................................... 126 5 Classes in the Java Class Libraries ............................................................................................ 131 5.1 Random...................................................................................................................................... 131 5.2 Character................................................................................................................................... 135 5.3 Scanner ...................................................................................................................................... 143 5.4 Math ............................................................................................................................................ 150 5.5 Integer ........................................................................................................................................ 154 6 ArrayLists ........................................................................................................................................... 159 6.1 Examples ................................................................................................................................... 161 6.2 Sieve of Eratosthenes........................................................................................................... 166 6.3 Parallel Lists ............................................................................................................................ 169 7 Designing Java Classes................................................................................................................... 173 7.1 Using Multiple Classes ......................................................................................................... 175 7.2 Fields........................................................................................................................................... 176 7.3 Methods ..................................................................................................................................... 180 7.4 Constructors ............................................................................................................................ 185 7.5 Visibility Specifications: Public, Private ....................................................................... 189 7.6 Constructors ............................................................................................................................ 190 7.7 Overloading.............................................................................................................................. 191 7.8 Associations ............................................................................................................................. 192 7.9 Reusing code............................................................................................................................ 196 7.10 Parameter lists and arguments........................................................................................ 198

7.10.1 Varargs: a variable number of arguments ......................................................... 201 5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download