, Learning Computer
Programming using
JAVA with
101
Examples
Mr Oddo
Atiwong Suchato
, Table of Contents
Chapter 1: Introduction to Computer Systems .............................. 1
Hello Computer, my dear friend!................................................................1
Computers in Our Lives ...............................................................................2
What Computers do ......................................................................................4
Hardware........................................................................................................6
Central Processing Unit................................................................................6
Memory ........................................................................................................7
I/O Devices...................................................................................................8
Software ..........................................................................................................9
Application Software Vs. System Software ...............................................10
Operating System (OS)...............................................................................11
Binary Representation of Data...................................................................11
The Power of Two .......................................................................................12
Units of Measure..........................................................................................13
Problem Solving Using Computer Program............................................14
Exercise ......................................................................................................18
Chapter 2: Programming Concepts .............................................21
Programming Languages ...........................................................................21
Running a Java Program.............................................................................22
Typical Programming Cycle ......................................................................23
Preparing Java Computing Environment ................................................24
Getting the required software .....................................................................24
Letting Your OS Know Where to Find Java...............................................25
Compiling and Running Java Programs ..................................................27
Integrated Development Environment (IDE) ..........................................28
Basic Program Structure in Java ................................................................29
Syntax, Keywords, and Identifiers ............................................................31
Comments.....................................................................................................32
Be Neat and Organized...............................................................................33
A First Look at Methods .............................................................................33
Escape Sequences.........................................................................................36
Variable at a Glance.....................................................................................37
Naming Rules and Styles............................................................................38
Statements and Expressions.......................................................................40
III
, Simple Calculation.......................................................................................41
Representing Algorithms Using Flowcharts............................................42
Manual Input................................................................................................44
Decisions and Iterations..............................................................................45
Subroutines...................................................................................................49
More Shapes .................................................................................................51
Exercise ......................................................................................................52
Chapter 3: Working with Data ..................................................... 55
Strong Data Typing .....................................................................................55
Data Types in Java .......................................................................................56
Primitive Data Type for Integers ...............................................................56
Primitive Types for Floating-Point Values...............................................57
Primitive Type for Characters....................................................................58
Primitive Type for Logical Values.............................................................59
String .............................................................................................................59
Pointers..........................................................................................................59
Assigning Data to Variables .......................................................................60
Final Variables..............................................................................................63
Un-initialized Variables ..............................................................................64
Operators ......................................................................................................65
String and the addition operator (+) .........................................................67
Math methods ..............................................................................................68
Precedence and Associativity.....................................................................70
Exercise ......................................................................................................76
Chapter 4: More Issues on Data Manipulation ............................. 79
Numeric Data Type Specification..............................................................79
Data Type Conversion ................................................................................80
Automatic Type Conversion and Explicit Type Casting........................82
Expressions with Multiple Data Types.....................................................83
Limitation on Floating Point Computation..............................................85
An Issue on Floating Point Value Comparison .......................................87
Overflow and Underflow ...........................................................................87
Numbers Divided by Zero .........................................................................90
Compound Assignment..............................................................................94
Increment and Decrement ..........................................................................94
Exercise ......................................................................................................96
IV
Programming using
JAVA with
101
Examples
Mr Oddo
Atiwong Suchato
, Table of Contents
Chapter 1: Introduction to Computer Systems .............................. 1
Hello Computer, my dear friend!................................................................1
Computers in Our Lives ...............................................................................2
What Computers do ......................................................................................4
Hardware........................................................................................................6
Central Processing Unit................................................................................6
Memory ........................................................................................................7
I/O Devices...................................................................................................8
Software ..........................................................................................................9
Application Software Vs. System Software ...............................................10
Operating System (OS)...............................................................................11
Binary Representation of Data...................................................................11
The Power of Two .......................................................................................12
Units of Measure..........................................................................................13
Problem Solving Using Computer Program............................................14
Exercise ......................................................................................................18
Chapter 2: Programming Concepts .............................................21
Programming Languages ...........................................................................21
Running a Java Program.............................................................................22
Typical Programming Cycle ......................................................................23
Preparing Java Computing Environment ................................................24
Getting the required software .....................................................................24
Letting Your OS Know Where to Find Java...............................................25
Compiling and Running Java Programs ..................................................27
Integrated Development Environment (IDE) ..........................................28
Basic Program Structure in Java ................................................................29
Syntax, Keywords, and Identifiers ............................................................31
Comments.....................................................................................................32
Be Neat and Organized...............................................................................33
A First Look at Methods .............................................................................33
Escape Sequences.........................................................................................36
Variable at a Glance.....................................................................................37
Naming Rules and Styles............................................................................38
Statements and Expressions.......................................................................40
III
, Simple Calculation.......................................................................................41
Representing Algorithms Using Flowcharts............................................42
Manual Input................................................................................................44
Decisions and Iterations..............................................................................45
Subroutines...................................................................................................49
More Shapes .................................................................................................51
Exercise ......................................................................................................52
Chapter 3: Working with Data ..................................................... 55
Strong Data Typing .....................................................................................55
Data Types in Java .......................................................................................56
Primitive Data Type for Integers ...............................................................56
Primitive Types for Floating-Point Values...............................................57
Primitive Type for Characters....................................................................58
Primitive Type for Logical Values.............................................................59
String .............................................................................................................59
Pointers..........................................................................................................59
Assigning Data to Variables .......................................................................60
Final Variables..............................................................................................63
Un-initialized Variables ..............................................................................64
Operators ......................................................................................................65
String and the addition operator (+) .........................................................67
Math methods ..............................................................................................68
Precedence and Associativity.....................................................................70
Exercise ......................................................................................................76
Chapter 4: More Issues on Data Manipulation ............................. 79
Numeric Data Type Specification..............................................................79
Data Type Conversion ................................................................................80
Automatic Type Conversion and Explicit Type Casting........................82
Expressions with Multiple Data Types.....................................................83
Limitation on Floating Point Computation..............................................85
An Issue on Floating Point Value Comparison .......................................87
Overflow and Underflow ...........................................................................87
Numbers Divided by Zero .........................................................................90
Compound Assignment..............................................................................94
Increment and Decrement ..........................................................................94
Exercise ......................................................................................................96
IV