160 likes | 395 Views
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
E N D
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 Documentation http://docs.oracle.com/javase/7/docs/api/ • Java Tutorial http://docs.oracle.com/javase/tutorial/index.html • Lots of other stuff http://www.oracle.com/technetwork/java/javase/overview/index.html
What is a computer? processor memory disk data I/O devices instructions
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)
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)
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
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
Applications vs. Applets • A program which runs directly on the computer is called an application • programsmay also be downloaded from the web and run in a browser; these are called applets • applets are an outgrowth of Java’s platformindependence, 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
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)
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 • compilethe file in an MS-DOS Prompt window: javacMyProgram.java • runthe class in an MS-DOS Prompt window: javaMyProgram
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: javacMyApplet.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)
Hello World Application // the HelloWorld class, in file HelloWorld.java publicclass HelloWorld { // the main method publicstaticvoid main (String[] args) { System.out.println (“Hello World!”); } } • class • method definition • method invocation • method parameters/Strings • comments • keywords (in blue) • curly braces/indentation
Hello World Applet import java.applet.*; import java.awt.*; // the HelloWorldApplet class // (saved as HelloWorldApplet.java) public class HelloWorldApplet extendsApplet { // the paint method public void paint (Graphics g) { g.drawString (“Hello World!”, 20, 20); } }
Hello World Applet (cont.) • the HTML, saved as HelloWorldApplet.html <HTML> <HEAD> <TITLE>First Applet</TITLE> </HEAD><BODY><APPLETcode=HelloWorldApplet.class width=200 height=100></APPLET></BODY> </HTML>
Edit program Program Development Process Compile program yes Compiler errors? Run program Exceptions? Incorrect Results? yes
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