1
Programming Fundamentals
AITI-GP
2
Introduction to Programming
3
The Programming Process
Create/EditProgram
CompileProgram
ExecuteProgram
Compile Errors? Run-Time Errors?
SourceProgram
ObjectProgram
4
The Software Life Cycle Problem Solving Phase
Analyze Problem Develop Solution (Algorithm construction) Verify
Implementation Phase (Programming) Implement Algorithm Test
Maintenance Phase
5
Problem Solving and Programming
Problem Algorithm Program
6
Constructing Algorithms Pseudo-code
English-like Concise Not constrained by language rules
Flowchart Diagram Visual objects (rectangles, arrows,
etc) to describe control flow
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
8
Introduction to Java
9
Computing in the 1990s
The Internet and the WWW Graphical User Interfaces (GUIs) Object-oriented Programming
(OOP)
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
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
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)
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
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 (?)
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
16
Simple Java Application
File: Hello.java
// Hello World application
public class Hello {
public static void main(String args[]) {
System.out.println(“Hello world”);
}
}
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
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);
}
}
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
20
Sample .html Document
File: HA.html
<h1> My First Applet </h1><applet code=“HelloAgain.class” height=200
width=100></applet>
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)
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
23
Lab Exercises Create, compile, and execute the
sample Java application and applet
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
25
Object Interaction in Java
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
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
28
Hello.java Application
System.outobject
println()
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
30
HelloAgain Applet:Another Scenario
USER BROWSER
HelloAgainApplet
g: Graphicsobject
1: Move the browser window
2: paint()
3: drawString()
Top Related