1 programming fundamentals aiti-gp. 2 introduction to programming

30
1 Programming Fundamentals AITI-GP

Upload: charleen-sharp

Post on 01-Jan-2016

239 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

1

Programming Fundamentals

AITI-GP

Page 2: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

2

Introduction to Programming

Page 3: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

3

The Programming Process

Create/EditProgram

CompileProgram

ExecuteProgram

Compile Errors? Run-Time Errors?

SourceProgram

ObjectProgram

Page 4: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

4

The Software Life Cycle Problem Solving Phase

Analyze Problem Develop Solution (Algorithm construction) Verify

Implementation Phase (Programming) Implement Algorithm Test

Maintenance Phase

Page 5: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

5

Problem Solving and Programming

Problem Algorithm Program

Page 6: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

6

Constructing Algorithms Pseudo-code

English-like Concise Not constrained by language rules

Flowchart Diagram Visual objects (rectangles, arrows,

etc) to describe control flow

Page 7: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

7

Algorithm Example (Pseudocode)

Computing a total

step 1. initialize total to zerostep 2. while there is a number to

read, repeat steps 2a and 2b: step 2a. read in the number step 2b. add number to totalstep 3. print out total

Page 8: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

8

Introduction to Java

Page 9: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

9

Computing in the 1990s

The Internet and the WWW Graphical User Interfaces (GUIs) Object-oriented Programming

(OOP)

Page 10: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

10

The Internet A global network of computers The World Wide Web (WWW)

users retrieve documents remote programs may be invoked

Need language platforms that are: network-aware portable secure

Page 11: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

11

Graphical User Interfaces Users interact with an application

through visual objects: Windows, Buttons, Text fields, etc.

Mouse and pointer facilitate certain actions: click, drag, drop, etc.

“Visual Programming” focus is on handling UI objects and events

Page 12: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

12

Object-Oriented Programming Program (revised definition):

collection of interacting objects Object:

a thing that has identity, state, and behavior instance of a class

OOP: focus is on developing classes that specify

state (variables) and behavior (functions)

Page 13: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

13

Enter Java 1991

Sun Microsystems develops a language (based on C) for consumer electronic devices

1993 WWW explodes in popularity increased need for “dynamic” Web pages

1995 Sun formally announces Java for web use

Page 14: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

14

What is Java?

Java is a general purpose programming language that is: object-oriented interpreted, architecture-neutral,

portable distributed (network-aware), secure simple, robust multi-threaded high-performance (?)

Page 15: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

15

Two Types of Java Programs Applications

general-purpose programs standalone executed through the operating system

Applets programs meant for the WWW embedded in a Web page normally executed through a browser

Page 16: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

16

Simple Java Application

File: Hello.java

// Hello World application

public class Hello {

public static void main(String args[]) {

System.out.println(“Hello world”);

}

}

Page 17: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

17

Compilation and Execution

Compile using javac (compiler)C> javac Hello.java Hello.class file is produced

Execute using java (interpreter)C>java Hello requires a Hello.class file

Page 18: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

18

Simple Java AppletFile: HelloAgain.java

import java.awt.*;

import java.applet.Applet;

public class HelloAgain extends Applet {

public void paint(Graphics g) {

g.drawString(“Hello”,50,50);

}

}

Page 19: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

19

Executing Applets

After compiling the java program:

Embed an “applet tag” in an .html document that references the .class file

Open the .html document using a browser or the appletviewer

Page 20: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

20

Sample .html Document

File: HA.html

<h1> My First Applet </h1><applet code=“HelloAgain.class” height=200

width=100></applet>

Page 21: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

21

What is HTML? Hypertext Markup Language Underlying language of Web pages A means of providing formatting

instructions for presenting content Text-based .html documents:

collection of content and controls (tags)

Page 22: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

22

Java Program Structure Java Program

(optional) import declarations class declaration

Class class name should match its file name may extend an existing class (such as

Applet) contains method/function declarations

Page 23: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

23

Lab Exercises Create, compile, and execute the

sample Java application and applet

Page 24: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

24

Event-Driven Programming Programming User Interface events

e.g., what happens when you click on a button

Example: TFCopy1 Applet two text fields and a button clicking on the button causes the

contents of a text field to be transferred to the other

Page 25: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

25

Object Interaction in Java

Page 26: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

26

Programming Paradigms Procedural Programming (C)

focus is on writing procedures/functions program execution viewed as functions

calling other functions Object-oriented Programming (Java)

focus is on writing classes for objects program execution viewed as objects

interacting with each other

Page 27: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

27

OOP and Object Interaction

Objects pass messages to each other An object responds to a message by

executing an associated method defined in its class

Causes a “chain reaction” The user could be viewed as an object Depicted in an Object Interaction

Diagram

Page 28: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

28

Hello.java Application

System.outobject

println()

Page 29: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

29

HelloAgain Applet: Creation

USER BROWSER

HelloAgainApplet

g: Graphicsobject

1: Open HA.html

2: new3: paint()

4: drawString()

Note: new means the object of class HelloAgain is created

Page 30: 1 Programming Fundamentals AITI-GP. 2 Introduction to Programming

30

HelloAgain Applet:Another Scenario

USER BROWSER

HelloAgainApplet

g: Graphicsobject

1: Move the browser window

2: paint()

3: drawString()