Lecture notes on Java Programming

lecture notes on advanced java programming pdf, advanced java lecture notes pdf, lecture notes on core java complete reference, lecture notes for advanced java programming
Dr.DavisHardy Profile Pic
Dr.DavisHardy,Germany,Teacher
Published Date:22-07-2017
Your Website URL(Optional)
Comment
TM Java Programming for Kids, Parents and GrandParents Yakov Fain Java Programming for Kids, Parents and Grandparents iii Java Programming for Kids, Parents and Grandparents by Yakov Fain Copyright © 2004 Smart Data Processing, Inc. 14 Molly Pitcher Dr. Manalapan, New Jersey, 07726, USA All rights reserved. No part of this book may be reproduced, in any form or by any, without permission in writing from the publisher. Cover design and illustrations: Yuri Fain Adult technical editor: Yuri Goncharov Kid technical editor: David Fain May 2004: First Electronic Edition The information in this book is distributed without warranty. Neither the author nor the publisher shall have any liability to any person or entitle to any liability, loss or damage to be caused directly or indirectly by instructions contained in this book or by the computer software or hardware products described herein. Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Windows 98, Windows NT, Windows 2000 and Windows XP are trademarks of Microsoft Corporation. All other product names and company names are the property of their respective owners. The publisher offers discount on this book when ordered in bulk quantities. For more information, send an e-mail at bookssmartdataprocessing.com. ISBN: 0-9718439-5-3 Table of Contents PREFACE...............................................................................................IX ACKNOWLEDGEMENTS.............................................................................XI CHAPTER 1. YOUR FIRST JAVA PROGRAM....................................................1 How to Install Java on Your Computer ....................................................................................... 2 Three Main Steps in Programming .............................................................................................. 6 Step 1 – Type the Program ........................................................................................................... 6 Step 2 – Compile the Program...................................................................................................... 8 Step 3 – Run the Program............................................................................................................. 9 Additional Reading ...................................................................................................................... 10 CHAPTER 2. MOVING TO ECLIPSE..............................................................11 Installing Eclipse .......................................................................................................................... 11 Getting Started with Eclipse........................................................................................................ 13 Creating Programs in Eclipse ..................................................................................................... 15 Running HelloWorld in Eclipse............................................................................................... 16 How HelloWorld Works? ........................................................................................................ 17 Additional Reading ...................................................................................................................... 20 Practice.......................................................................................................................................... 20 Practice for Smarty Pants............................................................................................................ 21 CHAPTER 3. PET AND FISH – JAVA CLASSES..............................................22 Classes and Objects...................................................................................................................... 22 Data Types .................................................................................................................................... 25 Creation of a Pet.......................................................................................................................... 28 Inheritance – a Fish is Also a Pet ................................................................................................ 33 Method Overriding ...................................................................................................................... 37 Additional Reading ...................................................................................................................... 38 Practice.......................................................................................................................................... 38 Practice for Smarty Pants............................................................................................................ 39 Java Programming for Kids, Parents and Grandparents v CHAPTER 4. JAVA BUILDING BLOCKS.......................................................40 Program Comments ..................................................................................................................... 40 Making Decisions with if Statements........................................................................................ 41 Logical Operators......................................................................................................................... 43 The logical not here is applied to the expression in parentheses. ............................................. 44 Conditional operator.................................................................................................................... 44 Using else if ............................................................................................................................ 44 Making Decisions With switch Statement.............................................................................. 45 How Long Variables Live? .......................................................................................................... 46 Special Methods: Constructors ...................................................................................................47 The Keyword this...................................................................................................................... 48 Arrays............................................................................................................................................ 49 Repeating Actions with Loops..................................................................................................... 51 Additional Reading ...................................................................................................................... 54 Practice.......................................................................................................................................... 54 Practice for Smarty Pants............................................................................................................ 54 CHAPTER 5. A GRAPHICAL CALCULATOR...................................................55 AWT and Swing ........................................................................................................................... 55 Packages and Import Statements................................................................................................ 55 Major Swing Elements................................................................................................................. 56 Layout Managers.......................................................................................................................... 59 Flow Layout ............................................................................................................................... 59 Grid Layout ................................................................................................................................ 60 Border Layout ............................................................................................................................ 62 Combining Layout Managers..................................................................................................... 62 Box Layout................................................................................................................................. 65 Grid Bag Layout......................................................................................................................... 66 Card Layout................................................................................................................................ 68 Can I Create Windows Without Using Layouts? ....................................................................... 68 Window Components................................................................................................................... 68 Additional Reading ...................................................................................................................... 72 Practice.......................................................................................................................................... 72 Practice for Smarty Pants............................................................................................................ 73 CHAPTER 6. WINDOW EVENTS.................................................................74 Interfaces....................................................................................................................................... 75 Action Listener ............................................................................................................................. 77 Registering Components with ActionListeneter ............................................................. 78 What’s the Source of an Event? ................................................................................................. 79 How to Pass Data Between Classes ............................................................................................. 81 Finishing Calculator..................................................................................................................... 83 Some Other Event Listeners....................................................................................................... 89 How to Use Adapters.................................................................................................................... 90 Additional Reading ...................................................................................................................... 91 Practice.......................................................................................................................................... 91 Practice for Smarty Pants............................................................................................................ 91 CHAPTER 7. THE TIC-TAC-TOE APPLET.....................................................92 Learning HTML in 15 Minutes................................................................................................... 93 Writing Applets Using AWT .......................................................................................................96 How to Write AWT Applets ........................................................................................................97 Writing a Tic-Tac-Toe Game ......................................................................................................99 The Strategy ............................................................................................................................... 99 The Code .................................................................................................................................. 100 Additional Reading .................................................................................................................... 110 Practice........................................................................................................................................ 110 Practice for Smarty Pants.......................................................................................................... 111 CHAPTER 8. PROGRAM ERRORS - EXCEPTIONS........................................112 Reading the Stack Trace............................................................................................................ 113 Genealogical Tree of Exceptions ............................................................................................... 114 The keyword throws................................................................................................................ 117 The Keyword finally .......................................................................................................... 118 The Keyword throw ............................................................................................................... 119 Creating New Exceptions........................................................................................................... 121 Additional Reading .................................................................................................................... 123 Practice........................................................................................................................................ 123 Java Programming for Kids, Parents and Grandparents vii Practice for Smarty Pants.......................................................................................................... 123 CHAPTER 9. SAVING THE GAME SCORE...................................................124 Byte Streams ............................................................................................................................... 124 Buffered Streams........................................................................................................................ 127 Command-Line Arguments....................................................................................................... 129 Reading Text Files...................................................................................................................... 132 Class File .................................................................................................................................. 135 Additional Reading .................................................................................................................... 137 Practice........................................................................................................................................ 137 Practice for Smarty Pants.......................................................................................................... 138 CHAPTER 10. MORE JAVA BUILDING BLOCKS.........................................139 Working with Date and Time Values ....................................................................................... 139 Method Overloading .................................................................................................................. 140 Reading Keyboard Input ........................................................................................................... 143 More on Java Packages.............................................................................................................. 145 Access Levels............................................................................................................................... 148 Getting Back to Arrays ............................................................................................................. 151 Class ArrayList ...................................................................................................................... 154 Additional Reading .................................................................................................................... 158 Practice........................................................................................................................................ 158 Practice for Smarty Pants.......................................................................................................... 159 CHAPTER 11. BACK TO GRAPHICS – THE PING PONG GAME.....................160 The Strategy................................................................................................................................ 160 The Code ..................................................................................................................................... 161 Java Threads Basics ................................................................................................................... 169 Finishing Ping Pong Game ........................................................................................................175 What to Read Next on Game Programming ............................................................................ 185 Additional Reading .................................................................................................................... 186 Practice........................................................................................................................................ 186 Practice for Smarty Pants.......................................................................................................... 186 APPENDIX A. JAVA ARCHIVES - JARS......................................................188 Additional Reading .................................................................................................................... 189 APPENDIX B. ECLIPSE TIPS....................................................................190 Eclipse Debugger........................................................................................................................ 191 APPENDIX C. HOW TO PUBLISH A WEB PAGE.........................................194 Additional Reading .................................................................................................................... 197 Practice........................................................................................................................................ 197 INDEX....................................................................................................198 Java Programming for Kids, Parents and Grandparents ix Preface One day my son Davey-steamboat showed up in my office with my rated “R” Java tutorial in his hands. He asked me to teach him programming so he could create computer games. At that time I’ve already written a couple of books on Java and taught multiple classes about computer programming, but all of this was for grownups A search on Amazon could not offer anything but books for dummies, but Davey is not a dummy After spending hours on Google I found either some poor attempts to create Java courses for kids, or some reader-rabbit-style books. Guess what? I decided to write one. To help me understand the mentality of the little people, I decided to ask Davey to become my first kid student. This book will be useful for the following groups of people • Kids from 11 to 18 years old • School computer teachers • Parents who want to teach their kids programming • Complete beginners in programming (your age does not matter) Even though I use a simple language while explaining programming, I promise to treat my readers with respect - I’m not going to write something like “Dear friend You are about to begin a new and exciting journey…”. Yeah, right Just get to the point First chapters of the book will end with simple game-like programs with detailed instructions on how to make them work. Also we are going to create a calculator that looks and works similarly to the one that you have in your computer. In the second part of the book we’ll create together game programs Tic-Tac-Toe and Ping- Pong. You’ll need to get used to the slang of professional programmers, and all important words will be printed in this font. Java language elements and programs will be shown in a different font, for example String. This book does not cover each and every element of the Java language, otherwise it would be too fat and boring. But at the end of each chapter there is a section Additional Reading wit links to Web sites with more detailed explanations of the subject. You’ll also find assignments at the end of each chapter. Every reader has to complete assignments given in the section Practice. If these assignments are too easy for you, I challenge you to do assignments from the section Practice for Smarty Pants. Actually, if you are reading this book, you are a smart person and should try to complete all the assignments. To get the most out of this book, read it from the beginning to the end. Do not move on until you understand the chapter you are reading now. Teenagers, parents and grandparents should be able to master this book without asking for help, but younger kids should read this book with an adult. Java Programming for Kids, Parents and Grandparents xi Acknowledgements Thank you all architects and developers who worked for free on Eclipse – one of the best available Integrated Development Environment for Java. Special thanks to New Jersey Transit bus drivers for the smooth ride – a half of this book has been written while commuting to work on the bus 139. Thanks to a lovely lady and my wife Natasha for successfully running a business called family. Special thanks to Yuri Goncharov - an expert Java programmer from Toronto, Canada. He reviewed the book, tested every code example, and provided a valuable feedback to make this book a little better. Chapter 1. Your First Java Program People talk to each other using different languages. Similarly, they write computer programs like games, calculators, text editors using different programming languages. Without programs, your computer would be useless, and its screen would be always black. Computer parts are called hardware, and programs are known as software. The most popular computer languages are Visual Basic, C++, and Java. What makes the Java language different from many others? First of all, the same Java program can run (work) on different computers like PC, Apple and others without changes. As a matter of fact, Java programs do not even know where they run, because they run inside of a special software shell called Java Virtual Machine (JVM). If, for example, your Java program needs to print some messages, it asks JVM to do this, and JVM know how to deal with your printer. Second, Java makes it easy to translate your programs (screens, menus and messages) to different human languages. Third, Java allows you to create program elements (classes) that represent objects from the real world. For example, you can create a Java class called Car and set attributes of this class like doors, wheels, similarly to what the real cars have. After that, based on this class you can create another class, for example Ford, which will have all the features of the class Car plus something that only Fords have. Fourth, Java is more powerful than many other languages. Fifth, Java is free You can find everything for creating your Java programs on the Internet without paying a penny How to Install Java on Your Computer To start programming in Java you need to download a special software from the Web site of the company called Sun Microsystems, that created this language. The full name of this software is Java 2 Software Development Kit (J2SDK). At the time of this writing its latest version 1.5.0 could be downloaded from this Web site: http://java.sun.com/j2se Select release J2SE 1.5.0 or the newer one, and on the next Web page under the title Downloads click on the link to this release. Then click on the word Download under the title SDK. Accept the license agreement and select Windows Offline Installation (unless you have a Mac, Linux or Solaris computer). Press the button Save on the next screen and select the folder on your hard disk where you’d like to save the Java installation file. The file download will start. After the download ends, start the installation process – just double-click on the file that you’ve downloaded, and this will install J2SDK on your disk. For example, on Windows computer it will create a folder like this one: c:\Program Files\java\j2sdk1.5.0, where c: is the name of your hard disk. Java Programming for Kids, Parents and Grandparents 3 If you do not have enough room on your c: drive, select a different one, otherwise, just keep pressing the buttons Next, Install and Finish on the windows that will be popping up on your screen. In several minutes the installation of Java on your computer will be complete. In the next step of installation, you need to define two system variables. For example, in Windows click on the button Start, and get to the Control Panel (it might be hidden behind the menu Settings), and click on the icon System. Select there a tab Advanced, and click on the button Environment Variables. On the next page you can see how this screen looks like on my Windows XP notebook. The next window will show all system variables that already exist in your system. Java Programming for Kids, Parents and Grandparents 5 Press the lower button New and declare the variable Path that will help Windows (or Unix) find J2SDK on your machine. Double check the name of the folder where you’ve installed Java. If the variable Path already exists, just add the new Java directory and a semicolon to the very beginning of the box Variable Value: Also, declare the variable CLASSPATH by entering a period and a semicolon as its value. This system variable will help Java find your programs. The period means that Java has to start looking for your programs from the current disk folder, and the semicolon is just a separator: Now the installation of J2SDK is complete If you have an old Windows 98 computer, you’ll need to set the PATH and CLASSPATH variable in a different way. Find the file autoexec.bat on your c: drive, and using Notepad or other text editor enter the proper values for these variable at end of this file, for example: SET CLASSPATH=.; SET PATH=c:\j2sdk1.5.0\bin;%PATH% After making this change you’ll need to restart your computer. Three Main Steps in Programming To create a working Java program you need to go through the following tree steps: Write the program in Java and save it on a disk. Compile the program to translate it from Java language into a special byte code that JVM understands. Run the program. Step 1 – Type the Program You can use any text editor to write Java programs, for example Notepad. Java Programming for Kids, Parents and Grandparents 7 First, you’ll need to type the program and save it in a text file with a name ending in .java. For example, if you want to write a program called HelloWorld, enter its text (we call it source code) in Notepad and save it in the file named HelloWorld.java. Please do not use blanks in Java file names. Here is the program that prints on the screen the words Hello World: public class HelloWorld public static void main(String args) System.out.println("Hello World"); I’ll explain how this program works a little later in this chapter, but at this point just trust me – this program will print the words Hello World in the step 3. Step 2 – Compile the Program Now you need to compile this program. You’ll be using the javac compiler, which is a part of J2SDK. Let’s say you’ve saved your program in the directory called c:\practice. Select the menus Start, Run, and enter the word cmd to open a black command window. Just to make sure that you’ve set the system variables PATH and CLASSPATH correctly, enter the word set and take another look at their values. Change the current folder to c:\practice and compile the program: cd \practice javac HelloWorld.java You do not have to name the folder practice – give it any name you like. In Windows 98 select the “MS DOS Prompt” from the Start menu to open a command prompt window. The program javac is Java compiler. You won’t see any confirmation that your program HelloWorld has been compiled successfully. This is the case when no news is good news. Type a command dir and it’ll show you all the files that exist in your folder. You should see there a new file named HelloWorld.class. This proves that your program has been successfully compiled. Your original file HelloWorld.java is also there, and you can modify this file later to print Hello Mom or something else.

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.