Free Essay

Java

In:

Submitted By Rajat123
Words 7556
Pages 31
Eclipse and Java for Total Beginners
Tutorial Companion Document

Eclipse And Java For Total Beginners
Companion Tutorial Document
By Mark Dexter

Table of Contents
Introduction........................................................................................................... .............................2
.
Tutorial Target Audience.....................................................................................................................2
Tutorial Objectives..............................................................................................................................2
Why learn Java with Eclipse?.............................................................................................................3
Topics Covered...................................................................................................................................3
Tutorial Approach............................................................................................................... ................3
.
Getting The Most From This Tutorial..................................................................................................3
Sample Java Application – Personal Lending Library........................................................................4
Downloading and Installing Eclipse ...................................................................................................4
Playing the Lessons................................................................................................................ ...........5
.
Re-Sizing the Video........................................................................................................................5
Closed Captions and Lesson Table of Contents............................................................................5
Lesson Outlines..................................................................................................................................6
Lesson 1 – Create Your First Java Class.......................................................................................6
Lesson 2 – Add Methods To Class.................................................................................................6
Lesson 3 – Use Eclipse Scrapbook................................................................................................6
Lesson 4 – JUnit Testing in Eclipse, Part 1....................................................................................6
Lesson 5 – JUnit Testing Continued...............................................................................................7
Lesson 6 – Using Test-First Development in Eclipse.....................................................................7
Lesson 7 – Create Book Class.......................................................................................................7
Lesson 8 – Add Person to Book Class...........................................................................................7
Lesson 9 – MyLibrary Class and ArrayList.....................................................................................7
Lesson 10 – Start on MyLibrary Class...........................................................................................7
Lesson 11 – Create first methods in MyLibrary class.....................................................................8
Lesson 12 – Create checkOut, checkIn Methods..........................................................................8
Lesson 13 – Continue checkOut Method.......................................................................................8
Lesson 14 – Finish checkOut Method............................................................................................8
Lesson 15 – Finish MyLibrary Methods..........................................................................................8
Lesson 16 – Create main Method and JAR File.............................................................................9
Alphabetical Index by Lesson.............................................................................................................9
Glossary of Terms.............................................................................................................................12
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 1 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document
Additional Resources........................................................................................................................14
Eclipse Websites..........................................................................................................................15
Java Websites.............................................................................................................................. 15
.
Java Books......................................................................................................... .........................15
.
Code Snapshots...............................................................................................................................15
Lesson 1 – Person class (fields and constructor)........................................................................15
Lesson 2 – Person class (with get and set methods)...................................................................16
Lesson 3 (scrapbook snapshots)..................................................................................................17
Lesson 5 – PersonTest class........................................................................................................17
Lesson 6 – Added toString() Method to Person class..................................................................18
Lesson 7 – TestBook and Book Classes......................................................................................20
Lesson 8 – Add Person to Book Class.........................................................................................21
Lesson 9 – MyLibrary Class and ArrayList...................................................................................23
Lesson 10 – Start on MyLibrary Class.........................................................................................23
Lesson 11 – Create first methods in MyLibrary class...................................................................24
Lesson 12 – Create checkOut, checkIn Methods........................................................................27
Lesson 13 – Continue checkOut Method.....................................................................................31
Lesson 14 – Finish checkOut Method..........................................................................................33
Lesson 15 – Finish MyLibrary Methods........................................................................................35
Lesson 16 – Create main Method and JAR File..........................................................................41

Introduction
This document is designed to accompany the “Eclipse And Java For Total Beginners” video tutorial, which is available at http://eclipsetutorial.sourceforge.net/.

Tutorial Target Audience
This tutorial is targeted for people who are new to Eclipse and to Java. It is designed to work either for those with prior programming experience in other languages or for those without prior experience.

Tutorial Objectives
The objectives of this tutorial are as follows:


Demonstrate the basics of using Eclipse for writing Java programs



Demonstrate how to use Eclipse for agile software development



Demonstrate how to use existing Java learning resources (tutorials, examples, books) within
Eclipse



Create a foundation for continuing to learn Java and Eclipse

Note that Java and Eclipse are both large subject areas that cannot possibly be mastered in a short period of time. This tutorial will help get you started and give you some of the skills needed to learn on your own.
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 2 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Why learn Java with Eclipse?
There are many ways to learn how to program in Java. The author believes that there are advantages to learning Java using the Eclipse integrated development environment (IDE). Some of these are listed below:


Eclipse provides a number of aids that make writing Java code much quicker and easier than using a text editor. This means that you can spend more time learning Java, and less time typing and looking up documentation.



The Eclipse debugger and scrapbook allow you to look inside the execution of the Java code.
This allows you to “see” objects and to understand how Java is working behind the scenes



Eclipse provides full support for agile software development practices such as test-driven development and refactoring. This allows you to learn these practices as you learn Java.



If you plan to do software development in Java, you’ll need to learn Eclipse or some other IDE.
So learning Eclipse from the start will save you time and effort.

The chief concern with learning Java with an IDE is that learning the IDE itself will be difficult and will distract you from learning Java. It is hoped that this tutorial will make learning the basics of Eclipse relatively painless so you can focus on learning Java.

Topics Covered
This tutorial will cover the following topics:


Basics of Eclipse for Java development



Basics of Java and object-oriented programming (OOP)



Test-driven development (TDD) and unit testing in Eclipse

Tutorial Approach
The tutorial is organized around the following activities.


Write a small sample Java application to track your personal lending library.



Use the “test-first” approach to develop most methods.



Write a “test drive” program and create an executable JAR file, and run the JAR file from the system console.

Concepts are introduced as needed during the development of the sample applications.

Getting The Most From This Tutorial
This tutorial can be used as an in-depth demonstration of Java development in Eclipse. However, if you want to actually learn how to write Java programs in Eclipse, the following approach is recommended: Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 3 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document



Have Eclipse installed and ready to go.



Work side-by-side with the lessons, pausing and rewinding as needed.



Use this guide as needed.



Consult other resources as needed to understand the topics covered in greater depth.



Keep a positive attitude!

Sample Java Application – Personal Lending Library
We will develop a small sample application to track our personal library of books. It will also include a list of people to whom we loan our books. The application will have three Java classes:
1. Person will have fields for the person's name and a number which will be the maximum number of books this person can borrow at one time.
2. Book will have fields for title, author, and the person who is currently borrowing the book.
3. MyLibrary will contain a list of all of our books and a list of all of the people who might borrow them. Downloading and Installing Eclipse
Before Installing Eclipse, you need to have either the Java JDK (Java development kit) or Java JRE
(Java runtime engine) installed on your computer. These are available at http://java.sun.com/javase/downloads/index.jsp. Installing the JDK or JRE is reasonably simple. Detailed, step-by-step instructions, if needed, are available in the PDF Eclipse Tutorial at the https://www.arctechsoftware.com/tutorial/welcomePage.do. (Follow the link to “Beginning Eclipse”.)
For Java development, the JDK is recommended because it allows you to see documentation and source code for the standard Java classes. However, either the JDK or JRE will work for this tutorial.
This tutorial is based on Eclipse 3.3, although you could use 3.2 and probably later versions as well.
Here are the steps to install Eclipse 3.3 from www.eclipse.org:


Navigate to www.eclipse.org/downloads



Select “Eclipse IDE for Java Developers”. If your platform is Linux or MacOSX, be sure to select the link to the right. Note that you can use “Eclipse IDE for Java EE Developers”,
“Eclipse for RCP/Plug-in Developers”, or “Eclipse Classic” as well. All of these include the Java development portions of Eclipse used in this tutorial.



On the www.eclipse.org/downloads page, follow the link “Find out more”. Scroll your browser to display the far right-hand side of the screen to the column “Tutorials and Help”. The first tutorial is a Screencam tutorial that steps you through downloading and installing Eclipse on
Windows.

