You are on page 1of 48

Chapter 1

Computer Systems and


Java Programming
• Computer Systems
• Operating Systems and Linux
• Computer Programming
• Object-Oriented Programming
• Java Programming
• Development of a Java Program
• Writing My First Java Program
1
Computer Systems

Computer systems = Hardware + Software

Hardware – Physical components/devices

Software – Computer programs with instructions


that perform a particular task based on the
hardware

2
Computer Hardware
Components CPU
- interprets and executes
Input Device instructions
Central Processing Unit
Main Memory
Arithmetic Control
Logic Unit
`
Unit - store data and
Output Device instructions
- 1 byte = 8 bits
`

Registers
Input Device
Secondary Storage - mouse, keyboard, etc.
`

Output Device
Main Memory
- printer, monitor, etc.
Secondary Storage
- permanent
- floppy disks, hard disks,
3
CDs
Computer Software

Utility Programs
Language Translators Application Software Application
(E.g. Assemblers, (E.g. Word Processors,
(E.g. Editors)
Compilers, Interpreters) Database Systems, etc.) Software

System Software
Operating Systems
(E.g. Unix, MS-DOS, Windows 95, etc.)
-Operating
Systems
-Utility Programs
-Language
Hardware Translators

4
Computer Memory

Memory
Address
Memory Cells
1000 0 1 0 1 0 1 1 0
1001 Content of
1 0 1 0 0 1 0 1
1002
memory
0 1 0 1 1 0 1 0
1003
1004
To store program
1005
instructions and
1006
1007
data
1008
1009

5
Chapter 1
Computer Systems and
Java Programming
• Computer Systems
• Operating Systems and Linux
• Computer Programming
• Object-Oriented Programming
• Java Programming
• Development of a Java Program
• Writing My First Java Program
6
Operating Systems and Linux
• An operating system is a collection of programs
that coordinates the operation of computer
hardware and software.
• The Linux operating system is Unix-like and is
one of the most popular operating systems
nowadays.
• Creator: Linus Torvalds in 1991
• Linux Online:
– http://www.linux.org/
• Also look at the faq (frequently asked questions)
– http://www.linux.org/info/faq1.html#faq.q1
• Lab 1 will give you some practices on using Linux.
7
The Linux File System
 
• A file system is a collection of files.

• A file is usually stored on a disk.

• Files can be grouped into directories and organized


into a hierarchical directory structure.

• The top of this hierarchical directory structure is called


the root directory.

• The Home directory is the directory assigned to you.

• The path name of a file enables you to identify a file


uniquely to the Unix file system.
8
Working with Files
 
 
• To list the files in a directory: ($ - command
prompt)
 
$ls
 
• To examine the contents of a file:
 
(1) $cat file1
 
(2) $more file1

9
• To make a copy of a file:
 
$cp file1 file1.backup
 
 
• To rename a file:
 
$mv file1 newFile1
 
• To remove a file:
 
$rm rubbish
 

10
Working with Directories
 
• To display your working directory:
 
$pwd
 
• To create a directory:
 
$mkdir lab1
 
• To change directory:
 
$cd lab1
$cd /home/user1/hp123456/lab2
$cd ..
11
 
• To copy a file from one directory to another:
 
$cp program.java lab1
 
• To move a file from one directory to another:
 
$mv program.java lab1
 
• To remove a directory:
 
$rmdir dir1

12
Chapter 1
Computer Systems and
Java Programming
• Computer Systems
• Operating Systems and Linux
• Computer Programming
• Object-Oriented Programming
• Java Programming
• Development of a Java Program
• Writing My First Java Program
13
Computer Programming
• Computer Programs
– Set of step-by-step instructions
– Perform a specific task to solve a problem
public class HelloStudents {
public static void main(String[] args){
System.out.println(“Hello Students”);
}
}

– Examples: Computer games, word processors,


web browsers, database systems, etc
• Computer programming is to design and
implement computer programs
14
Computer Programming
Learning computer programming is just like learning a
new language such as Japanese, Chinese, Malay, etc.
Add 2 to
3
???

Human language Computer


A programming language defines a set of instructions
(with specific format) that can be given to a computer.
Two important issues on writing programs:
(1) Program syntax – like English grammar
15
(2) Program logic – in order to understand the contents
Programming Languages
Computer programs are written in programming
languages - closest to natural language English
- source code is portable
- e.g. C, C++, Pascal, Java, etc.
High-Level Language - compilers/interpreters translate
HLL into machine code
- more or less human readable
Assembly Language - not portable (hardware dependent)
- assembler assembles AL into
machine executable code
Machine Language - more natural language for
hardware
- bits in 0s and 1s
Hardware 16
- not portable (hardware dependent)
Translator, Interpreter and Compiler
Compilers
- converts HLL
J Translator programs into ML
- translation is off-line,
High Level Machine
Language Language
not at runtime

O
Execute the
object code
Interpreters
S Compiler
- translation is on-line
Source Code Object Code - HLL programs can
be executed directly
S Interpreter Execute the
interpreted
Source Code
code NB: compiled code
can be executed
faster 17
Chapter 1
Computer Systems and
Java Programming
• Computer Systems
• Operating Systems and Linux
• Computer Programming
• Object-Oriented Programming
• Java Programming
• Development of a Java Program
• Writing My First Java Program
18
High-Level Programming
• Procedural or Structured Programming
- COBOL, Pascal, C, etc.
- Algorithm, task driven – “How does?”

• Object-Oriented Programming (OOP)


- C++, Java
- Paradigms, object driven – “What is?”
- Three Design Principles:
 Encapsulation
 Polymorphism
 Inheritance
19
OOP: Objects

Characteristics:
– Identity
– State
– Behaviour

20
OOP: Classes

Identity
• machine serial number
State/Properties
• color, max speed, etc.
Behavior/Methods
• turn, brake, accelerate

21
OOP: Encapsulation

OBJECT

Brake Method
Apply Brake

Colour
Max Speed
….

Accelerate
Method
Apply Accelerate

22
OOP: Composite Objects

Is a Car a Complex Object?

Yes, it’s an object that is made up of other objects!


- Wheels, Seats, Chassis, Exhaust, Steering Wheel, etc, etc...
And… a wheel itself is also a complex object!
- Tire, Trim, Hub Cap, etc, etc... 23
Why OOP?
• Save development time by reusing code
- We can reuse an object class once that is
created

• Easier debugging
- Classes can be tested independently
- Reused objects have already been tested

24
Chapter 1
Computer Systems and
Java Programming
• Computer Systems
• Operating Systems and Linux
• Computer Programming
• Object-Oriented Programming
• Java Programming
• Development of a Java Program
• Writing My First Java Program
25
Take a break…want a cup of
coffee?

26
Java – A Brief History
• James Gosling, Sun Microsystems
Inc., 1991
• Originally a language for
programming embedded consumer
electronic applications
• Initially named as Oak. Then
changed to Java after visiting a
local coffee shop Why OAK?
Because, while trying to think
• In 1995 – Java is a language for of a name, James Gosling
developing Internet applications looked out of his office
window and saw an oak tree!
• Now – one of the most important
general-purpose programming
languages
27
Java 2 Platform
 
• Java 2 Platform, Standard Edition (J2SE)
– for developing client side standalone applications or
applets
• Java 2 Platform, Enterprise Edition (J2EE)
– for developing client side applications or servlets
• Java 2 Platform, Micro Edition (J2ME)
– for developing applications for mobile devices such as
smart phones

We use the Standard Edition Version 5.0 (or


J2SE 5.0), Java Development Kit 5.0 (also known
as JDK 1.5).
28
Why Java Programming?
 

• Java is object-oriented
• Java is simple
• Java is robust and readable
• Java is portable
• Java is secure and distributed
• Java is easy to learn
• Java has many useful features
29
Java Program Translation
Source Code

Both compilation and interpretation

c
Compiler

Java source code compiled


Java Virtual Machine
(Byte Code) into Java byte code for JVM

Java Interpreter Java Interpreter Java Interpreter


(Windows) (Unix/Linux) (Macintosh)

Interpreter translates
from byte code into
hardware-specific
IBM PC UNIX/Linux Macintosh
machine code
30
Applets vs Java Applications
 
• Java Applets
• Java programs to be downloaded via the Internet
• Relatively small programs
• Need a web browser
• Java Applications
• Standalone programs
• Generally larger applications
• Only slight difference in programming, this
course focuses on Java applications

31
Chapter 1
Computer Systems and
Java Programming
• Computer Systems
• Operating Systems and Linux
• Computer Programming
• Object-Oriented Programming
• Java Programming
• Development of a Java Program
• Writing My First Java Program
32
Development of a Java Program
Keyboard

Text Editor Step1: Write the Java source code

Source Code File Syntax Errors

Compiler

Step2: Compile the Java source code


Byte Code File

Java Virtual
Machine

Execute the
Step3: Run the compiled Java code or class file
interpreted
codes

33
Java Development Environment (JDE)
(1) Use Java Development Kit (JDK) from Sun
Microsystems (Version 1.5)

(2) Java Development Environment


• JGRASP : http://www.jGRASP.com/
• JCreator
• BlueJ
• NetBeans from Sun Microsystems
• JBuilder from Borland
• etc.

In Labs – you will use the Linux operating System, and


standard editors will be used for creating Java programs.
However, you are free to use JDE in your lab or at home.
34
Chapter 1
Computer Systems and
Java Programming
• Computer Systems
• Operating Systems and Linux
• Computer Programming
• Object-Oriented Programming
• Java Programming
• Development of a Java Program
• Writing My First Java Program
35
My First Java Application Program
Step1: Write the Java source code into the file Filename:
HelloStudents.java (must be ClassName.java)

/*
Purpose: A sample program to print a message “Hello
Students!” On the screen.
Author: S.C. Hui
Date: 30 July 2006
*/
public class HelloStudents { // begin class
public static void main(String[] args) { // main method
System.out.println("Hello Students!"); // statement
} // end method
36
} // end class
Comments
Three ways to write comments:

(1)
/* multiple line comment
This is the first comment
This is the second comment
Will manly use these
This is the third comment
two types of
*/
comments
(2)
// single line comment
Mainly used for
(3)
documentation
/** javadoc comment */
purpose 37
Class Definition

Java program consists of at least one class.

public class HelloStudents { // begin class


// class body
} // end class

public class – reserved keywords

HelloStudents - class name

Class body is enlosed by { …}

38
The main() Method
For every Java application, it must contain a main() method.
Java applications start execution from the main() method.

public class HelloStudents { // begin class


public static void main(String[ ] args) { // main method
System.out.println("Hello Students!"); // statement
} // end method
} // end class

public static void – reserved keywords


main - method name
String[ ] args – method arguments
main method body is enclosed by { …}
39
Statements
A statement is a command to the computer.
A statement may be
• a simple statement
• a compound statement
A simple statement is one statement terminated by a
semicolon. A compound statement is a sequence of one
or more statements enclosed in braces. E.g.,
  {
statement_1;
statement_2;
...
statement_N;
}
Each of the statement_i can be a simple or a compound
40
statement.
Using Methods from Other Classes
The HelloStudents.java program uses the method
println() from the class System.out for displaying the
text messages.

public class HelloStudents { // begin class


public static void main(String[ ] args) { // main method
System.out.println("Hello Students!"); // statement
} // end method
} // end class

41
Compiling My First Java Program
Step 2: To compile your program
Type:
  $javac HelloStudents.java
 
where javac is JDK Java compiler

$ is the command prompt

The compiler translates the source code into byte


code and saves it into the file HelloStudents.class

42
Executing My First Java Program
Step 3: To run the byte code with the Java
interpreter
Type: $java HelloStudents
Memory

This program does


not have input Byte Code Central
Processing
Unit
Input Data
Only has
Keyboard Output Results output
Hello
Students!
43
jGRASP – Hello Students using Console I/O

44
Hello Students using Message Dialog Box

Using
JOptionPane

45
Key Terms
• Central Processing Unit (CPU)
• main memory
• hardware, software
• Assembly language vs High-level language
• Compiler vs Interpreter
• Java Virtual Machine (JVM)
• Java Byte code instructions
• Java Development Environment (JDE)
• Java Development Toolkit (JDK)
• .java file, .class file
• java command, javac command
• Class definition
• main method
• comment
• statement 46
Review Question

File name: MyFirstProgram.java


Line 1 : public class MyFirstExe {
Line 2 : public void main(String args) {
Line 3 : System.out.println(“Java is easy!!”)
Line 4 : }
Line 5 : }

Is there any error in the program? Write your answers:


Line 1 :
Line 2 :
Line 3 :
Line 4 :
Line 5 :

47
Further Reading

• Read Chapter 1 on “Computer Systems and Java


Programming” of the textbook.
• Read the “Tutorials for JGRASP Integrated
Development Environment” – Available at

http://www.jGRASP.com/

48

You might also like