The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

16
The Joy of Programming (also known as) Introduction to Object-Oriented Programming

Transcript of The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Page 1: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

The Joy of Programming

(also known as)

Introduction to Object-Oriented Programming

Page 2: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Resources• Java in a Nutshell from O’Reilly

• Java Standard Edition (JSE) download

http://www.oracle.com/technetwork/java/javase/downloads/index.html(download the SDK, not the JRE; you may not want it bundled with

NetBeans; you can also download the documentation from this link)

• Java API Documentationhttp://docs.oracle.com/javase/7/docs/api/

• Java Tutorialhttp://docs.oracle.com/javase/tutorial/index.html

• Lots of other stuffhttp://www.oracle.com/technetwork/java/javase/overview/index.html

Page 3: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

What is a computer?

memoryprocessor disk

I/Odevices

data

instructions

Page 4: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Programming Languages• assembled languages

// X = (A+B) * (C+D)

LR 1, A // load register 1 from A

AR 1, B // add B to register 1

LR 2, C // load register 2 from C

AR 2, D // add D to register 2

MP 2, #1 // multiply register 2 by register 1

SR 2, X // store register 2 in X

• compiled languages (Fortran, Cobol)X = (A + B) * (C + D)

• structured languages (C, Pascal)

Page 5: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Programming Languages (cont.)• object-oriented languages (C++, Ada, Smalltalk,

Java)

• 4th generation languages: special purpose (RPG, DBASE, Delphi)– databases– proprietary products

• visual programming environments /integrated development environments (Visual Basic, Visual C++, JBuilder, Eclipse)

Page 6: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Compiled vs. Interpreted• Compiled Languages

source code => compiler => relocatable object code =>

loader => absolute code– source code is portable (sort of)– object code is platform-specific

• Interpreted Languages (Java)source code=> compiler => bytecode => interpreter =>

absolute code on the fly– bytecode is portable to any platform with an interpreter– interpreting is slower

Page 7: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Java• an object oriented language

• an interpreted language

• developed by Sun MicroSystems

• versions:– JDK v1.7.x

• available free from Oracle:http://www.oracle.com/technetwork/java/javase/downloads/index.html

Page 8: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Applications vs. Applets• A program which runs directly on the computer is

called an application

• programs may also be downloaded from the web and run in a browser; these are called applets

• applets are an outgrowth of Java’s platform independence, which allows an applet to run in a browser on any machine (PC, MAC, UNIX, …)

• applets are subject to security restrictions:– they cannot read or write the local hard drive– they cannot print– they can only talk to the computer that served them

Page 9: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Java Structure• The basic programming element in Java is the class

• every class has a name

• all instructions and data in Java exist in a class

• classes usually contain methods, which also have a name

• all instructions in Java exist in a method inside a class (almost)

Page 10: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Application Mechanics• write a class in a text editor; save it to a file with the same

name as the class and a .java suffix• set the path:

– In the MS-DOS Prompt window set path=%path%;c:\jdk1.4\bin

– Windows98, etc.:in C:Autoexec.bat (save the original first)

– WindowsXP: Control Panel > System > Advanced > Environment Variables > select Path under System Variables > Edit

• compile the file in an MS-DOS Prompt window: javac MyProgram.java

• run the class in an MS-DOS Prompt window:java MyProgram

Page 11: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Applet Mechanics• write a class that extends Applet in a text editor;

save it to a file with the same name as the class and a .java suffix

• compile in an MS-DOS Prompt window: javac MyApplet.java

• write an html program to load the applet; save it to a file with .html suffix

• load the html file in a browser, or via appletviewer in an MS-DOS Prompt window:

appletviewer MyApplet.html

• applets usually involve a graphical display, and/or some interaction with the user (GUI)

Page 12: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Hello World Application// the HelloWorld class, in file HelloWorld.javapublic class HelloWorld{ // the main method public static void main (String[] args) { System.out.println (“Hello World!”); }}

– class– method definition– method invocation– method parameters/Strings– comments– keywords (in blue)– curly braces/indentation

Page 13: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Hello World Appletimport java.applet.*;

import java.awt.*;

// the HelloWorldApplet class

// (saved as HelloWorldApplet.java)

public class HelloWorldApplet extends Applet

{

// the paint method

public void paint (Graphics g)

{

g.drawString (“Hello World!”, 20, 20);

}

}

Page 14: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Hello World Applet (cont.)• the HTML, saved as HelloWorldApplet.html

<HTML>

<HEAD>

<TITLE>First Applet</TITLE>

</HEAD><BODY> <APPLET code=HelloWorldApplet.class

width=200 height=100> </APPLET></BODY>

</HTML>

Page 15: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Edit program

Compile program

Compilererrors?

Run program

Exceptions?IncorrectResults?

yes

yes

ProgramDevelopmentProcess

Page 16: The Joy of Programming (also known as) Introduction to Object-Oriented Programming.

Algorithms

• A procedure which is– unambiguous– executable– terminating

• Example: IRS Form 1040

• computer programs implement algorithms

• you must understand the algorithm before it can be programmed