The Eclipse installation is very straightforward. There is no installation program. Instead, you just create the top-level folder and the unzip the file inside this folder. In Windows XP, for example, just
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 4 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document copy the zip file to your root directory (e.g., “C:\”) and then unzip the downloaded zip file. This will create a folder called “C:\eclipse”. The Eclipse programs will be created in several subfolders
(configuration, features, plugins, readme). The procedure for Linux is similar, except your unzip the
.tar.gz file.

Playing the Lessons
To play the lessons, follow these steps.
1. Download the 16 lesson zip files (totalbeginnerlessonxx.zip).
2. Unzip each lesson's zip file into a directory on your system.
3. Find the file called “lessonxx.html”, where xx is the lesson number 01-16.
4. Open this file with your browser (e.g., Internet Explorer or Firefox) and press the large play button on the video thumbnail. The lesson should play inside the browser. Note that you need to have the Adobe Flash player installed on your system. This can be downloaded from Adobe at http://www.adobe.com/products/flashplayer/.
5. Controls for the video are below the video. A Pause/Play button is in the lower left corner. A slide control allows you to fast-forward or rewind the video to any point in time. Controls for
Volume, Table of Contents, and Closed Captions are in the lower right corner.

Re-Sizing the Video
Alternatively, you can open the file “lessonxx.swf” with your browser. In Windows Internet Explorer you might get a security warning and need to click and select “Allow blocked content”. If you open the lessonxx.swf file directly (as opposed to lessonxx.html), you can resize the video to any desired size, making it larger or smaller. Note that playing the video from the lessonxx.html file will provide the clearest rendition of the video but does not allow resizing.

Closed Captions and Lesson Table of Contents

The audio track of each lesson can be accompanied by closed captions (subtitles), which display in the lower portion of the video. Press the “CC” button in the lower right corner to toggle them on and off. Each lesson also contains a table of contents that allows you to jump to a specified point inside the video. To access the table of contents, click on the Table of Contents icon in the lower right corner of the video. Next to the Table of Contents control is the Volume control. The controls are shown above. Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 5 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Lesson Outlines
Lesson 1 – Create Your First Java Class


Create Java project in Eclipse



Create Java package



Discuss the Lending Library Application



Introduce classes and objects, naming conventions



Write a simple Java class (Person)

Lesson 2 – Add Methods To Class


Introduce Eclipse Views and Perspectives



Introduce Eclipse user interface – drag / drop, context menus, help



Add get and set methods to Person class

Lesson 3 – Use Eclipse Scrapbook


Introduce Eclipse Scrapbook



Introduce Java expressions, statements



Discuss Java packages



Create Person object in Scrapbook

Lesson 4 – JUnit Testing in Eclipse, Part 1


Create test source folder



Create PersonTest class



Run first JUnit test

Lesson 5 – JUnit Testing Continued


Test Person class – part 2



Create test methods for constructor, getName, and getMaximumBooks



Static methods

Lesson 6 – Using Test-First Development in Eclipse


Use test-first approach to write the Person toString() method



Method overriding

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 6 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document



Field Hiding

Lesson 7 – Create Book Class


Create BookTest before creating Book class



Test Book constructor



Create get and set methods

Lesson 8 – Add Person to Book Class


Create a relationship between the Book class and the Person Class



Test getPerson method



Create JUnit Test Suite

Lesson 9 – MyLibrary Class and ArrayList


How can we hold books, etc. in a collection?



MyLibrary object to hold Person & Entry objects



Introduce ArrayList in scrapbook



Introduce Java Generics



Method chaining

Lesson 10 – Start on MyLibrary Class


Create MyLibraryTest JUnit test



Create testMyLibrary to test MyLibrary constructor



Create MyLibrary constructor



Introduce instanceof operator



Introduce assertTrue method

Lesson 11 – Create first methods in MyLibrary class


Create test method for addBook, removeBook methods



Create addBook, removeBook methods and test



Create addPerson, removePerson methods



Introduce Eclipse refactoring – move local variable to field

Lesson 12 – Create checkOut, checkIn Methods


Create test for checkOut, checkIn methods

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 7 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document



Write checkout method



Introduce if / then / else syntax



Introduce boolean method



Write checkIn method

Lesson 13 – Continue checkOut Method


Test checkOut, checkIn methods



Fix compiler error – misplaced {}



Add test for maximum books



Create test for getBooksForPerson() method



Refactoring – extract method

Lesson 14 – Finish checkOut Method


Write getBooksForPerson method



Introduce “for each” loop



Introduce logical and operator “&&”



NullPointerException errors



Complete checkOut Method

Lesson 15 – Finish MyLibrary Methods


Create test for getAvailableBooks



Create getAvailableBooks



Create getUnavailableBooks

Lesson 16 – Create main Method and JAR File


Introduce main method



Write a main method



Run MyLibrary as Java application



Export to JAR file and run from Windows

Alphabetical Index by Lesson
Topic

Lesson

