CHAPTER 1
INTRODUCTIO
N
Slides by Donald W. Smith Final Draft
Copyright © 2013 by John Wiley & Sons. All rights reserved. TechNeTrain.com Oct. 15, 2011
Chapter Goals
To learn about computers and programming
To compile and run your first Java program
To recognize compile-time and run-time
errors
To describe an algorithm with pseudocode
In this chapter, you will learn how to write
and run your first Java program. You will
also learn how to diagnose and fix
programming errors, and how to use
pseudocode to describe an algorithm.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 2
Contents
Computer Programs
The Java Programming Language
Becoming Familiar with your Programming
Environment
Analyzing Your First Program
Errors
Problem Solving:
Algorithm Design
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 3
1.1 Computer Programs
A Computer Program is a sequence of
instructions and decisions
Computers execute very basic instructions
in rapid succession
Programming is the act of designing and
implementing computer programs
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 4
1.2 The Anatomy of a Computer
The central processing unit (CPU)
performs program control and data
processing
Storage devices include memory
(RAM) and secondary storage
Hard disk
Flash drives
CD/DVD drives
Input/Output devices allow the user to
interact with the computer
Mouse, keyboard, printer, screen…
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 5
1.3 The Java Language
In 1991, James Gosling of Sun
Microsystems designed what
would become the Java
programming language
Java was originally designed for
programming consumer devices,
but it was first successfully used
to write Internet applets
An applet is typically embedded
inside a web page and runs in the
context of a browser
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 6
Java Virtual Machines
Source code
Portable ‘byte code’
The compiler generates
byte code in a ‘class’ file
which can be run on any
Java Virtual Machine
Oracle Tutorials
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 7
The Java API
The Java Platform consists of two parts:
1) Java Virtual Machine
2) Java API
-- also called libraries
The Application Programming Interface
(API) is a huge collection of handy software
packages that programmers can use:
Graphics, user interface, networking, sound,
database, math, and many more
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 8
The Java SDK
You need to install the Java SDK (Software
Development Kit) to create Java programs
Your instructor will suggest one to start with
Google ‘Java SDK download,’ Get SE version
Location after installed on Windows will be:
• C:\Program Files\Java\jdk1.7.x
• The last few numbers may vary with releases
The SDK includes programs such as:
java.exe (Executes Java applications)
javac.exe (Java compiler)
javadoc.exe (Javadoc generator)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 9
1.4 Programming Environment
There are many free programming tools available for Java
Your instructor will suggest one to start with
Components of an Integrated Development Environment (IDE):
Source code editor helps programming by:
• Listing line numbers of code
• Color lines of code (comments, text…)
• Auto-indent source code
Output window
Debugger
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 10
An Example IDE
Editor
Output
Many IDEs are designed specifically for Java programming
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 11
Your First Program
Traditional ‘Hello World’ program in Java
We will examine this program in the next section
Typing it into your IDE would be good practice!
Be careful of spelling
JaVa iS CaSe SeNsItiVe
Java uses special characters, e.g. { } ( ) ;
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 12
Text Editor Programming
You can also use a simple text editor such as Notepad to write your source code
Once saved as HelloPrinter.java, you can use a console window to:
1) Compile the program
2) Run the program
Compile
Output Execute
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 13
Source Code to Running Program
The compiler generates the .class file which
contains instructions for the Java Virtual machine
Class files contain ‘byte code’ that you cannot edit
D:\temp\hello>Type HelloPrinter.class
╩■║╛ 2 ↔ ♠ ☼ ► ↕ ‼ ¶ § ▬☺ ♠<init>☺ ♥()V☺ ♦Code☺
☼LineNumberTable☺ ♦main▬([Ljava/lang/String;)V☺
Hello, World! elloPrinter.java♀ ↨♀ ↑ ↓☺
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 14
Organize your work
Your ‘source code’ is stored
in .java files
Create one folder per program
Can be many .java files
Be sure you know where your
IDE stores your files!
Backup your work!
Backup your work to a Flash Drive, external
hard drive, or network drive that is backed up
nightly.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 15
1.5 Analyzing your First Program
1: Declares a ‘class’ HelloPrinter
-- Every Java program has one or more classes.
3: Declares a method called ‘main’
-- Every Java application has exactly one ‘ main’ method
-- Entry point where the program starts
5: Method System.out.println outputs ‘Hello, World!’
-- A statement must end with a semicolon (;)
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 16
Syntax 1.1: The Java Program
Every application has the same basic layout
Add your ‘code’ inside the main method
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 17
Calling Java Library methods
Line 5 shows how to ‘call’ a ‘method’ from the Java API:
System.out.println
Code that somebody else wrote for you!
Notice the dots (periods)
Parenthesis surround the arguments that you ‘pass’ to a method
We are passing a String “Hello World”
• Note the double quotes which denote a String inside
You can also print numerical values
• System.out.println(3 + 4);
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 18
Getting to know println
The println method prints a string or a number and then
starts a new line.
System.out.println("Hello" Hello
); World!
System.out.println("Worl
println has a ‘cousin’ method named
d!”);
print that does not print a new line.
System.out.print("00"); 007
00
System.out.println(3+4);
A method is called by specifying
the method and its agruments
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 19
1.6 Errors
The Two Categories of Errors:
1) Compile-time Errors
• Syntax Errors
– Spelling, Capitalization, punctuation
– Ordering of statements, matching of braces/parenthesis…
• No .class file is generated by the compiler
• Correct first error listed, then compile again
2) Run-time Errors
• Logic Errors
• Program runs, but produces unintended results
• Program may ‘crash’
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 20
Syntax Errors
What happens if you
Misspell a word: System.ou.println
Don’t Capitalize a word system.out.println
Leave out a word void
Forget a Semicolon after ("Hello, World!")
Don’t match a curly brace? Remove line 6
Try it to see what error messages are generated
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 21
Logic Errors
What happens if you
Divide by Zero
System.out.println(1/0);
Mis-spell output ("Hello, Word!")
Forget to output Remove line 5
Programs will compile and run
The output may not be as expected
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 22
1.7 Problem Solving: Algorithm Design
Algorithms are simply plans
Detailed plans that describe the steps to solve
a specific problem
You already know quite a few
Calculate the area of a circle
Find the length of the hypotenuse of a triangle
Some problems are more complex and
require more steps
Calculate PI to 100 decimal places
Calculate the trajectory of a missile
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 23
Algorithm Defined
An algorithm describes a
sequence of steps that is:
Unambiguous
• Do not require ‘assumptions’
• Uses precise instructions
Executable
• Can be carried out in practice
Terminating
• Will eventually come to an end
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 24
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 25