01 Introduction
01 Introduction
INTRODUCTIO
N
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
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
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 11
Your First Program
Traditional ‘Hello World’ program in Java
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
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!
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 15
1.5 Analyzing your First Program
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
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!”);
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
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 21
Logic Errors
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 24
Copyright © 2013 by John Wiley & Sons. All rights reserved. Page 25