addBook() method.......................................................................................................... ..........11
.
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 8 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document addPerson() method............................................................................................................... ..11
.
agile software development........................................................................................................4
ArrayList class................................................................................................................... .....9
.
assertEquals() method ...............................................................................................................5 assertTrue() method.......................................................................................................... .......10
.
Book class................................................................................................................... ...............7
.
boolean type....................................................................................................................... ......10
.
build path........................................................................................................................... .........4
.
capitalization in Java...................................................................................................................1 checkOut() method................................................................................................................ ...12
.
class (Java)........................................................................................................... .....................1
.
code assist (CTRL+Space).........................................................................................................2 code assist template...................................................................................................................2 comments: in-line (//)............................................................................................................... ...1
.
constructor (Java).......................................................................................................................1
Convert Local Variable to Field, Eclipse Wizard.......................................................................11
CTRL+/, toggle in-line comments................................................................................................8
CTRL+Shift+P – find matching curly brace...............................................................................13
CTRL+Space – code assist........................................................................................................2 curly brace, highlighting in Eclipse............................................................................................13
Eclipse code assist (CTRL+Space)............................................................................................2
Eclipse Explorer class view.........................................................................................................2
Eclipse scrapbook.......................................................................................................................3
Eclipse user interface..................................................................................................................2 equals method for Strings (vs. == operator).............................................................................14 executable JAR file...................................................................................................................16 execute button, Eclipse scrapbook.............................................................................................3
Export to Java Archive (JAR) file, Eclipse wizard.....................................................................16 expressions........................................................................................................... .....................3
.
extends keyword.........................................................................................................................4 extreme programming.................................................................................................................4 fields (class)................................................................................................................................1 for each loop............................................................................................................................. 14
.
Generate Getters and Setters Wizard.........................................................................................2
Generics feature, Java..........................................................................................................9, 10 get() method, ArrayList................................................................................................................9 getAvailableBooks() method ....................................................................................................15 getBooksForPerson() method...................................................................................................14 getter methods............................................................................................................................2 getUnavailableBooks() method.................................................................................................15 if then statement .................................................................................................................. ....12
.
indexOf() method, ArrayList........................................................................................................9 inspect button, Eclipse scrapbook..............................................................................................3 instanceof operator...................................................................................................................10
JAR archive file.........................................................................................................................16 java -jar command................................................................................................................. ...16
.
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 9 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document
JUnit test class, create new........................................................................................................4
JUnit test library..........................................................................................................................4
JUnit test, running.......................................................................................................................5
JUnit View...................................................................................................................................5
JUnit, compare actual with expected..........................................................................................5 lending library application........................................................................................................... 1
.
main() method...........................................................................................................................16 methods (class).............................................................................................................. ............2
.
MyLibrary class.........................................................................................................................10 mylibrary.jar file.........................................................................................................................16 new keyword...............................................................................................................................3
NullPointerException.................................................................................................................14
Object class, Java.......................................................................................................................3 object dependency......................................................................................................................8 object equality, in Java ...............................................................................................................8 override, method ........................................................................................................................6 package-protected access modifier..........................................................................................10 package, creating............................................................................................................ ...........1
.
packages, Java...........................................................................................................................3
Person class........................................................................................................................ .......1
.
perspective (Eclipse)......................................................................................................... .........2
.
printStatus() method............................................................................................................... ..16
.
private access modifier...............................................................................................................8 project, creating..................................................................................................................... .....1
.
public access modifier.................................................................................................................8
Quick fix (CTRL+1).................................................................................................................... .4
.
Quick fix (CTRL+1), in test-first development.............................................................................7
Refactor Menu, Eclipse.............................................................................................................11 removeBook() method...............................................................................................................11 removePerson() method........................................................................................................... 11
.
scrapbook, Eclipse......................................................................................................................3 scrapbook, set imports button.....................................................................................................3 setter methods.............................................................................................................................
2
setup() method, JUnit test class................................................................................................11
Source folder, create new...........................................................................................................4 statements.......................................................................................................................... ........2
.
static methods......................................................................................................... ...................5
.
subclass .....................................................................................................................................4 superclass............................................................................................................ ......................4
.
System.out.println() command....................................................................................................3
Task view, TODO comments.......................................................................................................7 test method as specification .......................................................................................................6 test method, create ....................................................................................................................5 test suite, JUnit..................................................................................................................... ......8
.
test-driven development (TDD)...................................................................................................6 test-first development..................................................................................................................6
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 10 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document testBook() method ........................................................................................................... ..........7
.
testCheckOut() method ............................................................................................................12 testGetAvailableBooks() method..............................................................................................15 testGetBooksForPerson() method ...........................................................................................13 testGetUnavailableBooks() method..........................................................................................15 this keyword............................................................................................................................2, 6
TODO comment, task list............................................................................................................7 toString() method .......................................................................................................................6 unit test............................................................................................................................. ..........4
.
view (Eclipse)......................................................................................................... ....................2
.
welcome screen (Eclipse)...........................................................................................................1 workbench (Eclipse)......................................................................................................... ..........2
.
workbench regions......................................................................................................................2 workspace (Eclipse)....................................................................................................................1
; semicolon, in Java................................................................................................................. ...2
.
!= operator (not equal to)..........................................................................................................12
( ) parentheses, in Java............................................................................................................. .2
.
{ } curly braces, in Java...............................................................................................................2
/* */ comments............................................................................................................... .............2
.
&& operator (and operator).......................................................................................................14
== operator (equals)............................................................................................................... ..12
.

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 11 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Glossary of Terms
Term

Quick Definition

Access Modifier

Reserved words “public”, “private”, “protected” in Java. Control whether classes and members may be accessed from any class, only this class, subclasses. Default is access from any class in the package.

Agile (or Extreme)
Development

Methodology for developing software that emphasizes, among other things, unit testing as part of development process.

API (Application
Programming Interface)

The way one program uses another program. In Java, the API can be thought of as the collection of public methods for a class or package.

Class

Main building block in Java. Contains members, including fields and methods. Classes are the “blueprint” for creating objects.

Constructor

Special block of code used to create an instance of a class (or, if you prefer, an object whose type is the class). Used with the “new” keyword
(e.g., Person p = new Person() calls the Person() constructor).

Field

Member in a class that holds data (e.g., name, age, etc.). Usually marked private so that other programs cannot directly access.

IDE (Integrated
Development
Environment)

Program, like Eclipse, that provides the different tools required to develop a software package.

JVM (Java Virtual
Machine) (also known as
Java Runtime Engine or
JRE)

The program that runs Java programs on a specific platform. Java source code is compiled into .class files. These contain the instructions used by the JVM to actually run the programs on a Windows PC, a Linux computer, a Mac computer, etc. The JVM is written for each platform supported by
Java.

JUnit Test

A Java class used to test individual methods in a class. Used to build test cases, e.g., when using agile development methodology.

Method

Member in a class that does some processing (e.g., like a subroutine or function in other languages).

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 12 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document
Term

Quick Definition

Method Argument, Method Parameters refers to the list of variables in a method declaration.
Parameter
Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order. For example, in the method public setName(String name) {…}
“name” is the parameter for this method. If this method is used as follows: myObject.setName(“Fred”); “Fred” is the argument of the method and it must match the type of the method’s parameter.
Method Signature

A method’s name plus it’s parameter list. For example, a method defined as “setName (String name)” has a method signature of “setName(String)”.
Method signatures are important because they allow methods to be overloaded (i.e., have the same name but different signatures). For example, the method “setName(String firstName, String lastName) could be an overload of “setName(String name)” because it as a different signature (“setName(String, String)”).

Object

An instance of a class. For example, Cookie could be a class, and a cookie (e.g. “thisCookie”) would be an object created using the class. In other words, “thisCookie” is an object of type Cookie or an instance of
Cookie.

Overload (Method)

To provide multiple methods with the same name but different parameters
(i.e., same name but different signatures).

Override (Method)

When a subclass implements a method inherited from the super class, this method is said to be overridden.

Package

Packages are imported into a source file to save typing the full name of the class (e.g., can say “Person” instead of
“org.eclipsetraining.librarytutorial.Person” and to avoid the possibility of two classes having identical names.

Project

In Eclipse, a way to organize your work. An Eclipse workspace can contain multiple projects. Each project can contain multiple packages.
Each package can contain multiple classes.

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 13 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document
Term

Quick Definition

Refactor

To improve a program without changing the way it works (i.e., its API).
Example include renaming fields or variables, streamlining code, etc. Very important in agile development because of emphasis on self-documenting code. Reference Variable

In Java, variable that holds an object reference (e.g., p = new Person();).
Points to an area on the “heap” where the object resides. Contrast with value variable.

Scrapbook Page

Area in Eclipse where you can execute Java code “snippets” and see how they work. Great for experimenting with Java statements.

Static Method

A method that belongs to the entire class instead of one instance of the class. Invoked with . (e.g., Person.getTotalCount()).
Used for methods that don’t rely on any one instance of a class.

Swing

A set of standard Java packages that implement a graphical user interface without using any “native” code.

SWT (Standard Widget
Toolkit)

Set of Java classes and native programs developed by Eclipse to allow
Java programs to have the look and feel of native programs on each platform. Used to create the Eclipse IDE.

Type

In Java, an attribute of a variable to indicate either a primitive type (int, boolean, etc.) or class membership. For objects, the type is the class to which it belongs. Types also include interfaces and enumerations.

Value Variable

In Java, variable that holds the value of a Java primitive (e.g., integer, character, etc.). Held in the memory stack. Contrast with reference variable. Workspace

Top-level container for Eclipse work. Holds multiple projects. In a single
Eclipse session, only one workspace can be active.

Additional Resources
There are many resources available for learning more about Eclipse and Java. These are just a few that I’ve found helpful.

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 14 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Eclipse Websites


www.eclipse.org/resources. This lists a number of articles, books, presentations, demonstrations and other resources on a variety of topics related to Eclipse.



eclipse.newcomer newsgroup. This is a friendly, active newsgroup where newcomers to
Eclipse can ask questions. The search feature of this and other newsgroups can be especially valuable, since there is a good chance that your question has already been asked and answered. 

Beginning Eclipse Tutorial on ArcTech Software LLC website. Written tutorial to get you started with Eclipse and Java. Login required to download. It has a very good section on downloading and installing the Java JDK. Link to tutorial is https://www.arctechsoftware.com/tutorial/tutorial.do?subcatId=1. Link to home page is https://www.arctechsoftware.com/tutorial/welcomePage.do. Java Websites


The Java Tutorials from Sun (http://java.sun.com/docs/books/tutorial/java/index.html). The gold standard for learning Java, and it’s free.



JavaRanch Big Moose Saloon web site (http://saloon.javaranch.com/cgibin/ubb/ultimatebb.cgi?category=1). This has a variety of forums, including Java in General
(beginner), Java in General (intermediate), and many other Java topics. Very active and friendly, with knowledgeable moderators.



The Java Developers Almanac 1.4 (http://www.exampledepot.com/). Contains Java code samples for many topics.

Java Books


Head First Java, by Kathy Sierra & Bert Bates. Excellent, fun, creative book for Java and OOP beginners. 

Thinking In Java, by Bruce Eckel. Excellent, thorough reference for Java. For all levels of programmer. 

Effective Java, by Joshua Bloch. Concise book documents specific recommendations for Java best practices. For intermediate to advanced programmers.

Code Snapshots
The following pages contain code snapshots as of the end of each lesson. These can be used to compare your code to or to help you fix any problems you might have. If needed, you can copy and past this code into your Eclipse Java source files. Also, if you want to start the tutorial in the middle, these can help you catch up to the correct point.

Lesson 1 – Person class (fields and constructor)

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 15 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

package org.totalbeginner.tutorial;

public class Person {
// fields private String name; // name of the person private int maximumBooks; // most books the person can check out
// constructors public Person() { name = "unknown name"; maximumBooks = 3;
}
}

Lesson 2 – Person class (with get and set methods)
Note: Highlighted code added in this lesson. package org.totalbeginner.tutorial;

public class Person {
// fields private String name; // name of the person private int maximumBooks; // most books the person can check out
// constructors public Person() { name = "unknown name"; maximumBooks = 3;
}
//methods public String getName() { return name;
}
public void setName(String anyName) { name = anyName;
}
public int getMaximumBooks() {
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 16 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

return maximumBooks;
}
public void setMaximumBooks(int maximumBooks) { this.maximumBooks = maximumBooks;
}
}

Lesson 3 (scrapbook snapshots)
Expressions:
2 + 2 int a = 5; a = a * 10; a (after import of org.totalbeginner.tutorial.*)
Person p = new Person();
p.setName(“Fred”);
P

Lesson 5 – PersonTest class package org.totalbeginner.tutorial; import junit.framework.TestCase; public class PersonTest extends TestCase { public void testPerson() {
Person p1 = new Person(); assertEquals("unknown name", p1.getName());
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 17 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

assertEquals(3, p1.getMaximumBooks());
}
public void testSetName() {
Person p2 = new Person(); p2.setName("Fred"); assertEquals("Fred", p2.getName());
}
public void testSetMaximumBooks() {
Person p3 = new Person(); p3.setMaximumBooks(10); assertEquals(10, p3.getMaximumBooks());
}
}

Lesson 6 – Added toString() Method to Person class
Note: Person and PersonTest classes are complete at this point. package org.totalbeginner.tutorial;

public class Person {
// fields private String name; // name of the person private int maximumBooks; // most books the person can check out
// constructors public Person() { name = "unknown name"; maximumBooks = 3;
}
//methods public String getName() { return name;
}
public void setName(String anyName) { name = anyName;
}

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 18 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

public int getMaximumBooks() { return maximumBooks;
}
public void setMaximumBooks(int maximumBooks) { this.maximumBooks = maximumBooks;
}
public String toString() { return this.getName() + " (" + this.getMaximumBooks() +
" books)";
}

}

