CPCS202 01 Introduction S19

Download as pdf or txt
Download as pdf or txt
You are on page 1of 90

Chapter 1:

Introduction to
Programming and Java
Objectives
To review Program Design and Problem-Solving Techniques
To describe the relationship between Java and the World Wide Web (§1.5).
To understand the meaning of Java language specification, API, JDK, and IDE
(§1.6).
To write a simple Java program (§1.7).
To display output on the console (§1.7).
To explain the basic syntax of a Java program (§1.7).
To create, compile, and run Java programs (§1.8).
To use sound Java programming style and document programs properly (§1.9).
To explain the differences between syntax errors, runtime errors, and logic
errors (§1.10).
To develop Java programs using NetBeans (§1.11).

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 2


What is Computer Science?
Computer Science can be summarized with
two simple words: problem solving.
Computer Science is the study of problems,
problem-solving, and the solutions that come
out of this problem-solving process.
Given a problem, the goal is to develop an
algorithm to solve the problem.
An algorithm is a step-by-step list of
instructions to solve the problem.
© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 3
Algorithm
Algorithm: a step-by-step series of
instructions to complete a task
Similar to a recipe!
a step-by-step series of instructions to
prepare a specific food
Such as Mandi!
So you can think of a recipe as a type of
algorithm that is specific for making food.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 4


What is Programming?
Once you have developed the algorithm on
paper, you must now “prove it” and show
that it works.
Programming is the process of encoding your
algorithm into a programming language, so
that it can then be executed by a computer.
But what is the first step?
You need a solution.
This means you need an algorithm!
© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 5
So who is good at Programming?
Are you good at problem solving?
Are you good at strategy?
These are the core fundamentals of
programming.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 6


Program Design &
Problem-Solving
Techniques
How Do We Write a Program?
◼ A Computer is not intelligent.
◼ It cannot analyze a problem and come up with a solution.
◼ A human (the programmer) must analyze the problem, develop
the instructions for solving the problem, and then have the
computer carry out the instructions.
◼ To write a program for a computer to follow, we must go through a
two-phase process: problem solving and implementation.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 8


Problem-Solving Phase (PSP)
◼ Analysis and Specification- Understand (define) the
problem and what the solution must do.
◼ General Solution (Algorithm)- Specify the required
data types and the logical sequences of steps that
solve the problem.
◼ Verify- Follow the steps exactly to see if the solution
really does solve the problem.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 9


Implementation Phase
◼ Concrete Solution (Program)- Translate the
algorithm (the general solution) into a programming
language.
◼ Test- Have the computer follow the instructions.
◼ Then manually check the results.
◼ If you find errors, analyze the program and the algorithm to determine the source of the
errors, and then make corrections.

◼ Once a program is tested, it enters into next phase


(maintenance).
◼ Maintenance requires Modification of the program to
meet changing requirements or to correct any errors
that show up while using it.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 10


Steps in Program
Development
Steps in Program Development
1. Define the problem into three separate
components:
• inputs
• processing steps to produce required outputs.
• outputs

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 12


Steps in Program Development

2. Outline the solution.


• Decompose the problem to smaller steps.
• Establish a solution outline.

3. Develop the outline into an algorithm.


• The solution outline is now expanded into an
algorithm.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 13


Steps in Program Development
4. Test the algorithm for correctness.
• Very important in the development of a program,
but often forgotten
• Major logic errors can be detected and corrected
at an early stage.

5. Code the algorithm into a specific


programming language.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 14


Steps in Program Development
6. Run the program on the computer.
• This step uses a program compiler and
programmer-designed test data to machine-
test the code for
• syntax errors
• logic errors

7. Document and maintain the program.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 15


Algorithms & Flowcharts
Algorithm

What is an algorithm?
– A step-by-step series of instructions in order to
perform a specific task.
– An algorithm must:
Be lucid (clear), precise and unambiguous
Give the correct solution in all cases, and eventually end

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 17


Flowchart
• A graphical representation of the sequence of
operations in an information system or program.

• Program flowcharts show the sequence of


instructions in a single program or subroutine.
• shows logic of an algorithm
• emphasizes individual steps and their
interconnections
• e.g. control flow from one action to the next
–Note: Different symbols are used to draw each
type of flowchart.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 18


Flowchart Symbols
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition , subtraction , division etc .

Diamond Denotes a decision (or branch ) to be made .


