20
CHAPTER 1 INTRODUCTION GOALS To understand the activity of programming To learn about the architecture of computers To learn about machine code and high level programming languages To become familiar with your computing environment and your compiler To compile and run your first

CHAPTER 1 INTRODUCTION GOALS To understand the activity of programming To learn about the architecture of computers To learn about machine code and

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

CHAPTER 1

INTRODUCTIONGOALS To understand the activity of programming

To learn about the architecture of computers

To learn about machine code and high level programming languages

To become familiar with your computing environment and your compiler

To compile and run your first Java program

To recognize syntax and logic errors

Page 2: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

PREREQUISITES

• Computer savvy (file management, text editing)

Problem solving skills Time management High school math (algebra, trigonometry) No prior programming background

required

Page 3: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

What is a computer? Central processing unit Memory Peripherals Executes very simple instructions Executes instructions very rapidly General purpose device Programs describe specific actions

Page 4: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Central Processing Unit (CPU)

Page 5: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Schematic Diagram of a Computer

Page 6: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

What Is A Program?• Program: a sequence of instructions that tell

computer to perform a specific task

• Programming: how to organize the sequence of instructions

• Programming language: a set of conventions that represent the instructions

• Details of instruction description– machine code– assembler– high-level

Page 7: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Programming Languages Machine/Virtual Machine

-- binary code: 21 40 16 100 163 240 Assembleriload intRatebipush 100if_icmpgt intError

High-level languageif (intRate > 100) . . .

Translating to machine code Machine code is the only code that computers can recognize Assembler and high-level languages must be translated to

machine code Two ways for translation

Interpretation Compilation

Page 8: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

The Java Programming Language A high-level language Simple Safe Platform-independent ("write once, run

anywhere") Rich library Designed for the internet Object-oriented

Page 9: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Applets on a Web Page

Page 10: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Becoming Familiar with your Computer

• Login

• Locate the Java compiler

• Understand files and folders

• Write a simple program (later)

• Save your work

• Backup copies– frequently save your file to disks

Page 11: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

A Shell Window

Page 12: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

An Integrated Development Environment

Page 13: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

File Hello.java

1 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 }

Page 14: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

A simple program public class ClassName public static void main(String[] args) // commentMethod call object.methodName(parameters)

System class System.out object

println method

Page 15: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Some Java Characteristics• Case-sensitive• Free-form layout• A Java program must have one and only one

method, called main, which is the program entrance

• Two ways for comments– // -- comment a single line– /* ... */ -- comment a paragraph

• Escape sequence: \character– Special characters that can not be displayed: e.g. \n – Specially used characters: e.g. \”– Non-English letters: e.g. è

Page 16: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Syntax 1.1: Method Call

• Syntax:

– object.methodName(parameters)

– class.methodName(parameters)

• Example:System.out.println("Hello, Dave!");

• Purpose: To invoke a method of an object and supply any additional parameters

Page 17: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Compiling and Running Type program into text Editor Save to disk (hard or floppy disk) Open command shell Compile into byte codes -- Compiler javac Hello.java

Execute byte codes -- Interpreterjava Hello

Page 18: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

From Source Code to Running Program

Page 19: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

Errors Syntax errors (or compile-time error)

Detected by the compiler Examples:

Misspelling words: System.ouch.print("...");

Case-sensitive: system.out.print("Hello”);

Missing something: System.out.print( "Hello);

Semantics error (or Logic errors or Run-time error ) Detected (hopefully) through testing Examples:

System.out.print("Hell");

Page 20: CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and

The Edit-Compile-Test Loop