300 likes | 320 Views
Programming Fundamentals AITI-GP. Introduction to Programming. The Programming Process. Compile Errors?. Run-Time Errors?. Create/Edit Program. Compile Program. Execute Program. Source Program. Object Program. The Software Life Cycle. Problem Solving Phase Analyze Problem
E N D
The Programming Process Compile Errors? Run-Time Errors? Create/Edit Program Compile Program Execute Program Source Program Object Program
The Software Life Cycle • Problem Solving Phase • Analyze Problem • Develop Solution (Algorithm construction) • Verify • Implementation Phase (Programming) • Implement Algorithm • Test • Maintenance Phase
Problem Solving and Programming Problem Algorithm Program
Constructing Algorithms • Pseudo-code • English-like • Concise • Not constrained by language rules • Flowchart • Diagram • Visual objects (rectangles, arrows, etc) to describe control flow
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
Computing in the 1990s • The Internet and the WWW • Graphical User Interfaces (GUIs) • Object-oriented Programming (OOP)
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
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
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)
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
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 (?)
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
Simple Java Application File: Hello.java // Hello World application public class Hello { public static void main(String args[]) { System.out.println(“Hello world”); } }
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
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); } }
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
Sample .html Document File: HA.html <h1> My First Applet </h1> <applet code=“HelloAgain.class” height=200 width=100> </applet>
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)
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
Lab Exercises • Create, compile, and execute the sample Java application and applet
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
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
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
Hello.java Application println() System.out object
HelloAgain Applet: Creation USER BROWSER 1: Open HA.html 2: new 3: paint() g: Graphics object 4: drawString() HelloAgain Applet Note: new means the object of class HelloAgain is created
HelloAgain Applet:Another Scenario USER BROWSER 1: Move the browser window 2: paint() g: Graphics object 3: drawString() HelloAgain Applet