Topic1 Programming Concepts
Topic1 Programming Concepts
◦ software - programs in which contents invisible instructions that control the hardware
and make it perform specific tasks.
◦ You can learn a programming language without knowing computer hardware, but
you will be better able to understand the effect of the instructions in the program if
you do.
What is a Computer?
A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer,
and communication devices.
4
The Hardware
⚫ ASCII encoding:
◦ ‘a’ is represented by 01100001
◦ ‘b’ is encoded as 01100010
12
Programs
◦ Computer programs, known as software, are instructions to
the computer.
◦ Are machine-dependent
● each computer type has its own machine language.
◦ Must use language translators, called assemblers, to convert them to machine code.
◦ Disadvantages:
◦ In general, each assembly language instruction corresponds to one machine
language instruction. Therefore, the programs written in them are lengthy.
◦ Because of variations in assembly languages, programs written using them are not
portable.
18
Interpreting/Compiling Source Code
◦ Since a computer cannot understand a source program, a source program must be translated
into machine code for execution.
◦ A compiler is a program used to translate the source program into a machine language
program called an object program.
◦ The object program is often then linked with other supporting library code before the object
can be executed on the machine.
◦ 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.
20
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.
21
⚫ Operating Systems:
◦ The operating system (OS) is a program that
manages and controls a computer’s activities.
◦ Java Applets
● Java programs that run from a Web Browser
● Applets use a modern graphical user interface with buttons, text fields etc.
to interact with users on the web and process their request.
◦ Java can also be used to develop applications for hand-held devices such as
Palm and cell phones
�
⚫ Java’s History
◦ James Gosling and Sun Microsystems �
◦ Originally called Oak
◦ Java, May 20, 1995, Sun World
◦ HotJava
●The first Java-enabled Web browser
◦ Early History Website:
●http://java.sun.com/features/1998/05/birthday
.html
�
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 26
⚫ Java Is Dynamic
www.cs.armstrong.edu/liang/JavaCharacteristics.pdf
⚫ The Java Language Specification, API, JDK and IDE
◦ The Java language specification is a technical definition of
the language that includes the syntax and semantics of the
Java programming language.
⚫ Major changes
◦ J2SE 1.2 (a.k.a JDK 1.2, 1998) – codename Playground
◦ J2SE 1.3 (a.k.a JDK 1.3, 2000) – codename Kestrel
◦ J2SE 1.4 (a.k.a JDK 1.4, 2002) – codename Merlin
◦ J2SE 5.0 (a.k.a JDK 1.5, 2004) – codename Tiger
◦ Java SE 6 (a.k.a JDK 6, 2009) – codename Mustang
◦ Java SE 7 (a.k.a JDK 7, 2011) – codename Dolphin
◦ Java SE 8 (a.k.a JDK 8, 2014)
Java SE Java SE Version
Version Version Release Date Version Number Release Date
Number
Java SE 9 9 September, 21st 2017
JDK 1.0 1.0 January 1996
(Oak)
Java SE 10 10 March, 20th 2018
Ref : https://www.codejava.net/java-se/java-se-versions-history
⚫ JDK Editions
◦ Java Development Toolkit (JDK) consists of a set of separate
programs for developing and testing Java programs.
◦ Editing, compiling, building, debugging, and online help are integrated in one
graphical user interface.
◦ Just enter source code in one window or open an existing file in a window,
then click a button, menu item, or function key to compile and run the
program.
A Simple Program (IDE tool – eclipse)
⚫ Example
A Simple Program (IDE tool – BlueJ)
⚫ Example https://www.bluej.org/
Creating and Editing Using
NotePad
DOS prompt – use command cmd
To use NotePad, type command
notepad Welcome.java
Creating and Editing Using WordPad
To use WordPad, type
write Welcome.java
from the DOS prompt.
35
⚫ Creating, Compiling, and Running Programs
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 37
shown below.
Java Virtual Machine is a software that
interprets Java bytecode.
animation
38
animation
Execute statement
39
animation
40
Anatomy of a Java Program
⚫ Comments
⚫ Package
⚫ Reserved words
⚫ Modifiers
⚫ Statements
⚫ Blocks
⚫ Classes
⚫ Methods
⚫ The main method
Comments
Comments are ignored by the compiler.
//… (one line), or
/* …. */ (for one or multiple lines)
◦ When the compiler sees //, it ignores all text after // in the
same line.
◦ When it sees /*, it scans for the next */ and ignores any text
between /* and */.
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
42
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.
[] Brackets Array
50
Programming Or Implementation Phase
⚫ Transcribing the logical flow of solution steps in flowchart or algorithm to
program code and run the program code on a computer using a programming
language.
⚫ Once the program is coded using one of the programming language, it will be
compiled to ensure there is no syntax error. Syntax free program will then be
executed to produce output and subsequently maintained and documented for
later reference.
CODING
SYNTAX YES
ERROR ?
NO
EXECUTE OR
RUN
DOCUMENTATION OR
MAINTENANCE
Programming Phase: Coding
◦ Translation or conversion of each operation in the flowchart or
algorithm (pseudocode) into a computer-understandable language.
◦ The compiler will check the program code know also as source
code so that any part of the source code that does not follow the
format or any other language requirements will be flagged as
syntax error.
Programming Phase: Debugging
◦ This syntax error in also called bug, when error is found the
programmer will debug or correct the error and then recompile
the source code again.
59
Runtime Errors
60
Logic Errors
// Program to compute the sum of two integer numbers
public class Sum {
public static void main(String[] args) {
int num1 = 3;
int num2 = 2;
int sum = num1 + num2;
sum = 0;
System.out.println(“Total is " + sum);
}
}
Which displays:
Total is 0
But the result that we want is 5.
61
Debugging
▪ Logic errors are called bugs. The process of finding and correcting
errors is called debugging.
▪ A common approach to debugging is to use a combination of
methods to narrow down to the part of the program where the bug is
located.
▪ You can hand-trace the program (i.e., catch errors by reading the
program), or you can insert print statements in order to show the
values of the variables or the execution flow of the program.
▪ This approach might work for a short, simple program.
▪ But for a large, complex program, the most effective approach for debugging
is to use a debugger utility.