Weekly Report Chapter 1 Introduction to Computer and Java 1 Contents 1 Introduction 2 Why Program? 3 Computer Systems Hardware and Software 4 Programming Languages 5 What is a Program Made of? 6 The P.
Trang 1Chapter 1
Introduction to Computer and Java
Trang 25 What is a Program Made of?
6 The Programming Process
7 Object-Oriented Programming
Trang 31 Introduction
A powerful programming language
Runs on every type of computer
To create large or small applications
Trang 42 Why Program?
professions.
whatever job their programs, or
software , tell them to do.
Trang 52 Why Program? (Cont’d)
Create software
program:
The logical flow of the instructions
The mathematical procedures
The layout of the programming statements
The appearance of the screens
The way information is presented to the user
The program’s ‘user friendliness’
Manuals, help system, and/or other written
documentation
Trang 63 Computer Systems
All computer systems consist of similar
hardware devices and software components.
Hardware
Refers to the physical components that a
computer made of.
A typical computer system consists of:
The central processing unit (CPU)
Main memory
Secondary storage devices
Input devices
Trang 7Hardware
Trang 8Hardware (Cont’d)
Central Processing Unit (CPU)
CPU’s job:
Fetch instructions
Follow the instructions
Produce some resulting data
Consists of two parts:
Control unit:
coordinates all of the computer’s operation
Arithmetic and logic unit (ALU)
performs mathematical operations
Trang 9CPU (Cont’d)
A program is a sequence of instructions
stored in the computer’s memory
When a computer is running a program,
the CPU is engaged in a process known as
fetch/decode/execute cycle
Fetch
CPU fetches, from main memory, the next instruction
in the sequence of program instruction
Trang 11 Main memory is commonly known as random
access memory (RAM)
RAM is the computer’s main memory that
holds information:
the sequences of instructions in the programs that are running
the data those program are using
RAM is usually a volatile type of memory
is used only for temporary storage
When the computer is turned off, the contents of RAM are erased.
Trang 12Memory (Cont’d)
is assigned a unique number known as an
Trang 13Memory (Cont’d)
Trang 14Secondary Storage
Secondary storage is a type of memory
Can hold data for long periods of time – even when there is no power to the computer
Programs are usually stored in secondary
memory and loaded into main memory when they are running.
Disk drives
Hard disk drive , floppy disk drive, USB drive
Optical drives
Trang 15Input Devices
Input is any data the computer collects
from the outside world.
The device that collects the data and
sends it to the computer is called an
input device
Keyboard, mouse, scanner, digital
camera, …
Disk drives and optical drives can be
considered input devices
Trang 16Output Devices
to the outside world.
printers.
be considered output devices.
Trang 17run on a computer
Operating system: is a set of programs
that manages the hardware and control
their processes
Application software: refers to programs that make the computer useful to the user such as word processing, spreadsheet,
database packages, …
Trang 184 Programming Languages
What Is a Program?
A program is a set of instructions a
computer follows in order to perform a task.
language used to write computer
programs
Trang 19What Is a Program?
Suppose we want the computer to
calculate someone’s gross pay.
The following is a list of things the
computer should do to perform the task:
1 Display a message on the screen: “How
many hours did you work?”
2 Allow user to enter the number of hours
worked.
3 Once the user enters a number, store it in memory.
Trang 20What Is a Program? (Cont’d)
4 Display a message on the screen: “How
much do you get paid per hour?”
5 Allow the user to enter an hourly pay rate.
6 Once the user enters a number, store it in memory.
7 Once both the number of hours worked and the hourly pay rate are entered, multiply the two numbers and store the result in memory.
8 Display a message on the screen that
shows the amount of money earned.
Trang 21What Is a Program? (Cont’d)
Trang 22What Is a Program? (Cont’d)
(numbers).
words instead of number, were
invented to ease the task of
programming.
Trang 23What Is a Program? (Cont’d)
Trang 24A History of Java
Sun Microsystems It was named the Green Team.
hand-held device named *7 that could
be used to control a variety of home
entertainment devices.
It had to use a programming language that
Trang 25A History of Java (Cont’d)
engineer, created a programming
language, which was named Oak
Then, it was renamed Java.
browser HotJava:
was able to download and run small Java programs known as applets
Trang 26Java Application and Applets
may be created with Java:
Application: is a stand-alone program that runs on your computer
Applet: is designed to be transmitted over the Internet from a Web server, and then executed in a Web browser
Trang 275 What Is a Program Made
of?
common to all programming
languages.
Language Elements
Programmer-Defined Names, Syntax
Trang 28Language Elements
Key Words (Reserved Words) (Table 1-3)
Have a special meaning
Used for their intended purpose only
Operators
Are symbols or words that perform operator
on one or more oparands.
Trang 29Language Elements (Cont’d)
Defined by the programmer
Used to identify storage locations in
memory and parts of the program
Usually called identifiers
Rules that be followed when writing a
program
Trang 30Language Elements (Cont’d)
1 public class Payroll
8 grossPay = hours * payRate ;
9 System out println (“Your gross pay is $” + grossPay );
10 }
11 }
Trang 31Lines and Statements
A program contains lines of codes
Some lines are empty → to make a
program more readable
Trang 32 Variables are used to store an item of
data in memory.
A variable is a named storage location in
the computer’s memory.
Variables are symbolic names made up
by programmer that represent locations
in the computer’s memory.
hours, grossPay, payRate are
variables
Trang 33The Compiler and the Java
Virtual Machine
computer and saved to a file using a text editor
A Java programming statements written by the programmer are called source code
The file is called a source file, and the
filename ends with the .java extension,
e.g Payroll.java
Trang 34The Compiler and the Java
Virtual Machine (Cont’d)
source code into an executable form.
Syntax errors are mistakes that the
programmer has made that violate the
rules of the programming languages
These errors must be corrected before the compiler can translate the source code
Trang 35The Compiler and the Java
Virtual Machine (Cont’d)
(e.g C/C++ compilers) translate
source code directly into files that
contains machine language
instructions These files are called
executable files.
Trang 36The Compiler and the Java
Virtual Machine (Cont’d)
The Java compiler translates a Java
source file into a file (a byte code file)
that contains byte code instructions
Byte code filename ends with .class extension.
Byte code instructions are not machine
language, and cannot be directly
executed by the CPU.
To execute the byte code file, we use the
Java Virtual Machine (JVM).
Trang 37The Compiler and the Java
Virtual Machine (Cont’d)
and executes them.
JVM is often called an interpreter, and Java
is often referred to as an interpreted
language
Trang 38Program Development Process
Trang 39 Portable : A program may be written
on one type of computer and then run
on a wide variety of computers, with little or no modification necessary.
computers: highly portable
computer that has a Java Virtual
Trang 40Portability (Cont’d)
Java byte code may be run on any
computer with Java Virtual Machine.
Trang 41Java Software Editions
Java programs is referred to as the
Java SE ̶̶ The Java Standard Edition
provides all the essential software tools
necessary for writing Java applications and applets
Trang 42Java Software Editions
(Cont’d)
Java EE ̶̶ The Java Enterprise Edition
provides tools for creating large business applications that employ servers and
provide services over the Web
Java ME ̶̶ The Java Micro Edition provides
a small, highly optimized runtime
environment for consumer products such
as cell phones, pagers, and appliances
Trang 43Compiling and Running a Java Program
Download Java SE 6 Update RC 10 at
http://java.sun.com/javase/downloads/index.j sp
For Windows Platform, download this file
http://www.java.net/download/jdk6/6u1 0/promoted/b32/binaries/jdk-6u10-rc2- bin-b32-windows-i586-p-
12_sep_2008.exe
Trang 44Compiling and Running a Java Program (Cont’d)
Run 12_sep_2008.exe
Trang 45jdk-6u10-rc2-bin-b32-windows-i586-p-Compiling and Running a Java Program (Cont’d)
Payroll.java in the folder
Trang 46Compiling and Running a Java Program (Cont’d)
Trang 47Compiling and Running a Java Program (Cont’d)
Run the file java.exe
java ClassFilename (class filename
without extension .class)
java Payroll
Trang 48Integrated Development
Environments
Java integrated development
environments (IDEs) consist of
Trang 49Integrated Development
Environments (Cont’d)
Unzip and run Eclipse.exe
Create a new Java project:
File New Java Project
Enter the project name
Choose some options
Finish
Create a new class
File New Class
Enter the class name
Choosing some options
Finish
Trang 506 Programming Process
The programming Process consists of
several steps, which include design,
creation, test, and debugging activities.
The following steps help you in writing
programs
1 Clearly define what the program is to do
2 Visualize the program running on the
computer
Trang 516 Programming Process
(Cont’d)
3 Use design tools to create a model of
the program
4 Check the model for logical errors
5 Enter the code and compile it
6 Correct any errors found during
compilation Repeat Steps 5 and 6 as many times as necessary
7 Run the program with test data for
input
Trang 526 Programming Process
(Cont’d)
8 Correct any running errors found while running the program Repeat Steps 5 to 8
as many times as necessary
9 Validate the results of the program
Trang 53Calculate the user’s gross pay
1 Clearly define what the program is to
do
Purpose To calculate the user’s gross pay
Input Number of hours worked,
hourly pay rate
Process Multiply number of hours
worked by hourly pay rate
The result is the gross pay.
Output Display a message indicating
the user’s gross pay.
Trang 54Calculate the user’s gross pay (Cont’d)
the computer
First create it in your mind
Imagine what the computer screen will
look like while the program is running
Trang 55Calculate the user’s gross pay (Cont’d)
3 Use design tools to create a model of
the program
Use one or more design tools to create a
model of the program
For example, pseudocode
is a cross between human language and a programming language
is helpful when designing an algorithm
Looks more like commands than English statements
Trang 56Calculate the user’s gross pay (Cont’d)
Pseudocode describes the pay-calculating algorithm
Get payroll data
Calculate gross pay
Display gross pay
Trang 57Calculate the user’s gross pay (Cont’d)
A more detailed pseudocode
Display “How many hours did you work?”.
Display the value in the pay variable
Trang 58Calculate the user’s gross pay (Cont’d)
Logical errors mistakes that cause the
program to produce erroneous results
Once a model of the program is
assembled, it should be checked for these errors
If pseudocode is used, the programmer
should trace through it, checking the logic
Trang 59Calculate the user’s gross pay (Cont’d)
5 Enter the code and compile it
Once a model of the program has been
created, checked, and corrected, the
programmer is ready to write source code on the computer.
The programmer saves the source code to a file and begin the process of compiling it.
During this step the compiler will find any
syntax errors that may exist in the program.
Trang 60Calculate the user’s gross pay (Cont’d)
compilation Repeat Steps 5 and 6 as many times as necessary.
If the compiler reports any errors, they
must be corrected Steps 5 and 6 must be repeated until the program is free of
compile-time error
Trang 61Calculate the user’s gross pay (Cont’d)
input
Run-time error is an error that occurs while the program is running These are usually logical errors
Testing for run-time errors requires that
the program must be executed with
samples data or sample input
Trang 62Calculate the user’s gross pay (Cont’d)
8 Correct any running errors found while running the program Repeat Steps 5 to 8
as many times as necessary
When runtime-errors are found in a program, they must be corrected.
9 Validate the results of the program
Enter test data and determine if the program solves the original problem.
Trang 637 Object-Oriented
Programming
Java is an object-oriented programming
(OOP) language OOP is a method of
software development that has its own practices, concepts, and vocabulary.
There are two primarily methods of
programming in use today:
Procedural
Object-oriented
Trang 647 Object-Oriented
Programming (Cont’d)
Procedural Approach
Procedure is a set of programming statements that,
together, perform a task.
Procedures typically operate on data items that separate from the procedure.
In a procedural program, data items are commonly passed from one procedure to another.
The focus of procedural programming is on the creation of procedures that operate on the program’s data.
The separation of data and the code that operates on the data often leads to problems
Trang 657 Object-Oriented
Programming (Cont’d)
Object-Oriented Approach
OOP is centered on creating objects.
An object is a software entity that contains
data and procedures
Data contained in an object is known as the object’s
Trang 667 Object-Oriented
Programming (Cont’d)