package org.totalbeginner.tutorial; import junit.framework.TestCase; public class PersonTest extends TestCase { public void testPerson() {
Person p1 = new Person(); assertEquals("unknown name", p1.getName()); assertEquals(3, p1.getMaximumBooks());
}
public void testSetName() {
Person p2 = new Person(); p2.setName("Fred"); assertEquals("Fred", p2.getName());
}
public void testSetMaximumBooks() {
Person p3 = new Person(); p3.setMaximumBooks(10); assertEquals(10, p3.getMaximumBooks());
}
public void testToString() {
Person p4 = new Person(); p4.setName("Fred Flintstone");

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 19 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

p4.setMaximumBooks(7);
String testString = "Fred Flintstone (7 books)"; assertEquals(testString, p4.toString());
}
}

Lesson 7 – TestBook and Book Classes package org.totalbeginner.tutorial; import junit.framework.TestCase; public class BookTest extends TestCase { public void testBook() {
Book b1 = new Book("Great Expectations"); assertEquals("Great Expectations", b1.title); assertEquals("unknown author", b1.author);
}
}

package org.totalbeginner.tutorial; public class Book {
String title;
String author; public Book(String string) { this.title = string; this.author = "unknown author";
}
public String getAuthor() { return author;
}

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 20 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

public void setAuthor(String author) { this.author = author;
}
public String getTitle() { return title;
}
}

package org.totalbeginner.tutorial; import junit.framework.Test; import junit.framework.TestSuite; public class AllTests { public static Test suite() {
TestSuite suite = new TestSuite("Test for org.totalbeginner.tutorial");
//$JUnit-BEGIN$
suite.addTestSuite(BookTest.class); suite.addTestSuite(PersonTest.class); //$JUnit-END$ return suite;
}
}

Lesson 8 – Add Person to Book Class
Note: BookTest and Book classes are complete at this point.

package org.totalbeginner.tutorial; public class Book {
String title;
String author;
Person person;

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 21 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

public Book(String string) { this.title = string; this.author = "unknown author";
}
public String getAuthor() { return author;
}
public void setAuthor(String author) { this.author = author;
}
public String getTitle() { return title;
}
public void setPerson(Person p2) { this.person = p2;
}
public Person getPerson() { return this.person;
}
}

package org.totalbeginner.tutorial; import junit.framework.TestCase; public class BookTest extends TestCase { public void testBook() {
Book b1 = new Book("Great Expectations"); assertEquals("Great Expectations", b1.title); assertEquals("unknown author", b1.author);
}
public void testGetPerson() {

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 22 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Book b2 = new Book("War And Peace");
Person p2 = new Person(); p2.setName("Elvis"); // method to say book is loaned to this person b2.setPerson(p2); // get the name of the person who has the book
Person testPerson = b2.getPerson();
String testName = testPerson.getName();

//
//

String testName = b2.getPerson().getName(); assertEquals("Elvis", testName);

}
}

Lesson 9 – MyLibrary Class and ArrayList
Scrapbook Example
ArrayList list = new ArrayList();
Book b1 = new Book("Great Expectations");
Book b2 = new Book("War and Peace"); list.add(b1); list.add(b2);
Person p1 = new Person(); p1.setName("Fred"); b1.setPerson(p1); list.remove(b1); list

Lesson 10 – Start on MyLibrary Class
Note: From this point on, if needed use the Person and PersonTest classes from Lesson 6 and the
Book and BookTest classes from Lesson 8.

package org.totalbeginner.tutorial;

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 23 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

import java.util.ArrayList; import junit.framework.TestCase; public class MyLibraryTest extends TestCase {
// test constructor public void testMyLibrary() {
MyLibrary ml = new MyLibrary("Test"); assertEquals("Test", ml.name); assertTrue(ml.books instanceof ArrayList); assertTrue(ml.people instanceof ArrayList);
}
}

package org.totalbeginner.tutorial; import java.util.ArrayList; public class MyLibrary {
String name;
ArrayList books;
ArrayList people; public MyLibrary(String name) { this.name = name; books = new ArrayList(); people = new ArrayList();
}
}

Lesson 11 – Create first methods in MyLibrary class

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 24 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

package org.totalbeginner.tutorial; import java.util.ArrayList; import junit.framework.TestCase; public class MyLibraryTest extends TestCase { private Book b1; private Book b2; private Person p1; private Person p2; private MyLibrary ml;
// test constructor public void testMyLibrary() {
MyLibrary ml = new MyLibrary("Test"); assertEquals("Test", ml.name); assertTrue(ml.books instanceof ArrayList); assertTrue(ml.people instanceof ArrayList);
}
public void setup() { b1 = new Book("Book1"); b2 = new Book("Book2"); p1 = new Person(); p2 = new Person(); p1.setName("Fred"); p2.setName("Sue"); ml = new MyLibrary("Test");
}
public void testAddBook() {
//create test objects setup(); //test initial size is 0 assertEquals(0, ml.getBooks().size()); ml.addBook(b1); Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 25 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

ml.addBook(b2); assertEquals(2, ml.getBooks().size()); assertEquals(0, ml.getBooks().indexOf(b1)); assertEquals(1, ml.getBooks().indexOf(b2)); ml.removeBook(b1); assertEquals(1, ml.getBooks().size()); assertEquals(0, ml.getBooks().indexOf(b2)); ml.removeBook(b2); assertEquals(0, ml.getBooks().size());
}
}

