Chapter 1 Introduction to computers and programs(1)
Chapter 1 Introduction to computers and programs(1)
computers and
programs
CHAPTER 1
1
Learning outcomes
1.1 Components of a computer
1.2 Bits and Bytes
1.3 Programming Languages
1.4 Java Specification, JDK, JRE, IDE and API
1.5 Sample program
1.6 Eclipse IDE
1.7 Comment, case-sensitive, main method, block, keyword,
statement, public class, non-public class, display text, display number,
plus (+) sign
2
Computer Organization
Source: http://hubpages.com/
3
1.1 Components of a Computer
Central Processing Unit (CPU) Secondary Storage
Input
Unit (Hard disk,
Devices Arithmetic and usb drives, CDs,
Control Unit DVDs)
Logic Unit (ALU)
Memory Unit
Communication
Output Devices
Random Access Read Only
Devices (Network Card,
Memory (RAM) Memory (ROM)
Modem, etc.)
4
1.1 Components of a Computer (i)
Central Processing Unit
5
1.1 Components of a Computer (ii)
Memory Unit
◦ Random Access Memory (RAM)
◦ internal memory of the CPU for storing data, program, and program
result.
◦ It is a read/write memory which stores data when the machine is
working.
◦ All data will be erased once machine is switched off
7
1.1 Components of a Computer (iv)
Output devices
◦ An output device is any peripheral that receives data from a computer, usually
for display, projection, or physical reproduction.
◦ Example: monitor, speaker, printer, and etc.
8
1.1 Components of a Computer (v)
Secondary Storage Unit
◦ Also known Auxiliary storage, or long term storage.
◦ Is a non-volatile memory, i.e. it does not lose stored data when the device is
powered down
◦ It is not directly accessible by the CPU, because it is not accessed via the
input/output channels (it is an external device).
◦ Example: hard disks, USB drives, CDs, DVDs, etc.
9
1.1 Components of a Computer (vi)
Communication devices
◦ Is a hardware device capable of transmitting an analog or digital signal over
the telephone, other communication wire, or wirelessly to other computers or
electronic devices.
◦ Example: modem, router, network interface card, wireless adapter, etc.
10
1.2 Bits and byte
Data in computer are a series of switches – on or off
Bit
◦ is the smallest unit of storage.
◦ A bit stores a binary digit of either 0(off) or 1 (on).
◦ A bit is too small to be much use
Byte
◦ 1 byte = 8 bits
◦ All storage is measured in bytes, despite being very different hardware
1 kilobyte (KB) 1024 bytes (210 bytes)
1 megabyte (MB) 1 million bytes. (10242 bytes)
1 gigabyte (GB) 1 billion bytes (10243 bytes)
1 terabyte (TB) 1 trillion bytes (10244 bytes) 11
1.2 Bits and byte (i)
° One byte can store one character, e.g. 'A' or 'x' or '$'
° Data (characters, numbers) are encoded as a series of bytes
° Encoding scheme – the rule applied to translate (encode) the data to the format
that computer understands
° ASCII is one of the popular encoding representing each typed character by a
number.
° Each number is stored in one byte (so the number is in 0..255). E.g.
◦ ‘A’ is 65 ‘B‘ is 66
◦ ‘a’ is 96 Space is 32
12
13
1.2 Bits and byte (ii)
Each letter is stored in a byte
100 typed letters take up 100 bytes
Thus, when we type a word, the numbers are sent to computer
E.g. The ASCII value for the word “Spring” is as below:
S p r i n g
14
1.3 Programming Languages
Programming Languages tell the computer what needs to be done
Three generation of programming languages are
• Machine languages
• Assembly languages
Java Object C C
• High-level languages
High Level Language
Assembly Language
Machine Language
Computer Hardware
15
1.3.1 Machine Languages
The only language understood directly by computer
◦ Use binaries – 0s and 1s
Defined by computer’s hardware design
◦ Machine-dependent. Change from machine to machine
Disadvantages:
• Difficult to understand for human readers
• Slow, tedious and error-prone
Source: www.urgenthomework.com
16
1.3.2 Assembly Languages
◦ English-like abbreviations
◦ Represent elementary operations of computer
◦ Translated to machine language
◦ Assemblers convert to machine language
Source: www.allbusiness.com
17
1.3.3 High-Level Languages
◦ Similar to English language
◦ Contain common mathematical notation
Advantages:
◦ Easy to use.
◦ Single statements accomplish substantial tasks
Disadvantages:
◦ Need to be translated to machine language
◦ Compiler convert it to machine language. Conversion takes considerable
time
18
1.3.3 High-Level Languages (i)
Program written in High-Level language is known as source code
19
1.3.3 High-Level Languages (ii)
20
1.3.3 High Level Languages (4)
A compiler translates the entire source code to machine code file
before being executed
21
1.4 The Java language specification, API, JDK
and IDE
◦Java was originally developed by James Gosling at Sun
Microsystems in 1995. Oracle Corporation acquired Sun
Microsystems in 2009–10.
◦Java is a class-based, object-oriented programming
language, with syntax like C and C++.
◦All Java applications need to compile to bytecode that can James Gosling
run on any Java virtual machine.
◦Java version 19 was released in September 2022, and
version 20 is planned to release in 2023.
22
1.4 The Java language specification, API,
JDK and IDE (i)
Java language specification is a technical definition of the Java
programming language’s syntax and semantics.
(https://docs.oracle.com/javase/specs/jls/se18/html/index.html)
Application program interface (API), or library, contains predefined
classes and interfaces for developing Java programs.
Duke -
Java
mascot
23
1.4 The Java language specification, API,
JDK and IDE (iii)
◦The Java Development Kit (JDK) is a software used for developing Java
applications.
◦It contains the Java library, compiler, and other tools needed in Java Development.
◦There are 3 editions of JDK available:
◦ Java Standard Edition (Java SE) to develop client-side applications. The
applications can run standalone or as applets running from a Web browser.
◦ Java Enterprise Edition (Jakarta EE) to develop server-side
applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces
(JSF).
◦ Java Micro Edition (Java ME) to develop applications for old mobile
devices, such as Nokia series 40 and 60.
24
1.4 The Java language specification, api,
jdk and ide (iv)
Integrated development environment (IDE)
Is a software application that provides comprehensive facilities to computer
programmers for software development
Popular Java IDE are Eclipse (Eclipse Foundation) , NetBeans (Apache open-
source project), IntelliJ IDEA(JetBrains) & etc.
25
1.5 Simple Java Program
public class Welcome {
System.out.println("Welcome to Java!");
keyword
} API/library command
26
1.6 Eclipse IDE (integrated development environment )
◦ Eclipse is one of the popular IDE used for developing Java application.
◦ Initiated by IBM in November 2001.
◦Now, it is under Eclipse Foundation.
◦ Written in Java.
◦ Free and open-source software
27
1.6.1 Launch Eclipse application
28
1.6.2 Select a workspace 1 Click the Browse
button to
change
workspace
3 Click OK 4 Click OK 29
1.6.3 Eclipse welcome page
30
1.6.4 create new project
31
1.6.5 New Java Project
Enter a valid project name, such
1 as Chapter1
Click the Finish button
32
1.6.6 To package explorer
Click the restore button to
switch to the package
explorer
33
1.6.7 Add a new class
1
Choose New -> Class
34
1.6.8 New Java Class
35
1.6.9 new Java class
36
1.6.10 Create Welcome.java
37
1.6.11 execute the program
1
OR
38
1.6.12 save and launch
39
1.6.13 result on console
40
Try this out…..
Write a Java program to display
Name : your name
Age: your age
Birthdate: dd/mm/yyyy
41
1.7.1 Comment
◦A comment is a programmer-readable explanation or annotation in the source
code/program
◦They are added with the purpose of making the source code easier for humans to
understand
◦Generally ignored by compilers and interpreters
◦Syntax : //(single line) or /* */(block)
◦Example:
// this is a single comment line /*
this is multiline
comment code
*/
42
1.7.2 Case Sensitivity
◦keywords,
◦class name,
Case
sensitive
◦variables’ name,
◦methods’ name
43
1.7.3 Keywords
• Keywords are reserved words that have a specific meaning to the compiler
• Cannot be used for other purposes in the program.
abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends for final finally
goto if implements import instanceof
int interface long native new
package private protected public return
short static super switch synchronized
this throw throws transient try
void volatile while
44
1.7.4 Class
• The class is the essential Java construct.
• A class is a template or blueprint for objects.
• Java program is defined by using one or more classes.
class XXX {
:
}
45
1.7.5 Main Method
•The main method provides the control of program flow.
•The Java interpreter executes the application by calling the main method.
•Main method must be created under a class block
o Syntax of main method:
46
1.7.6 Block
A pair of braces { } in a program form a block that groups
components of a program.
Example:
public class Welcome {
public static void main(String args[]){
System.out.println("Welcome to Java!");
}
47
1.7.7 Statements
• A statement represents an action or a sequence of actions.
• A program made up by a series of statements.
• The statement System.out.println("Welcome to Java!"); in
the program is a statement to display display a text welcome to Java!.
• Every statement in Java ends with a semicolon (;).
48
1.7.8 Public vs non-public class
public class Test1 { class Test2 {
public static void main (String args[]) { public static void main (String args[]) {
System.out.println(“test 1”); System.out.println(“test 2”);
} }
} }
•Compulsory to save the file as Test1.java • Can save under any filename, e.g.
myfile.java, Test2.java, Testing.java
•Can execute directly in Eclipse
• The compiled file, Test2.class is generated
by compiler
• Need to run configuration in Eclipse
49
1.7.9 display text vs digit
Use System.out.println() command for displaying values on screen
class Display {
public static void main (String args[]) {
System.out.println(“Hello world!”); Text to be displayed must type in
the pair of double quotation marks
System.out.println(23);
System.out.println(“23”);
Digits can be displayed directly or
} placed in the pair of double quotation
marks
}
50
1.7.10 +(plus) symbol
2 roles:
◦ Addition
When both operands are number, +
◦ Joint / concatenate text
plays the role add/sum
System.out.println(2 3 + 4 );
Operand 1 Operand 2
51
Try this out (2) …..
Write a Java program to display the results for:
5+3
5–3
52