Copyright
Copyright 2013-2015 Annette Godtland
SECOND EDITION
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Godtland Software Corporation was aware of a trademark claim, the designations have been printed in initial caps or all caps.
No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein.
You can use and redistribute example code from this book only for non-commercial purpose and as long as you acknowledge their source and authorship. The source of the code should be noted in any documentation as well as in the program code itself (as a comment). The attribution should include author: Annette Godtland, title: Do-It-Yourself Java Games, and publisher: Godtland Software Corporation.
Last updated 10/29/2015
Published by:
Godtland Software Corporation, Publishing Division
4605 Alan Lane NW
Rochester, MN 55901
Do-It-Yourself Java Games
An Introduction to Java Computer Programming
2nd Edition
by Annette Godtland
Godtland Software Corporation, Publishing Division
Rochester, Minnesota
TABLE OF CONTENTS
Introduction
Do you like playing computer games? Do you enjoy solving puzzles? If so, you may enjoy computer programming.
Are you a hands-on kind of person? Would you rather start on a project right away instead of first reading long chapters about how things work? Do you lose interest if too much technical information is presented at a time? If so, this may be the right computer programming book for you.
This book teaches Java programming as you create projects (computer games) in small, manageable steps. You will create games like: Choose an Adventure, Secret Code, Hangman, Crazy Eights, and many more.
The lessons in each project are presented using a try-it approach: do the steps shown and see what happens. Youll be given programming tasks, each followed by a program . The listings have fill-in blanks to allow you to write much of the program code yourself. But don't worry if you are unsure of how to fill in the blanks; simply click the listing's link to see the missing code.
Don't skip any lessons-each lesson explains a new programming technique, most of which are used again in later lessons. Doing all the lessons will make later lessons easier.
has definitions of Java programming terms and links to more information. The first use of each term, like listing and code used above, will be a clickable link to the word in the glossary.
Several different approaches can often accomplish the same programming task. You don't have to use the code shown in the answer if your code does what was asked or if you like what your code does better. Feel free to experiment with the code and see what happens. Half the fun of programming is making programs do whatever you want.
This book is designed for e-readers. Click the image below or follow this link to watch a video showing the features in this electronic book that would not be possible in a print book.
Click the image below or follow this link to watch a video demonstration of the Word Mastermind, Hangman, and Crazy Eights programs you will create when you do the lessons in this book.
Do-It-Yourself Java Games was updated to a second edition to work in Windows 10 and Java 8.
Setup
The first step in Java programming is to install and set up a couple needed programs. If you want more detailed setup instructions, see . Once this short setup is complete, you'll write your first Java program.
Create a Java Work Folder
First, create a folder on your computer for all your Java work. I called mine java and put it in my user folder, c:\Users\Annette. Therefore, my Java is c:\Users\Annette\java.
If more than one person will create Java programs on your computer, you should create a different work folder for each of you. This book puts every action you are to take in a numbered list, like this:
- Create a Java work folder on your computer.
See if you want more detailed instructions for how to create a work folder.
Download and Install Java
The free Java Development Kit is required for creating Java programs.
- Download the latest version of the Java Development Kit (JDK) for Java Standard Edition (SE) from if you're unsure of which version to download.
- Double-click the downloaded file to install it.
Download and Install Eclipse
You'll use the free Eclipse Java development environment to enter your Java program code and to create your programs.
- Download the latest version of Eclipse IDE for Java Developers from http://www.eclipse.org/downloads/.
- to your computer. I put mine in my user folder, c:\Users\Annette. If more than one person will create Java programs on your computer, extract the files again into a different folder for each additional person.
- Create a to the eclipse.exe file in the eclipse directory you just extracted. My file was in c:\Users\Annette\eclipse. Paste this shortcut for starting Eclipse onto your desktop. If more than one person will create Java programs on your computer, create and rename a shortcut for each of you. For example, I renamed mine Eclipse for Annette.
See if you want more detailed instructions for how to download and install Eclipse or to create a shortcut.
Download DIYJava.zip
There are a few more files needed for the lessons.
- Download DIYJava.zip for free from http://www.godtlandsoftware.com/DIYJava/.
- Extract the files from DIYJava.zip into your Java work folder.
See if you want more detailed instructions for how to download DIYJava.zip.
Set up Eclipse
Now that everything is installed, set up Eclipse.
- Double-click your Eclipse shortcut. This window should appear:
- Click the Browse... button to find your Java work folder, as shown in the above screenshot, and then click OK. This window should appear:
- Click Workbench to open the main Eclipse window.
Use the Java perspective:
- Click Window / Perspective / Open Perspective / Java