The program should continue along one of
two routes . (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 19


Flowcharts
Are Flowcharts really necessary or helpful?
– In the real world, programs are not only 1000 lines
– Programs are hundreds of thousands of lines of code
Even Millions of lines of code
– Could you use only English to describe your
program?
Sure you could, but you would end up with a book!
– Therefore, think of Flowcharts as an easier, clearer
way to quickly understand what a program is doing.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 20


Example 1
Write an algorithm to determine a student’s
final grade and indicate whether it is
passing or failing. The final grade is
calculated as the average of four marks.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 21


Example 1
Algorithm:
➢ Input a set of 4 marks
➢ Calculate their average by summing and dividing by 4
➢ If average is below 60
Print “FAIL”
Else
Print “PASS”

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 22


Example 2
• Write an Algorithm and draw a flowchart to
convert the length in feet to centimeter.

Algorithm:
• Input the length in feet (LFT)
• Calculate the length in cm (LCM) by
multiplying LFT with 30
• Print length in cm (LCM)

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 23


Example 2
Pseudocode: Flowchart
• Step 1: Input LFT START

• Step 2: LCM  LFT x 30 Input


LFT
• Step 3: Print LCM

LCM  LFT x 30
Print
LCM

STOP

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 24


Example 3
Write an Algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.

Algorithm:
• Input the Length (L) and width (W) of a
rectangle
• Calculate the area (A) by multiplying L with W
• Print A

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 25


Example 3
Flowchart

START

Input
L,W

A LxW

Print
A

STOP

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 26


Example 4

• Write a flowchart that reads two values, determines the


largest value and prints the largest value with an
identifying message.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 27


Decision Structures
• The expression A>B is a logical expression
• it describes a condition we want to test
• if A>B is true (if A is greater than B) we take the
action on left
• print the value of A
• if A>B is false (if A is not greater than B) we
take the action on right
• print the value of B

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 28


Decision Structures

Y is N
A>B
Print A Print B

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 29


Example 4
START

Input
VALUE1,VALUE2

Y is
N
VALUE1>VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”, MAX

STOP

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 30


Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Or >= Greater than or equal to
 Or <= Less than or equal to
 Or != Not equal to

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 31


Programs
Computer programs, known as software, are instructions to
the computer.

You tell a computer what to do through programs. Without


programs, a computer is an empty machine. Computers do
not understand human languages, so you need to use
computer languages to communicate with them.

Programs are written using programming languages.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 32


Programming Languages
Machine Language Assembly Language High-Level Language

Machine language is a set of primitive instructions


built into every computer. The instructions are in
the form of binary code, so you have to enter binary
codes for various instructions. Program with native
machine language is a tedious process. Moreover
the programs are highly difficult to read and
modify. For example, to add two numbers, you
might write an instruction in binary like this:

1101101010011010

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 33


Programming Languages
Machine Language Assembly Language High-Level Language

Assembly languages were developed to make


programming easy. Since the computer cannot understand
assembly language, however, a program called assembler is
used to convert assembly language programs into machine
code. For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 34


Programming Languages
Machine Language Assembly Language High-Level Language

The high-level languages are English-like and easy to learn


and program. For example, the following is a high-level
language statement that computes the area of a circle with
radius 5:
area = 5 * 5 * 3.1415;

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 35


Popular High-Level Languages
Language Description

Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is used mainly in defense projects.
BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily
by beginners.
C Developed at Bell Laboratories. C combines the power of an assembly language with the ease of
use and portability of a high-level language.
C++ C++ is an object-oriented language, based on C.
C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.
COBOL COmmon Business Oriented Language. Used for business applications.
FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications.
Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-
independent Internet applications.
Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a
simple, structured, general-purpose language primarily for teaching programming.
Python A simple general-purpose scripting language good for writing short programs.
Visual Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop
Basic graphical user interfaces.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 36


Interpreting/Compiling Source Code
• A program written in a high-level language is
called a source program or source code.
• Because a computer cannot understand a source
program, a source program must be translated
into machine code for execution.
• The translation can be done using another
programming tool called an interpreter or a
compiler.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 37


Interpreting Source Code
An interpreter reads one statement from the source
code, translates it to the machine code or virtual
machine code, and then executes it right away, as
shown in the following figure. Note that a statement
from the source code may be translated into several
machine instructions.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 38


Compiling Source Code
A compiler translates the entire source code into a
machine-code file, and the machine-code file is
then executed, as shown in the following figure.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 39


Why Java?
The answer is that Java enables users to develop and
deploy applications on the Internet for servers, desktop
computers, and small hand-held devices. The future of
computing is being profoundly influenced by the Internet,
and Java promises to remain a big part of that future. Java
is the Internet programming language.

Java is a general purpose programming language.


Java is the Internet programming language.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 40


Java, Web, and Beyond
Java can be used to develop standalone
applications.
Java can be used to develop applications
running from a browser.
Java can also be used to develop applications
for hand-held devices.
Java can be used to develop applications for
Web servers.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 41


Companion
Website Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
www.cs.armstrong.edu/liang/JavaCharacteristics.pdf
© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 42
Companion
Website Characteristics of Java
Java Is Simple Java is partially modeled on C++, but greatly
simplified and improved. Some people refer to
Java Is Object-Oriented Java as "C++--" because it is like C++ but
Java Is Distributed with more functionality and fewer negative
aspects.
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 43


Companion
Website Characteristics of Java
Java Is Simple Java is inherently object-oriented.
Although many object-oriented languages
Java Is Object-Oriented began strictly as procedural languages,
Java Is Distributed Java was designed from the start to be
object-oriented. Object-oriented
Java Is Interpreted programming (OOP) is a popular
Java Is Robust programming approach that is replacing
Java Is Secure traditional procedural programming
techniques.
Java Is Architecture-Neutral
Java Is Portable One of the central issues in software
development is how to reuse code. Object-
Java's Performance oriented programming provides great
Java Is Multithreaded flexibility, modularity, clarity, and
reusability through encapsulation,
Java Is Dynamic inheritance, and polymorphism.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 44


Companion
Website Characteristics of Java
Java Is Simple Distributed computing involves several
computers working together on a network.
Java Is Object-Oriented Java is designed to make distributed
Java Is Distributed computing easy. Since networking
capability is inherently integrated into
Java Is Interpreted Java, writing network programs is like
Java Is Robust sending and receiving data to and from a
file.
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 45


Companion
Website Characteristics of Java
Java Is Simple You need an interpreter to run Java
programs. The programs are compiled into
Java Is Object-Oriented the Java Virtual Machine code called
Java Is Distributed bytecode. The bytecode is machine-
independent and can run on any machine
Java Is Interpreted that has a Java interpreter, which is part of
Java Is Robust the Java Virtual Machine (JVM).
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 46


Companion
Website Characteristics of Java
Java Is Simple Java compilers can detect many problems
that would first show up at execution time
Java Is Object-Oriented in other languages.
Java Is Distributed
Java has eliminated certain types of error-
Java Is Interpreted prone programming constructs found in
Java Is Robust other languages.
Java Is Secure
Java has a runtime exception-handling
Java Is Architecture-Neutral feature to provide programming support
Java Is Portable for robustness.

Java's Performance
Java Is Multithreaded
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 47


Companion
Website Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java implements several security
Java Is Robust mechanisms to protect your system against
Java Is Secure harm caused by stray programs.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 48


Companion
Website Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral Write once, run anywhere
Java Is Portable With a Java Virtual Machine (JVM),
Java's Performance you can write one program that will
run on any platform.
Java Is Multithreaded
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 49


Companion
Website Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable Because Java is architecture neutral,
Java programs are portable. They can
Java's Performance be run on any platform without being
Java Is Multithreaded recompiled.
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 50


Companion
Website Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable Java’s performance Because Java is
architecture neutral, Java programs are
Java's Performance portable. They can be run on any
Java Is Multithreaded platform without being recompiled.
Java Is Dynamic

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 51


Companion
Website Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance Multithread programming is smoothly
Java Is Multithreaded integrated in Java, whereas in other
languages you have to call procedures
Java Is Dynamic
specific to the operating system to enable
multithreading.
© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 52
Companion
Website Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance Java was designed to adapt to an evolving
environment. New code can be loaded on the
Java Is Multithreaded fly without recompilation. There is no need for
developers to create, and for users to install,
Java Is Dynamic major new software versions. New features can
be incorporated transparently as needed.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 53


JDK Versions
JDK 1.02 (1995)
JDK 1.1 (1996)
JDK 1.2 (1998)
JDK 1.3 (2000)
JDK 1.4 (2002)
JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
JDK 1.7 (2011) a. k. a. JDK 7 or Java 7
JDK 1.8 (2014) a. k. a. JDK 8 or Java 8

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 54


JDK Editions
Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications
such as Java servlets, Java ServerPages, and Java
ServerFaces.
Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
This book uses J2SE to introduce Java programming.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 55


Popular Java IDEs
NetBeans
Eclipse

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 56


Developing Java Programs
Using NetBeans
NetBeans is a free IDE for developing Java
Programs
What is an IDE?
Stands for “Integrated Development Environment”
An IDE is a software application that provides
computer programmers a suitable environment for
software development

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 57


Compiling Java Source Code
You can port a source program to any machine with appropriate
compilers. The source program must be recompiled, however, because
the object program can only run on a specific machine. Nowadays
computers are networked to work together. Java was designed to run
object programs on any platform. With Java, you write the program
once, and compile the source program into a special type of object
code, known as bytecode. The bytecode can then run on any computer
with a Java Virtual Machine, as shown below. Java Virtual Machine is
a software that interprets Java bytecode.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 58


animation

Trace a Program Execution


Enter main method

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 59


animation

Trace a Program Execution


Execute statement

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 60


animation

Trace a Program Execution

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

print a message to the


console

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 61


Anatomy of a Java Program
Class name
Main method
Statements
Statement terminator
Reserved words
Comments
Blocks

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 62


Class Name
Every Java program must have at least one class.
Each class has a name. By convention, class names
start with an uppercase letter. In this example, the
class name is Welcome.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 63


Main Method
Line 2 defines the main method. In order to run a
class, the class must contain a method named main.
The program is executed from the main method.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 64


Statement
A statement represents an action or a sequence of actions.
The statement System.out.println("Welcome to Java!") in
the program in Listing 1.1 is a statement to display the
greeting "Welcome to Java!“.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 65


Statement Terminator
Every statement in Java ends with a semicolon (;).

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 66


Reserved words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the word
after class is the name for the class.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 67


Blocks
A pair of braces in a program forms a block that groups
components of a program.

p
ubl
iccla
ssTes
t{
pu
bli
cstat
icvoi
dmain
(St
ring
[]a
rgs
){ C
la
ssb
loc
k
Sys
tem
.ou
t.p
rin
tln
("W
elc
ometoJ
ava
!")
;Me
tho
dblo
ck
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 68


Special Symbols

Character Name Description

{} Opening and closing Denotes a block to enclose statements.


braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.

" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 69


{ …}

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 70


( … )

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 71


;

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 72


// …

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 73


"…"

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 74


Programming Style and
Documentation
Appropriate Comments
Naming Conventions
Proper Indentation and Spacing Lines
Block Styles

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 75


Appropriate Comments
Include a summary at the beginning of the
program to explain what the program does, its key
features, its supporting data structures, and any
unique techniques it uses.

Include your name, class section, instructor, date,


and a brief description at the beginning of the
program.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 76


Comments
Three types of comments in Java.

Line comment: A line comment is preceded by two


slashes (//) in a line.
Paragraph comment: A paragraph comment is enclosed
between /* and */ in one or multiple lines.

javadoc comment: javadoc comments begin with /**


and end with */. They are used for documenting
classes, data, and methods. They can be extracted into
an HTML file using JDK's javadoc command.
© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 77
Naming Conventions
Choose meaningful and descriptive names.
Class names:
– Capitalize the first letter of each word in the
name. For example, the class name
ComputeExpression.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 78


Proper Indentation and Spacing
Indentation
– Indent using a tab
– Or with three to five spaces.
– Be CONSISTENT!

Spacing
– Use blank line to separate segments of the code.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 79


Block Styles
Use end-of-line style for braces.

N e
xt-lin
e p
ubl
iccla
ssTes
t
sty
le {
pu
bli
cstat
icvoi
dmain
(St
rin
g[]arg
s)
{
Sys
tem
.ou
t.p
rin
tln
("B
loc
kStyle
s")
;
}
}

E n
d-of-lin
e
sty
le
p
ubl
iccla
ssTes
t{
pu
bli
cstat
icvoi
dmain
(St
rin
g[]arg
s){
Sys
tem
.ou
t.p
rin
tln
("B
loc
kStyle
s")
;
}
}

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 80


Programming Errors
Syntax Errors
– Errors that are detected by the compiler
–Examples: mistyping a keyword, using an
opening brace but not the closing, etc.
–Usually easy to detect, because the compiler
tells you where the error is and what caused
them.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 81


Programming Errors
Syntax Error Example:

• The keyword void is missing before main in line 2.

• The string Welcome to Java should be closed


with a closing quotation mark in line 3.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 82


Programming Errors
Syntax Error Example:
Since a single error will often display many
lines of compile errors, it is a good practice to
fix errors from the top line and work
downward.
Fixing errors that occur earlier in the program
may also fix additional errors that occur later.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 83


Programming Errors
Runtime Errors
– Causes the program to terminate in an
abnormal way
–Known as “crashing” or “my program
crashed”
– Often caused by input mistakes, where the user
enters a value the program cannot handle
– Another example: divide by zero

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 84


Programming Errors
Runtime Error Example

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 85


Programming Errors
Logic Errors
– Produces incorrect result
– Program does not run the way we intended
– Usually the result of logical mistakes
– In fact, the program “works”, but the output is
wrong due to our logical mistake.
– These errors are harder to detect.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 86


Programming Errors
Common Errors
– Missing a closing brace, missing a semicolon,
missing quotation marks for strings, and
misspelling names are common errors for new
programmers.

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 87


Programming Errors
Common Errors
– Missing a semicolon:

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 88


Programming Errors
Common Errors
– Missing quotation marks:

– Thankfully, your IDE (such as NetBeans)


will insert the closing quotation marks
automatically

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 89


Programming Errors
Common Errors
– Misspelling Names:

– main is misspelled as Main


– String is misspelled as string

© Dr. Jonathan Cazalas Chapter 1: Introduction to Programming and Java page 90

You might also like