Introduction
• Understanding computers
• Understanding programming
languages
• Understanding java
understanding computer
• System Programs
• Application Programs
software
computer
System
hardware
• CPU, Main Memory (RAM)
• peripheral devices,
• I/O device
hardware
CPU RAM
Brain of the computer,
most expensive, the
faster computer. Temporary memory,
volatile, directly
CPU components– connected to the CPU,
control unit, program using memory cells unit.
counter, register
instruction, arithmetic
logic unit,
accumulator.
RAM – main memory in action
Main memory is divided into
many memory locations (or
cells) with unique address
9278
Each memory cell stores a set
9279 10011010 number of bits (usually 8 bits,
or one byte)
9280
9281
Large values are
9282
stored in consecutive
9283 memory locations
9284
9285
9286
4
peripheral devices
storage I/O dev
• secondary storage
• Provides permanent storage
• Examples of secondary storage:
•Hard disks
•Floppy disks
•Zip disks
•CD-ROMs
•Tapes.
storage capacity
Every storage device capacity’s can be
expressed in various units:
unit symbol Number of bytes
kilobyte KB 210 = 1024
megabyte MB 220 (over 1 million)
gigabyte GB 230 (over 1 billion)
40
terabyte TB 2 (over 1 trillion)
6
hardware in action
Monitor
Chip that executes
program commands
CPU
Primary storage area for
keyboard
programs and data that
are in active use RAM
Hard Disk
software
program data
The ‘food’ of
consists of
a sequence of simple steps
and operations, stated in a program. Can
precise language that the be supplied
hardware can interpret. interactively or
Programming involve via file.
algorithm design & coding
system application
types of software
• Manage hw and sw
software
• other than system software
• Main system software is • word processors,
operating system (OS) Spreadsheets, Web browsers,
• provides the user games
interface to computer
• Windows XP, Unix, Linux,
Mac OS
creating program
designing
Step by step instructions
to accomplish a task
algorithm Can be in the form of
flowchart or pseudo code,
Use core operation of
sequence, selection (If-
then-else) or
iteration(loop)
• the process of expressing
algorithm in a programming
coding •
language
The product of coding is a
program.
• The act of carrying out the
instructions contained in a
program is called program
execution
flowcharts
basic symbols example design steps
decision
process ouput
Direct
access
storage Magnetic
disk
Sequential terminal
access
examples algorithm design
language
Each type of CPU
A program must be translated
executes instructions
into machine language before
only in a particular
it can be executed
machine language
Language Description Examples Translator
Machine Instruction in 0 and 0111000011000001 None
1 bits
Assembly Instruction in LOAD 3 assembler
mnemonic code STOR 4
High-level Like human sum = 4 + 3; Compiler &
language interpreter
Pascal, C, C++,
Java…
A compiler is a software tool which
translates from high level language
into a specific machine language
the java language
Java n. An island of Indonesia, 48,842 square miles in area, lying
between the Indian Ocean and the Java Sea.
java n. Informal. Brewed coffee. [From Java.]
Java programming language
• Created by Sun Microsystems, Inc. introduced in 1995
• Rich library
• Platform-independent ("write once, run anywhere") or
architecture-neutral
• Java is very suitable for Internet applications.
java translation
Java source Machine
code (program) code
Java Bytecode
Creating compiler
‘.java’ file
interpreter
Creating
javac ‘.class’ file
Java Virtual
Machine (JVM)
Java
bytecode
byte-code can be used on
any computer with JVM
without a need to recompile.
Byte-code can be sent over
the Internet and used
anywhere in the world.
14
Hello.java Hello.class
javac java
Class Loader
setting up Java
1
Java SE Download Page
Install (http://www.oracle.com/technetwork/java/javas
e/downloads/index.html)
JDK Download JDK (ver 5.0 or above))
Documentation
API specification
Tutorial
2
NetBean IDE
Install http://www.netbeans.org
development (download ver 6.5 or above)
Tools JGrasp
zooming a java program
Source code source code are plain text files
file
with name ending in ‘.java’ eg.
Hello.java
In NetBeans project, the source files are
stored in the src folder
a file Source
/* sample complete java source code */
public class Hello{
public static void main(String[] args){
// display a greeting in the console window
styles: }
System.out.println("Hello, World!");
• comments }
• indentation
structure
Java source code files can have three package com.nasran.helloworld;
"top-level" elements: import java.util.*;
• An optional package public class HelloWorld {
declaration
}
• Any number of import
statements
• Class or interface definitions
Packages
are folders for classes which allow a developer to group classes (and interfaces) together
• Java/Android comes with a set of packages = class library = Java/Android
API (Can be nested hierarchically)
• Some example of packages within the Java/Android class library are:
• java.lang
• java.util
• java.net
• android.app
• android.content
• A program using a class residing in a package, must import
the class (with its package name) except for java.lang package
e.g. import java.util.Scanner;
• User can define own package
e.g. package com.Muhammad.helloworld;
Classes
• Java programs are sets of class definitions
• Classes are the main program unit in Java. Can contain
• source code of the program logic
• data fields
• methods (subprograms)
• No statements outside of class definition (except package & import
statements)
• The name of the file should be the same as the name of the public class
Java Statements Blocks
• A statement is a single line of code terminated by a semicolon(;)
salary = days * daySalary;
• A block is a collection of statements bounded by opening and closing braces:
{
x = x + 1;
y = y + 1;
}
• A block can be nested inside another block
Class Definition Structure
public class MyProgram { class header
private int data = 10;
public static void main (String[] args) Class
{ method header body/block
method body/block
}
}
public class SomeClass { Data Field
private static int mValue = 5;
public static void printValue() {
System.out.println("mValue = " + mValue);
}
Methods
public static void main(String[] args) {
System.out.println("Some Class");
printValue();
}
}
22
Comments
• A comment is a note written to a human reader of a program.
• The program compiles and runs exactly the same with or without
comments.
• Types of comment:
• Single-line (starts with //) and
• multi-line (enclosed by /* and */)
/* This program outputs “Hello World”
to screen
*/
public class SomeClass {
public static void main(String[] args) {
System.out.println(“Hello World"); // this is the
statement
}
}
Processing a Program
Creating a Program…
Create/modify source code
Source code
Compile source code Syntax errors
Byte code
JVM Run byte code
Runtime errors
Output
Logic errors
Errors
It is common for programmer to make mistake in a
program.
Three kinds of errors
Syntax errors
Runtime errors
Logic errors
Syntax Errors
Grammatical mistakes in a program
The grammatical rules for writing a program are very strict
The compiler catches syntax errors and prints an
error message.
Example: using a period where a program expects a
semicolon
System.out.print("...").
System.out.print("Hello);
Runtime Errors
Errors that are detected when your program is
running, but not during compilation
When the computer detects an error, it terminates
the program and prints an error message.
Example: attempting to divide an integer by 0
Logic Errors
Errors that are not detected during compilation or
while running, but which cause the program to produce
incorrect results
Example: an attempt to calculate a Fahrenheit
temperature from a Celsius temperature by
multiplying by 9/5 and adding 23 instead of 32
Another example:
System.out.print("Hell");
Savitch : Page 11
Self Test Questions