package org.totalbeginner.tutorial; import java.util.ArrayList; public class MyLibrary {
String name;
ArrayList books;
ArrayList people; public MyLibrary(String name) { this.name = name; books = new ArrayList(); people = new ArrayList();
}
public String getName() { return name;
}
public ArrayList getBooks() { return books;
}
public ArrayList getPeople() { return people;
}

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 26 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

public void addBook(Book b1) { this.books.add(b1); } public void removeBook(Book b1) { this.books.remove(b1); } public void addPerson(Person p1) { this.people.add(p1); } public void removePerson(Person p1) { this.people.remove(p1); }
}

Lesson 12 – Create checkOut, checkIn Methods package org.totalbeginner.tutorial; import java.util.ArrayList; import junit.framework.TestCase; public class MyLibraryTest extends TestCase { private Book b1; private Book b2; private Person p1; private Person p2; private MyLibrary ml;
// test constructor public void testMyLibrary() {
MyLibrary ml = new MyLibrary("Test"); assertEquals("Test", ml.name);

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 27 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

assertTrue(ml.books instanceof ArrayList); assertTrue(ml.people instanceof ArrayList);
}
public void setup() { b1 = new Book("Book1"); b2 = new Book("Book2"); p1 = new Person(); p2 = new Person(); p1.setName("Fred"); p2.setName("Sue"); ml = new MyLibrary("Test");
}
public void testAddBook() {
//create test objects setup(); //test initial size is 0 assertEquals(0, ml.getBooks().size()); ml.addBook(b1); ml.addBook(b2); assertEquals(2, ml.getBooks().size()); assertEquals(0, ml.getBooks().indexOf(b1)); assertEquals(1, ml.getBooks().indexOf(b2)); ml.removeBook(b1); assertEquals(1, ml.getBooks().size()); assertEquals(0, ml.getBooks().indexOf(b2)); ml.removeBook(b2); assertEquals(0, ml.getBooks().size());
}
private void addItems() { ml.addBook(b1); ml.addBook(b2); ml.addPerson(p1); Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 28 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

ml.addPerson(p2);
}

public void testCheckOut() {
// set up objects setup(); addItems(); assertTrue("Book did not check out correctly", ml.checkOut(b1,p1)); assertEquals("Fred", b1.getPerson().getName()); assertFalse("Book was already checked out", ml.checkOut(b1,p2)); assertTrue("Book check in failed", ml.checkIn(b1));

assertFalse("Book was already checked in", ml.checkIn(b1)); assertFalse("Book was never checked out", ml.checkIn(b2));

}

}

package org.totalbeginner.tutorial; import java.util.ArrayList; public class MyLibrary {
String name;
ArrayList books;
ArrayList people; public MyLibrary(String name) { this.name = name; books = new ArrayList();

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 29 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

people = new ArrayList();
}
public String getName() { return name;
}
public ArrayList getBooks() { return books;
}
public ArrayList getPeople() { return people;
}
public void addBook(Book b1) { this.books.add(b1); } public void removeBook(Book b1) { this.books.remove(b1); } public void addPerson(Person p1) { this.people.add(p1); } public void removePerson(Person p1) { this.people.remove(p1); } public boolean checkOut(Book b1, Person p1) { if ((b1.getPerson() == null) ){ b1.setPerson(p1); return true;
}
else { return false;
}
} public boolean checkIn(Book b1) { if (b1.getPerson() != null) {
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 30 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

b1.setPerson(null); return true;
}
else { return false;
}
}

}

Lesson 13 – Continue checkOut Method package org.totalbeginner.tutorial; import java.util.ArrayList; public class MyLibrary {
String name;
ArrayList books;
ArrayList people; public MyLibrary(String name) { this.name = name; books = new ArrayList(); people = new ArrayList();
}
public String getName() { return name;
}
public ArrayList getBooks() { return books;
}
public ArrayList getPeople() { return people;
}
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 31 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

public void addBook(Book b1) { this.books.add(b1); } public void removeBook(Book b1) { this.books.remove(b1); } public void addPerson(Person p1) { this.people.add(p1); } public void removePerson(Person p1) { this.people.remove(p1); } public boolean checkOut(Book b1, Person p1) { if ((b1.getPerson() == null) ){ b1.setPerson(p1); return true;
}
else { return false;
}
} public boolean checkIn(Book b1) {
// TODO Auto-generated method stub return false;
}
public ArrayList getBooksForPerson(Person p1) {
// TODO Auto-generated method stub return null;
}

}

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 32 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Lesson 14 – Finish checkOut Method package org.totalbeginner.tutorial; import java.util.ArrayList; public class MyLibrary {
String name;
ArrayList books;
ArrayList people; public MyLibrary(String name) { this.name = name; books = new ArrayList(); people = new ArrayList();
}
public String getName() { return name;
}
public ArrayList getBooks() { return books;
}
public ArrayList getPeople() { return people;
}
public void addBook(Book b1) { this.books.add(b1); } public void removeBook(Book b1) { this.books.remove(b1); } public void addPerson(Person p1) { this.people.add(p1); }

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 33 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

public void removePerson(Person p1) { this.people.remove(p1); } public boolean checkOut(Book b1, Person p1) { int booksOut = this.getBooksForPerson(p1).size(); if ((b1.getPerson() == null) && booksOut < p1.getMaximumBooks()){ b1.setPerson(p1); return true;
}
else { return false;
}
} public boolean checkIn(Book b1) { if (b1.getPerson() != null) { b1.setPerson(null); return true;
}
else { return false;
}
} public ArrayList getBooksForPerson(Person p1) {
ArrayList result = new ArrayList(); for (Book aBook : this.getBooks()) { if ((aBook.getPerson() != null) &&
(aBook.getPerson().getName()
.equals(p1.getName())))
{
result.add(aBook);
}
} return result;
}
}

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 34 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Lesson 15 – Finish MyLibrary Methods package org.totalbeginner.tutorial; import java.util.ArrayList; import junit.framework.TestCase; public class MyLibraryTest extends TestCase { private Book b1; private Book b2; private Person p1; private Person p2; private MyLibrary ml;
// test constructor public void testMyLibrary() {
MyLibrary ml = new MyLibrary("Test"); assertEquals("Test", ml.name); assertTrue(ml.books instanceof ArrayList); assertTrue(ml.people instanceof ArrayList);
}
public void setup() { b1 = new Book("Book1"); b2 = new Book("Book2"); p1 = new Person(); p2 = new Person(); p1.setName("Fred"); p2.setName("Sue"); ml = new MyLibrary("Test");
}
public void testAddBook() {
//create test objects setup(); //test initial size is 0
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 35 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

assertEquals(0, ml.getBooks().size()); ml.addBook(b1); ml.addBook(b2); assertEquals(2, ml.getBooks().size()); assertEquals(0, ml.getBooks().indexOf(b1)); assertEquals(1, ml.getBooks().indexOf(b2)); ml.removeBook(b1); assertEquals(1, ml.getBooks().size()); assertEquals(0, ml.getBooks().indexOf(b2)); ml.removeBook(b2); assertEquals(0, ml.getBooks().size());
}

