Java with BlueJ Part I Ron McFadyen September 9, 2015 2 (cid:13)c 2015 Ron McFadyen Department of Applied Computer Science University of Winnipeg 515 Portage Avenue Winnipeg, Manitoba, Canada R3B 2E9 [email protected] [email protected] This work is licensed under Creative Commons Attribution NonCommercial ShareAlike 4.0 International Public License. To view a copy of this license visit http://creativecommons.org/licenses/by-nc-sa/4.0/ This work can be distributed in unmodified form for non-commercial pur- poses. Modified versions can be made and distributed for non-commercial purposes provided they are distributed under the same license as the origi- nal. Other uses require permission of the author. The website for this book is www.acs.uwinnipeg.ca/rmcfadyen/CreativeCommons/ To Callum 3 4 Contents 1 Introduction 9 1.1 Java, the beginning . . . . . . . . . . . . . . . . . . . . . . . . 9 1.2 The Java Compiler and the Java Virtual Machine . . . . . . . 11 1.3 BlueJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.4 A First Program . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.5 Using BlueJ to Run HelloWorld . . . . . . . . . . . . . . . . 14 2 Basics 19 2.1 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.3 Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . 23 2.3.1 Numeric Data Types: byte, short, int, long . . . . 23 2.3.2 Numeric Data Types: float, double . . . . . . . . . . 26 2.3.3 Numeric Expressions . . . . . . . . . . . . . . . . . . . 29 2.3.4 boolean Data Type . . . . . . . . . . . . . . . . . . . 36 2.3.5 char Data Type . . . . . . . . . . . . . . . . . . . . . 40 2.4 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.5 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.6 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.6.1 System.out . . . . . . . . . . . . . . . . . . . . . . . . 54 2.6.2 Redirecting System.out . . . . . . . . . . . . . . . . . 58 2.6.3 JOptionPane . . . . . . . . . . . . . . . . . . . . . . . 60 2.7 Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 2.7.1 The Scanner Class . . . . . . . . . . . . . . . . . . . . 61 2.7.2 The JOptionPane Class . . . . . . . . . . . . . . . . . 65 3 Control Structures 67 3.1 Compound statements . . . . . . . . . . . . . . . . . . . . . . 67 3.2 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 5 6 CONTENTS 3.3 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 3.4 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 3.5 do ...while . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 3.6 switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 4 Classes in the Java Class Libraries 115 4.1 Random . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 4.2 Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 4.3 Scanner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 4.4 Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 4.5 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 5 ArrayLists 141 6 One-Dimensional Arrays 149 6.1 Initializing arrays . . . . . . . . . . . . . . . . . . . . . . . . . 151 6.2 Storage of arrays and copying arrays . . . . . . . . . . . . . . 152 6.3 The enhanced for . . . . . . . . . . . . . . . . . . . . . . . . . 153 6.4 Passing string values into main() . . . . . . . . . . . . . . . . 155 6.5 Parallel arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 156 6.6 Partially filled arrays . . . . . . . . . . . . . . . . . . . . . . . 158 6.7 Array utilities in Java class libraries . . . . . . . . . . . . . . 161 7 Designing Java Classes 165 7.1 Using Multiple Classes . . . . . . . . . . . . . . . . . . . . . . 167 7.2 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 7.3 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 7.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 7.5 Visibility Specifications: Public, Private . . . . . . . . . . . . 180 7.6 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 7.7 Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 7.8 Reusing code . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 7.9 Parameter lists and arguments . . . . . . . . . . . . . . . . . 190 7.10 Varargs: a variable number of arguments. . . . . . . . . . . . 193 7.11 Code listings: Student, Subject . . . . . . . . . . . . . . . . . 195 8 A Brief Introduction to Graphical User Interfaces 203 8.1 Brief Introduction to Simple GUI Builder . . . . . . . . . . . 205 8.1.1 Listings . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Preface This book is Part I of a two-part set that introduces the Java programming language. ThetextassumesthestudentwillbeusingtheBlueJdevelopment environment and provides some introductory BlueJ material. Our experi- ence has been that BlueJ is easy to learn and provides a good programming environment for the beginner programmer. The material in chapters 1 through 5, and 7 are required topics. • Chapter 1: This is a high-level introduction to Java. The typical HelloWorld program is discussed along with how to run HelloWorld in BlueJ. • Chapter2: Basicconceptshavingtodowithconstants, variables, data types, expressions and input/output are covered. • Chapter 3: This chapter covers the major control structures a pro- grammer uses. • Chapter 4: Java provides a great deal of functionality in its class libraries. In this chapter we introduce several of these classes such as Random ...Random gives the programmer the ability to simulate throwing dice or tossing coins. As well, useful functionality in utility classes such as Math, Integer, and Character are covered. • Chapter 5: Many applications require a program to work with col- lections of data. For example, the set of courses at a university is a collection. Java programs must be able to manage such a set and the ArrayList data structure is well-suited to the task. • Chapter 7: The program code in a Java system is managed in struc- tures where the basic component is the class. A Java class contains data and executable code. This chapter covers concepts that must be understood if one is to design and implement a Java-based system. 7 8 CONTENTS Chapters 6 and 8 are considered optional and are covered as time permits. Chapter6coversone-dimensionalArrays...arraysprovidesomeofthecapa- bility of the ArrayList, but programming arrays is much more difficult than programming ArrayLists. Chapter 8 introduces concepts on Graphical User Interfaces (GUIs) as provided for in a BlueJ extension. GUIs are required if one is going to create interactive programs, but there are many concepts to master and the topic is typically covered in great detail in advanced courses. The examples in the text, and solutions to many exercises, are available on the website for this text. Chapter 1 Introduction This book is about programming in Java. We begin with short descriptions of Java and BlueJ. We feel that BlueJ is one of the simplest development environments for the beginning programmer to use. All of the examples in this text have been tested using BlueJ. Sample solutions for most exercises are available on the website for this text. 1.1 Java, the beginning James Gosling is referred to as the father of the Java programming lan- guage. He graduated with a BSc (1977) from the University of Calgary and a PhD (1983) from Carnegie Mellon University. Later, in 1994 at Sun Microsystems he created the Java language while leading a team that was purposed with developing a handheld home-entertainment controller tar- geted at the digital cable television industry. That project did not produce the expected outcome, but in 1995, the team announced that the Netscape Navigator Internet browser would incorporate Java technology, and from there its adoption for implementing systems began. James Gosling has received several awards, including: • 2007 - appointed an Officer of the Order of Canada.[1] • 2013 - named an Association of Computing Machinery Fellow for ”Java, NeWS, Emacs, NetBeans, and other contributions to program- ming languages, tools, and environments”.[2] • 2015 - awarded the IEEE John von Neumann Medal for ”the Java programminglanguage,JavaVirtualMachine,andothercontributions to programming languages and environments”.[3] 9 10 CHAPTER 1. INTRODUCTION In2010OracleacquiredSunMicrosystemsandtookoverthedevelopmentof the language. The language has gone through a number of updates, and at the time of writing the current release is referred to as Java 8. All programs in this text have been tested on Java 8. This text is about programming Java applications. The student may be interested Java applets (these run in a web browser) which are discussed in a future appendix.
Description: