Notes, Part I
Download
Report
Transcript Notes, Part I
Programming Fundamentals
AITI-GP
1
Introduction to Programming
2
The Programming Process
Compile Errors?
Create/Edit
Program
Source
Program
Compile
Program
Run-Time Errors?
Object
Program
Execute
Program
3
The Software Life Cycle
Problem Solving Phase
Implementation Phase (Programming)
Analyze Problem
Develop Solution (Algorithm construction)
Verify
Implement Algorithm
Test
Maintenance Phase
4
Problem Solving and
Programming
Problem
Algorithm
Program
5
Constructing Algorithms
Pseudo-code
English-like
Concise
Not constrained by language rules
Flowchart
Diagram
Visual objects (rectangles, arrows, etc) to
describe control flow
6
Algorithm Example
(Pseudocode)
Computing a total
step 1. initialize total to zero
step 2. while there is a number to read,
repeat steps 2a and 2b:
step 2a. read in the number
step 2b. add number to total
step 3. print out total
7
Introduction to Java
8
Computing in the 1990s
The Internet and the WWW
Graphical User Interfaces (GUIs)
Object-oriented Programming (OOP)
9
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
10
Graphical User Interfaces
Users interact with an application
through visual objects:
Mouse and pointer facilitate certain
actions:
Windows, Buttons, Text fields, etc.
click, drag, drop, etc.
“Visual Programming”
focus is on handling UI objects and events
11
Object-Oriented Programming
Program (revised definition):
Object:
collection of interacting objects
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)12
Enter Java
1991
1993
Sun Microsystems develops a language
(based on C) for consumer electronic
devices
WWW explodes in popularity
increased need for “dynamic” Web pages
1995
Sun formally announces Java for web use13
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 (?)
14
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
15
Simple Java Application
File: Hello.java
// Hello World application
public class Hello {
public static void main(String args[])
{
System.out.println(“Hello world”);
}
}
16
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
17
Simple Java Applet
File: HelloAgain.java
import java.awt.*;
import java.applet.Applet;
public class HelloAgain extends Applet {
public void paint(Graphics g) {
g.drawString(“Hello”,50,50);
}
}
18
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
19
Sample .html Document
File: HA.html
<h1> My First Applet </h1>
<applet code=“HelloAgain.class” height=200
width=100>
</applet>
20
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)
21
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
22
Lab Exercises
Create, compile, and execute the
sample Java application and applet
23
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
24
Object Interaction in Java
25
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
26
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
27
Hello.java Application
println()
System.out
object
28
HelloAgain Applet: Creation
USER
1: Open HA.html
BROWSER
2: new
3: paint()
g: Graphics
object
4: drawString()
HelloAgain
Applet
Note: new means the object of class HelloAgain is created
29
HelloAgain Applet:
Another Scenario
USER
BROWSER
1: Move the
browser window
2: paint()
g: Graphics
object
3: drawString()
HelloAgain
Applet
30