public void testCheckOut() {
// set up objects setup(); addItems(); assertTrue("Book did not check out correctly", ml.checkOut(b1,p1)); assertEquals("Fred", b1.getPerson().getName()); assertFalse("Book was already checked out", ml.checkOut(b1,p2)); assertTrue("Book check in failed", ml.checkIn(b1));

assertFalse("Book was already checked in", ml.checkIn(b1)); assertFalse("Book was never checked out", ml.checkIn(b2));
// additional test for maximumBooks setup(); p1.setMaximumBooks(1); addItems(); Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 36 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

assertTrue("First book did not check out", ml.checkOut(b2, p1)); assertFalse("Second book should not have checked out", ml.checkOut(b1, p1));
}
private void addItems() { ml.addBook(b1); ml.addBook(b2); ml.addPerson(p1); ml.addPerson(p2);
}
public void testGetBooksForPerson() { setup(); addItems(); assertEquals(0, ml.getBooksForPerson(p1).size()); ml.checkOut(b1, p1);
ArrayList testBooks = ml.getBooksForPerson(p1); assertEquals(1, testBooks.size()); assertEquals(0, testBooks.indexOf(b1)); ml.checkOut(b2, p1); testBooks = ml.getBooksForPerson(p1); assertEquals(2, testBooks.size()); assertEquals(1, testBooks.indexOf(b2));
}
public void testGetAvailableBooks() { setup(); addItems();
ArrayList testBooks = ml.getAvailableBooks(); assertEquals(2, testBooks.size()); assertEquals(1, testBooks.indexOf(b2)); ml.checkOut(b1, p1); testBooks = ml.getAvailableBooks(); assertEquals(1, testBooks.size()); assertEquals(0, testBooks.indexOf(b2));

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 37 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

ml.checkOut(b2, p1); testBooks = ml.getAvailableBooks(); assertEquals(0, testBooks.size());
}
public void testGetUnavailableBooks() { setup(); addItems(); assertEquals(0, ml.getUnavailableBooks().size()); ml.checkOut(b1, p1);
ArrayList testBooks = ml.getUnavailableBooks(); assertEquals(1, testBooks.size()); assertEquals(0, testBooks.indexOf(b1)); ml.checkOut(b2, p2); testBooks = ml.getUnavailableBooks(); assertEquals(2, testBooks.size()); assertEquals(1, testBooks.indexOf(b2));
}
public void testToString() { setup(); addItems(); assertEquals("Test: 2 books; 2 people.", ml.toString()); }

}

package org.totalbeginner.tutorial; import java.util.ArrayList; public class MyLibrary {
String name;

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 38 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

ArrayList books;
ArrayList people; public MyLibrary(String name) { this.name = name; books = new ArrayList(); people = new ArrayList();
}
public String getName() { return name;
}
public ArrayList getBooks() { return books;
}
public ArrayList getPeople() { return people;
}
public void addBook(Book b1) { this.books.add(b1); } public void removeBook(Book b1) { this.books.remove(b1); } public void addPerson(Person p1) { this.people.add(p1); } public void removePerson(Person p1) { this.people.remove(p1); } public boolean checkOut(Book b1, Person p1) { int booksOut = this.getBooksForPerson(p1).size(); if ((b1.getPerson() == null) && booksOut < p1.getMaximumBooks()){ b1.setPerson(p1); return true;
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 39 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

} else { return false;
}
} public boolean checkIn(Book b1) { if (b1.getPerson() != null) { b1.setPerson(null); return true;
}
else { return false;
}
} public ArrayList getBooksForPerson(Person p1) {
ArrayList result = new ArrayList(); for (Book aBook : this.getBooks()) { if ((aBook.getPerson() != null) &&
(aBook.getPerson().getName()
.equals(p1.getName())))
{
result.add(aBook);
}
} return result;
}
public ArrayList getAvailableBooks() {
ArrayList result = new ArrayList(); for (Book aBook : this.getBooks()) { if (aBook.getPerson() == null) { result.add(aBook); }
}
return result;
}
public ArrayList getUnavailableBooks() {
ArrayList result = new ArrayList(); for (Book aBook : this.getBooks()) { if (aBook.getPerson() != null) {
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 40 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

result.add(aBook);
}
} return result;
}
public String toString() { return this.getName() + ": " + this.getBooks().size() + " books; " + this.getPeople().size() + " people.";
}
}

Lesson 16 – Create main Method and JAR File package org.totalbeginner.tutorial; import java.util.ArrayList; public class MyLibrary {
String name;
ArrayList books;
ArrayList people; public MyLibrary(String name) { this.name = name; books = new ArrayList(); people = new ArrayList();
}
public String getName() { return name;
}
public ArrayList getBooks() { return books;
}

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 41 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

public ArrayList getPeople() { return people;
}
public void addBook(Book b1) { this.books.add(b1); } public void removeBook(Book b1) { this.books.remove(b1); } public void addPerson(Person p1) { this.people.add(p1); } public void removePerson(Person p1) { this.people.remove(p1); } public boolean checkOut(Book b1, Person p1) { int booksOut = this.getBooksForPerson(p1).size(); if ((b1.getPerson() == null) && booksOut < p1.getMaximumBooks()){ b1.setPerson(p1); return true;
}
else { return false;
}
} public boolean checkIn(Book b1) { if (b1.getPerson() != null) { b1.setPerson(null); return true;
}
else { return false;
}
} public ArrayList getBooksForPerson(Person p1) {
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 42 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

ArrayList result = new ArrayList(); for (Book aBook : this.getBooks()) { if ((aBook.getPerson() != null) &&
(aBook.getPerson().getName()
.equals(p1.getName())))
{
result.add(aBook);
}
} return result;
}
public ArrayList getAvailableBooks() {
ArrayList result = new ArrayList(); for (Book aBook : this.getBooks()) { if (aBook.getPerson() == null) { result.add(aBook); }
}
return result;
}
public ArrayList getUnavailableBooks() {
ArrayList result = new ArrayList(); for (Book aBook : this.getBooks()) { if (aBook.getPerson() != null) { result.add(aBook); }
}
return result;
}
public String toString() { return this.getName() + ": " + this.getBooks().size() + " books; " + this.getPeople().size() + " people.";
}
public static void main(String[] args) {
// create a new MyLibrary
MyLibrary testLibrary = new MyLibrary("Test Drive Library");
Book b1 = new Book("War And Peace");
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 43 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

Book b2 = new Book("Great Expectations"); b1.setAuthor("Tolstoy"); b2.setAuthor("Dickens");
Person jim = new Person();
Person sue = new Person(); jim.setName("Jim"); sue.setName("Sue"); testLibrary.addBook(b1); testLibrary.addBook(b2); testLibrary.addPerson(jim); testLibrary.addPerson(sue);
System.out.println("Just created new library"); testLibrary.printStatus(); System.out.println("Check out War And Peace to Sue"); testLibrary.checkOut(b1, sue); testLibrary.printStatus(); System.out.println("Do some more stuff"); testLibrary.checkIn(b1); testLibrary.checkOut(b2, jim); testLibrary.printStatus(); } private void printStatus() {
System.out
.println("Status Report of MyLibrary \n" + this.toString()); for (Book thisBook : this.getBooks()) {
System.out.println(thisBook);
} for (Person

p : this.getPeople()) {

int count = this.getBooksForPerson(p).size();
System.out.println(p + " (has " + count
+ " of my books)");
}
System.out.println("Books Available: "
+ this.getAvailableBooks().size());
System.out.println("--- End of Status Report ---");
Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 44 of 45

Eclipse and Java for Total Beginners
Tutorial Companion Document

}
}

Copyright © 2007 Mark Dexter. Licensed under the Educational Community License version 1.0.

Page 45 of 45

Similar Documents

Premium Essay

Java and Java Script

...and lastly to the Internet, one thing has been a constant, different languages evolved based on a need. For these two languages, the Internet was a perfect fit, and without them the Internet would be a less dynamic and vibrant highway. As the Internet grew, more and more people found it a more viable place to do business. With that came a need for languages that were fairly easy to learn, dynamic, secure, portable, and maintainable. The industry answered that call with languages such as Java and JavaScript. This paper will perform an analysis of both Java and JavaScript. In order for the reader to gain a better understanding of these languages, the history of these languages with overviews will be presented along with a discussion of the benefits and drawbacks. The History of Java In the middle of May 1995 Java was introduced into the world, and along with Netscape it would be the new way for Internet users to access this new information superhighway. But before it got to this point, Java technology was developed almost by accident. Back in 1991, Sun Microsystems was looking into the future in anticipation of the future of computing, and they tasked a team that became know as the “Green Project”. Their main focus was to come up with a plan for the future of computing, but what they came out with was something quite unexpected. Under the guidance of James Gosling, a team was locked away in an external site to work on the project that would define Sun’s technology direction...

Words: 1329 - Pages: 6

Premium Essay

Java

