Introduction To Computer and Java
description
Transcript of Introduction To Computer and Java
AN OVERVIEW OF COMPUTERS &
PROGRAMMING LANGUAGES
AN OVERVIEW OF COMPUTERS &
PROGRAMMING LANGUAGES
GOALS To understand the activity of programming
To learn about the components of computers
To learn about level of programming languages
To become familiar with your computing
environment and your compiler
To compile and run your first Java program
To recognize 3 types of errors
What is Programming?
Computer programming the art and science of designing and writing
computer programs.
Computer program a sequences of instructions written in a
programming language to achieve a task/to solve a problem.
What is a computer? Hardware
the physical, tangible parts of a computerkeyboard, monitor, disks, wires, chips, etc.
Softwareprograms and dataa program is a series of instructions
A computer requires both hardware and software
Each is essentially useless without the other
Computer System Organization
Hardware•CPU•Main Memory (RAM)•Secondary Storage•I/O Device
Software•System Programs•Application Programs
Computer Organization
Hardware Components of a Computer
Hardware Components of a Computer – motherboard
processor chipadapter cards
memory chips
memory slots
motherboard
Expansionslots for adapter cards
Hardware Components of a Computer
CPU Brain of the computer, most expensive, the faster computer.CPU components– control unit, program counter, register instruction, arithmetic logic unit, accumulator.
RAMTemporary memory, volatile, directly connected to the CPU, using memory cells unit.
SECONDARY STORAGE
•Provides permanent storage for information.•Examples of secondary storage:
•Hard disks•Floppy disks•Zip disks•CD-ROMs•Tapes.
Hardware Components of a Computer
RAM vs Secondary Storage Primary memory
volatileFast Expensive Low capacity Works directly with the processor
Secondary StorageNonvolatileSlow Cheap Large capacity Not connected directly to the processor
INPUT DEVICES
Sheet-fed
Hardware Components of a Computer
OUTPUT DEVICES
Hardware Components of a Computer
CPU and Main Memory
CentralProcessing
Unit
MainMemory
Chip that executes program commands
Eg.Intel Pentium 4Sun ultraSPARC III
Primary storage area for
programs and data that are in
active use
Synonymous with RAM
Secondary Memory Devices
CentralProcessing
Unit
MainMemory
Floppy Disk
Hard Disk
Secondary memorydevices providelong-term storage
Information is movedbetween main memoryand secondary memoryas needed
Hard disksFloppy disksUSB drivesWritable CDsWritable DVDsTapes
Input / Output Devices
CentralProcessing
Unit
MainMemory
Floppy Disk
Hard Disk
Monitor
Keyboard
I/O devices facilitateuser interaction
Monitor screenKeyboardMouseJoystickBar code scannerTouch screen
Software
In contrast to hardware, software is an abstract, intangible entity.
Software can be categorized as system or application software (refer next slide)
It consists of program and data to be used to perform certain tasks
A program is a sequence of simple steps and operations, stated in a precise language that the hardware can interpret
The process of programming involve algorithm design & coding.
Software Categories System Software
Systems programs keep all the hardware and software running together smoothly
The most important system software is the operating system (OS) controls all machine activities provides the user interface to the computer manages resources such as the CPU, memory & I/O Windows XP, Unix, Linux, Mac OS
Application Software generic term for any other kind of softwares word processors, Spreadsheets, Web browsers, games
Algorithm
Algorithm refers to the strategy to solve a problemIt is a clear step by step sequence of instructions that describes how to accomplish a certain task.2 ways can be used to represent algorithm:a) pseudocode
- using english-like-phrases to describe the algorithmb) flowchart
-using diagrams that employ the symbol to describe the algorithm.
Coding
Algorithm need to be translated into computer language so that it can be executedCoding refers to the process of expressing algorithm in a programming languageThe product of coding is a program.The act of carrying out the instructions contained in a program is called program execution
A computer program is stored internally as a series of binary numbers known as the machine language of the computer
Digital Information Computers store all information digitally:
numberstextgraphics and imagesvideoaudioprogram instructions
In some way, all information is digitized - broken down into pieces and represented as numbers
Representing Text Digitally For example, every character is stored as a
number, including spaces, digits, and punctuation
Corresponding upper and lower case letters are separate characters
H i , H e a t h e r .
72 105 44 32 72 101 97 116 104 101 114 46
p. 4.15 Fig. 4-16 Next
The ASCII data set 128 characters (0 until 127) Character A in ASCII 01000001
American Standard Code for Computer InterChange (ASCII)
Binary Numbers Once information is digitized, it is represented
and stored in memory using the binary number system
A single binary digit (0 or 1) is called a bit A byte consists of 8 bits. Each byte in main memory resides at a
numbered location called its address.
Memory
24
Main memory is divided into many memory locations (or cells)
927892799280928192829283928492859286
Each memory cell has a numeric address, which uniquely identifies it
Storing Information
25
927892799280928192829283928492859286
Large values arestored in consecutivememory locations
1001101010011010Each memory cell stores a set number of bits (usually 8 bits, or one byte)
Main Memory
Storage Capacity Every memory device has a storage capacity,
indicating the number of bytes it can hold
Capacities are expressed in various units:
27
KB 210 = 1024
MB 220 (over 1 million)
GB 230 (over 1 billion)
TB 240 (over 1 trillion)
Unit Symbol Number of Bytes
kilobyte
megabyte
gigabyte
terabyte
Language Levels Levels of programming language levels:
machine languageassembly languagehigh-level language
Each type of CPU has its own specific machine language
The other levels were created to make it easier for a human being to read and write programs
28
Programming Languages
Machine language101101100110 011011010
Assembly languageiload intRatebipush 100if_icmpgt intError
High-level languageif (intRate > 100) . . .
Programming Languages Each type of CPU executes instructions only in a
particular machine language
A program must be translated into machine language before it can be executed
A compiler is a software tool which translates from high level language into a specific machine language
An assembler translates from assembly language into a specific machine language
30
Language Description Examples Translator
Machine Instruction in 0 and 1 bits
0111000011000001
None
Assembly Instruction in mnemonic code
LOAD 3
STOR 4
ADD
assembler
High-level Similar to human language, FORTRAN, COBOL, Pascal, C, C++, Java…
sum = 4 + 3; Compiler & interpreter
Programming Languages
The Java Programming Language
Created by Sun Microsystems, Inc. introduced in 1995 and it's popularity has
grown quickly since Rich library Platform-independent ("write once, run
anywhere") or architecture-neutral
Java Translation
The Java compiler translates Java source code into a special representation called bytecode
Java bytecode is not the machine language for any traditional CPU
Another Java software tool, called an interpreter (or Java Virtual Machine (JVM)) , translates bytecode into machine language and executes it
33
Java Translation
34
Java sourcecode (program)
Machinecode
Javabytecode
Bytecodeinterpreter
Javacompiler
Portability After compiling a Java program into byte-code, that
byte-code can be used on any computer with a byte-code interpreter and without a need to recompile.
Byte-code can be sent over the Internet and used anywhere in the world.
This makes Java suitable for Internet applications.
Becoming Familiar with your Computer to use Java
Understand files and folders/directories Locate the Java compiler/ Install J2SE Set path & Java class path Write a simple program (later) Save your work Compile & run Use Dos Command Prompt or IDE
A DOS Command Window
An Integrated Development Environment
File Hello.java1 public class Hello
2 {
3 public static void main(String[] args)
4 {
5 // display a greeting in the console window
6 System.out.println("Hello, World!");
7 }
8 }
Java Program Elements A Java program is made up of class definitions. A class definition must contains a header and a
body. A class contains zero or more methods A method is a named section of code that also has
a header & bodyA method contains program statements
Single-line (starts with //) and multi-line (enclosed by /* and */) comments are used to document the code
Java Program Structure
41
public class Hello{
}
// comments about the class
class header
class body
//Comments can be placed almost anywhere
Java Program Structure
42
public class MyProgram{
}
// comments about the class
public static void main (String[] args){
}
// comments about the method
method headermethod body
Compiling and Running
Type program into text editor Save (file name must be similar to class name) Open Dos Window Change directory to saved file directory Compile into byte codesjavac Hello.java
Execute byte codesjava Hello
Hello.java
javac
Hello.class
java
Class Loader
Processing a Java Program
Class Loader A Java program typically consists of several pieces
called classes. Each class may have a separate author and each is
compiled (translated into byte-code) separately. A class loader (called a linker in other programming
languages) automatically connects the classes together and loads the compiled code (bytecode) into main memory.
JVM
Create/modify source code
Source code
Compile source code
Byte code
Run byte code
Output
Syntax errors
Runtime errors or
incorrect results
Creating a Java Program…
Errors
It is common for programmer to make mistake in a program.
Three kinds of errorsSyntax errorsRuntime errorsLogic errors
Syntax Errors
Grammatical mistakes in a programThe 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 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
E.gSystem.out.print("Hell");