...Software Design Introduction to the Java Programming Language Material drawn from [JDK99,Sun96,Mitchell99,Mancoridis00] Software Design (Java Tutorial) © SERG Java Features • “Write Once, Run Anywhere.” • Portability is possible because of Java virtual machine technology: – Interpreted – JIT Compilers • Similar to C++, but “cleaner”: – No pointers, typedef, preprocessor, structs, unions, multiple inheritance, goto, operator overloading, automatic coercions, free. Software Design (Java Tutorial) © SERG Java Subset for this Course • We will focus on a subset of the language that will allow us to develop a distributed application using CORBA. • Input and output will be character (terminal) based. • For detailed treatment of Java visit: – http://java.sun.com/docs/books/tutorial/index.html Software Design (Java Tutorial) © SERG Java Virtual Machine • Java programs run on a Java Virtual Machine. • Features: – – – – – Security Portability Superior dynamic resource management Resource location transparency Automatic garbage collection Software Design (Java Tutorial) © SERG The Java Environment Java Source File (*.java) Java Compiler (javac) Java Bytecode File (*.class) Java Virtual Machine (java) Software Design (Java Tutorial) © SERG Program Organization Source Files (.java) Running Application Running Applet JAVA BYTECODE COMPILER Class Files (.class) JAVA VIRTUAL MACHINE WEB BROWSER Software Design (Java Tutorial) © SERG Program Organization Standards •...

Words: 5230 - Pages: 21

Premium Essay

Java

...we're continuing our Java security research series by analyzing other plug-ins, browser extensions and rich internet applications that are commonly exploited. Our previous research indicated that the current state of Java affairs isn't pretty. At that time, ninety-three percent of enterprises were vulnerable to known Java exploits. Nearly 50 percent of enterprise traffic used a Java version that was more than two years out of date. Through Websense ThreatSeeker Intelligence Cloud analysis we now discover: Only 19 percent of enterprise Windows-based computers ran the latest version of Java (7u25) between August 1-29, 2013. More than 40 percent of enterprise Java requests are from browsers still using outdated Java 6. As a result, more than 80 percent of Java requests are susceptible to two popular new Java exploits: CVE-2013-2473 and CVE-2013-2463. 83.86 percent of enterprise browsers have Java enabled. Nearly 40 percent of users are not running the most up-to-date versions of Flash. In fact, nearly 25 percent of Flash installations are more than six months old, close to 20 percent are outdated by a year and nearly 11 percent are two years old. Our in-depth analysis ran for one month, across multiple verticals and industries. We surveyed millions of real-world web requests for Java usage through our global Websense ThreatSeeker Intelligence Cloud. New Java Exploits and the Neutrino Exploit Kit New Java exploits CVE-2013-2473...

Words: 1745 - Pages: 7

Premium Essay

Java

...that has made his work popular with programmers for many years. Michael Schidlowsky and Sedgewick have developed concise new Java implementations that both express the methods in a natural and direct manner and also can be used in real applications. Algorithms in Java, Third Edition, Part 5: Graph Algorithms is the second book in Sedgewick's thoroughly revised and rewritten series. The first book, Parts 1-4, addresses fundamental algorithms, data structures, sorting, and searching. A forthcoming third book will focus on strings, geometry, and a range of advanced algorithms. Each book's expanded coverage features new algorithms and implementations, enhanced descriptions and diagrams, and a wealth of new exercises for polishing skills. The natural match between Java classes and abstract data type (ADT) implementations makes the code more broadly useful and relevant for the modern object-oriented programming environment. The Web site for this book (www.cs.princeton.edu/~rs/) provides additional source code for programmers along with a variety of academic support materials for educators. Coverage includes: A complete overview of graph properties and types Diagraphs and DAGs Minimum spanning trees Shortest paths Network flows Diagrams, sample Java code, and detailed algorithm descriptions A landmark revision, Algorithms in Java, Third Edition, Part 5 provides a complete tool set for programmers to implement, debug, and use graph algorithms...

Words: 281 - Pages: 2

Premium Essay

Java

...deliver this program, Accenture is partnering with Udacity, an online university focused on bridging the gap between real-world skills, relevant education, and employment. Participants are awarded scholarships for the Accenture Veteran Technology Training Program, which grants access to Udacity’s Intro to Java Programming online program. Accenture is offering the premium version of the course, where coaches actively support students and students earn a Verified Certificate. In this introductory course, you'll learn and practice essential computer science concepts using the Java programming language. You'll learn about Object Oriented Programming, a technique that allows you to use code written by other programmers in your own programs. You'll put your new Java programming skills to the test by solving real-world problems faced by software engineers. Upon successful completion of the training course, participants will be granted an interview with Accenture for the Entry Level Software Engineering Associate position, which is part of our global network of technology experts. If ultimately hired for this full-time position, you will apply your military background and new Java skills to assist with the development, delivery and management of technology-based business solutions. You will continue to develop new skills, through Accenture’s robust training...

Words: 264 - Pages: 2

Premium Essay

Java

...Mohammad Doush Mr. Matthew Robert English 103 13 April 2013 Java the Programming Language Computer is very important in our live, we use computer in everywhere on our live. The doctor uses the computer to see file or pictures of his patients. Also, each engineer uses it in many ways of his work. The teacher in the classroom, employees in the offices and student in their study all of them use computer in them daily live. They are not using the mouse, the keyboard or the scream. They are using the applications by them these applications in the computer are like the soul in the body. The only way to build these applications is programming. To program we need to know one of the programming languages which are very similar each other. If you are professional in one of these languages you can be professional in the other language in a short period of time. It is acceptable if you have the same application written with Java once and with C++ or C sharp at the same time. So for this reason you cannot say that a programming language is better than others. There are three types of programming languages procedural, functional and object-oriented languages. The most uses of these languages are object-oriented and one of these languages is Java you can write any application you need using it. Also you can translate any application to its word. The message of the High-Level programming languages such as Algol and Pascal in first programming revolution was...

Words: 2352 - Pages: 10

Premium Essay

Java

...Release Team[oR] 2001 [x] java Java 2: The Complete Reference by Patrick Naughton and Herbert Schildt Osborne/McGraw-Hill © 1999, 1108 pages ISBN: 0072119764 This thorough reference reads like a helpful friend. Includes servlets, Swing, and more. Table of Contents Back Cover Synopsis by Rebecca Rohan Java 2: The Complete Reference blends the expertise found in Java 1: The Complete Reference with Java 2 topics such as "servlets" and "Swing." As before, there's help with Java Beans and migrating from C++ to Java. A special chapter gives networking basics and breaks out networking-related classes. This book helps you master techniques by doing as well as reading. Projects include a multi-player word game with attention paid to network security. The book is updated where appropriate throughout, and the rhythm of text, code, tables, and illustrations is superb. It's a valuable resource for the developer who is elbow-deep in demanding projects. Table of Contents Java 2 Preface - 7 Part l The Java Language - The Complete Reference - 4 Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 hapter 10 - The Genesis of Java - 9 - An Overview of Java - 20 - Data Types, Variables, and Arrays - 36 - Operators - 57 - Control Statements - 75 - Introducing Classes - 94 - A Closer Look at Methods and Classes - 111 - Inheritance - 134 - Packages and Interfaces - 156 - Exception Handling - 174 Chapter 11 - Multithreaded Programming...

Words: 78285 - Pages: 314

Premium Essay

Java

...JMaster list of Java interview questions - 115 questions By admin | July 18, 2005 115 questions total, not for the weak. Covers everything from basics to JDBC connectivity, AWT and JSP. 1. What is the difference between procedural and object-oriented programs?- a) In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code. b) In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code. 2. What are Encapsulation, Inheritance and Polymorphism?- Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse. Inheritance is the process by which one object acquires the properties of another object. Polymorphism is the feature that allows one interface to be used for general class actions. 3. What is the difference between Assignment and Initialization?- Assignment can be done as many times as desired whereas initialization can be done only once. 4. What is OOPs?- Object oriented programming organizes a program around its data, i. e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling 5. access to code. What are Class, Constructor...

Words: 6762 - Pages: 28

Premium Essay

Java

...tutorial explains the installation and usage of the Java programming language. It also contains examples for standard programming tasks. 1. Introduction to Java 1.1. History Java is a programming language created by James Gosling from Sun Microsystems in 1991. The first publicly available version of Java (Java 1.0) was released in 1995. Over time new enhanced versions of Java have been released. The current version of Java is Java 1.7 which is also known as Java 7. From the Java programming language the Java platform evolved. The Java platform allows that the program code is written in other languages than the Java programming language and still runs on the Java virtual machine. 1.2. Java Virtual machine The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine. The Java virtual machine is written specifically for a specific operating system, e.g. for Linux a special implementation is required as well as for Windows. Java programs are compiled by the Java compiler into so-called bytecode. The Java virtual machine interprets this bytecode and executes the Java program. 1.3. Java Runtime Environment vs. Java Development Kit Java comes in two flavors, the Java Runtime Environment (JRE) and the Java Development Kit (JDK). The Java runtime environment (JRE) consists of the JVM and the Java class libraries and contains the necessary functionality to start Java programs. The JDK contains in addition the...

Words: 662 - Pages: 3

Premium Essay

Java

...A Comparison between Java and .Net Languages Introduction Java and .Net provide technologies that enable skilled developers to build quality enterprise applications. These technologies are rarely picked based on performance alone. There are many factors to consider when choosing Java or .Net. These considerations are often the deciding factor when choosing one or both of these platforms. Java Java is kenned as both a programming language and a development platform. It was first developed by Sun Microsystems in 1991 and subsequently relinquished in 1995. To help to make the language more accepted and accessible, Sun Microsystems developed it as an object oriented language with a syntax that is very similar to C++. (Java vs. .NET, 2007) Sun Microsystems decided to create this new platform out of a desire to be able to write programs only once that could be run on any system. (James) The Java 2 platform was launched in December 1998. This was a major amelioration of the platform, and included incipient graphics, user interface, and enterprise capabilities. This upgrade was over seven times as large as the initial Java 1.0 release and marked the maturity of the Java platform. (What is java?) Within the Java 2 platform there are 3 editions: • The Java 2 Standard Edition (J2SE) Provides the essential compiler, tools, runtimes, and APIs for writing, deploying, and running applets and applications. • The Java 2 Enterprise Edition (J2EE) Defines a standard for developing multi-tier...

Words: 2279 - Pages: 10

Premium Essay

Java

...November 30, 2011 Java Security Jessica Shaw: 628 Robert Grimsley: 596 Java is a programming language developed by Sun Microsystems in 1995, which is now called Oracle. The language itself is derived from the languages C and C++. Java is a simple language compared to C and C++; however they are all object-oriented languages. The language was designed to help minimize the amount of space and take up as little of your computer’s hardware resources as possible. The language was designed upon five key goals, and they are as followed: * It should be "simple, object-oriented and familiar" * It should be "robust and secure" * It should be "architecture-neutral and portable" * It should execute with "high performance" * It should be "interpreted, threaded, and dynamic" Java is used for a multitude of things. Java allows one to play virtual video games, view and design 3D photos, and many other interactive topics on the internet. Java is the main programming language for mobile devices as well as smart phones such as Android by google. Java is a very secure and reliable language that is used by over 800 billion people. Without the Java languages there are millions of applets and interactive applications on websites that wouldn’t preform properly without it. Java is compiled by byte code that allows for the program to be run through the Java Virtual Machine, or JVM. The compiling method allows for...

Words: 2557 - Pages: 11

Free Essay

Java

...1. Lab Exercise for Core Java Section A: a) Write a program to find HCF of two number reading inputs as command line argument. b) Write a program to display all prime numbers between two limits. c) Write a program to implement below diamond pattern. * *** ***** d) Write a program to implement string concatenation, finding string length, find sub string and check the given string is palindrome or not? e) Write a program to implement constructor overloading by passing different number of parameter of different types. f) Write a program to find area of Square and rectangle figures using method. g) Write a program to calculate bonus for different departments using method overriding. h) Write a program to perform the different arithmetic operation show the exception handling. i) Write a program to implement thread priorities. j) Write a program to create a linked list using LinkedList class of Collection Framework k) Write a program, to create a Frame Window in an applet. l) Write a program to create simple student registration using applet, read the input using text boxes and display the output once user click on the button. m) Write a program to draw different polygon by using AWT supported Graphics method n) Write a program to implement the appropriate interface in the key listener so that it will receive the type of event. ASSIGNMENT EXCEPTION Exception and exception handling -introduction, types of errors...

Words: 275 - Pages: 2

Premium Essay

Java for Dummies

...Programming Languages/Java ™ Jumpin’ Java! The bestselling Java beginner’s book is now fully updated for Java 7! Open the book and find: ava J • Definitions of the many terms you’ll encounter ® • The grammar of Java • How to save time by reusing code • All about if, for, switch, and while statements • An overview of object-oriented programming • Building blocks — learn to work with Java classes and methods and add comments • Hints about handling exceptions • How to write Java applets ® • The Java scoop — get an overview of Java, the enhancements in Java 7, and the software tools you need • Get loopy — understand the value of variables and learn to control program flow with loops or decision-making statements 5th Edition 5th Edition Java Java, the object-oriented programming language that works on almost any computer, is what powers many of those cool multimedia applications. Thousands have learned Java programming from previous editions of this book — now it’s your turn! Whether you’re new to programming or already know a little Visual Basic or C++, you’ll be doing Java in a jiffy. g Easier! Making Everythin • Ten ways to avoid mistakes • Class it up — explore classes and objects, constructors, and subclasses, and see how to reuse your code • A click ahead — experiment with variables and methods, use arrays and collections to juggle values, and create programs that respond to mouse clicks Learn...

Words: 34460 - Pages: 138

Free Essay

Java Basics

...1 Learn Java/J2EE core concepts and key areas With Java/J2EE Job Interview Companion By K.Arulkumaran & A.Sivayini Technical Reviewers Craig Malone Stuart Watson Arulazi Dhesiaseelan Lara D’Albreo Cover Design, Layout, & Editing A.Sivayini Acknowledgements A. Sivayini Mr. & Mrs. R. Kumaraswamipillai 2 Java/J2EE Job Interview Companion Copy Right 2005-2007 ISBN 978-1-4116-6824-9 The author has made every effort in the preparation of this book to ensure the accuracy of the information. However, information in this book is sold without warranty either expressed or implied. The author will not be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Please e-mail feedback & corrections (technical, grammatical and/or spelling) to java-interview@hotmail.com First Edition (220+ Q&A): Dec 2005 Second Edition (400+ Q&A): March 2007 3 Outline SECTION DESCRIPTION What this book will do for you? Motivation for this book Key Areas index SECTION 1 Interview questions and answers on: Java Fundamentals Swing Applet Performance and Memory issues Personal and Behavioral/Situational Behaving right in an interview Key Points SECTION 2 Interview questions and answers on: Enterprise Java J2EE Overview Servlet JSP JDBC / JTA JNDI / LDAP RMI EJB JMS XML SQL, Database, and O/R mapping RUP & UML Struts Web and Application servers. Best practices and performance considerations. Testing and deployment. Personal and...

Words: 23255 - Pages: 94

Free Essay

Dr Java

...A Quick Start Guide to DrJava A Quick Start Guide to DrJava Table of Contents 1. Introduction ................................................................................................................... 1 2. Getting Ready to Use DrJava ............................................................................................ 2 Downloading the JDK ................................................................................................. 2 Downloading DrJava ................................................................................................... 3 3. Using DrJava, the Basics ................................................................................................. 5 Running DrJava ......................................................................................................... 5 Opening and Creating Files .......................................................................................... 5 Saving Files .............................................................................................................. 6 Compiling Files .......................................................................................................... 6 The Interactions Pane .................................................................................................. 8 System.in .................................................................................................................. 9 Find and Replace ..........................

Words: 8211 - Pages: 33