15.561 Information Technology Essentials Session 5 Programming Languages Copyright © 2005 Thomas Malone, Stuart Madnick Acknowledgments: Slides marked “SM” are adapted from Stuart Madnick, MIT. Outline • Types of software • Types of programming languages • Examples – Java – Y2K Types of software • System software – Operating systems – Programming languages – Database systems • Application software – – – – – General office tasks (word processing, etc.) Accounting Design Factory automation … Programming languages • Machine language • Assembly language • High-level languages • Fourth-generation languages A sample LMC program 1 INSTRUCTIONS opcode symbolic 000 1xx 2xx 3xx 5xx 901 902 stop add subtract store load get put ASSEMBLY LANG (Source Program) 2 MACHINE LANG. (Object Program) STEP INSTRUCTION LOC INSTRUCTION 00 01 02 03 04 05 06 07 get store w get store b load w add b put stop 00 01 02 03 04 05 06 07 901 398 901 399 598 199 902 000 Source: S. Madnick, MIT Machine language • Binary • Machine dependent • Stored in the computer when the program is running • Example: 01110110001010010010 …. Assembly language • Mnemonic • Symbolic addressing • One-to-one correspondence with machine language • Example: Get X Add Y Store Z Automatically translating assembly language to machine language Get X Add Y Store Z ... Assembly language program (“source code”) Assembler 11001000100 01100100011 10001011001 ... Machine language program (“object code”) High-level languages • Closer to how people think about their problems • No one-to-one correspondence to machine language • General purpose • Example: Z=X+Y High-level languages - Examples • Fortran • Basic • Visual Basic •C • C++ • Java Example 1 – Basic ! K G K QF H L QF L OQF X L G O F X G Q F X O �X R Q G O V L I O Y Q Q F X O �X R V W L R O X R O U D P QF X W I Z V W F X OZ Q F X O � G O F X O O F W W X OF W I P V O Y R Q O F W I P O ` O ? ? ? G W I Z G W I � F W I P K V W F X Z K V W F X � 6 Q F X O � G O F X O X QF W X OF W I P V V L Z G W I +K V W F X Q F X O �X R O L O V J O X R F H H OX R V W L R OX R OU D P V H OJ QF H OX R O L OV J OQF X L G OD V W O F X E V H OX D O? ? ? OX V OQF H QK X OX R O F H OV J OH X E I P E R OF ' X OF W I P E O F W I P O Q G E2O L Example 2 – C++ ++O L QF L OQF X L G O F X H OX R V W L R OX R OU D P V ( 5 *$ 9 / � � O ` 5 0 : ) 8 � . < , < . 5 *O 1 O 3O 90 . )O. � 8 . ; � 2 5 *) */ < � 8 � O $ 0 / *) � 8 O Z O 2O O 5 *) : / < $ 0 / ) ` ` O �X R Q G O V L I O Y Q $ 0 / ) ` ` O �X R V W L R O X R O U D P $ 0 / ) ` ` O � G O F X O O F W $ 5 * O */ < � 8 2 , 5 9 � O 1 */ < � 8 O % Z ? ? ? 3 & & : / < $ 0 / $ 0 / $ 5* H OZ O 2 O J Q F H O X R O L O V J O Q F X L G O D V W O F X O *E2 V H O X D O ? ? ? O X V O Q F H Q K X O F H O V J O H X O *E2 I P E2 O Z : / < O � O */ < � 8 2 *) � 8 � � 2 ) ` ` O �* G F X O X R O F ' X O F W I P E2 */ < � 8 2 . � 8 . ; � O Z O : / < O + O $ 0 / *) � 8 2 $ 0 / ) ` ` O �*X R L O V J O X R O F W I P G O Q G O �` ` O . � 8 . ; � 2 Example 3 – Java { import java.io.*; import java.lang.*; /** ** Prompts user for list of numbers and outputs average **/ class AverageNumbers { public static void main (String[] args) { float sum = 0; float average = 0; int counter = 0; System.out.println("THIS PROGRAM WILL FIND THE AVERAGE OF THE INTEGERS YOU ENTER THROUGH THE KEYBOARD. TYPE 999 TO INDICATE END OF DATA."); try } } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String cin = "0"; System.out.println("Please enter a number."); while (!(cin=in.readLine()).equals("999")) { sum = sum + Integer.parseInt(cin); counter = counter + 1; System.out.println("Please enter another number."); } in.close(); average = sum/counter; System.out.println("The average of the numbers is : "+average); } catch (IOException e) { System.out.println("Ooops.."); } Automatically translating high-level language to machine language Z=X+Y ... High-level language program (“source code”) Compiler 11001000100 01100100011 10001011001 ... Machine language program (“object code”) “Interpreting” high level languages Z=X+Y ... High-level language program (“source code”) Interpreter Interpreting high level languages • Advantages – Can give machine independence » (e.g., one machine can “look” like another) – Can be easier to debug and modify – Can give more flexibility at “run time” • Disadvantages – Slower “Fourth-generation” languages • Even closer to how people think about their problems • Special purpose • Examples: – Scripting languages » FIND ALL RECORDS WHERE NAME IS “SMITH” – Spreadsheet formulas? Object-oriented programming • A special kind of high-level language • Can increase programming efficiency and software re-use • Combines procedures and data into “objects” • Arranges objects in “class hierarchies” • “Inherits” properties of objects in this hierarchy Class inheritance in object-oriented programming Bank account Owner Balance ----­ Open Deposit Withdrawal Credit card account Owner Balance Credit limit ----­ Open Deposit Withdrawal Authorize charge Checking account Owner Balance Minimum balance: $100 ----­ Open Deposit Withdrawal Joe’s checking account Owner: Joe Balance: $400 Minimum balance: $100 ----­ Open Deposit Withdrawal What’s good about Java? • Highly interactive – Traditional Web - application software runs on server – Java applets dynamically downloaded and run on client (e.g., input data validation) • “Nice” programming language – Simpler than C/C++ – Object-oriented • Secure programming environment – “Sandbox” approach • Portable (“write once, run anywhere”) – Based on Java byte-code interpreter SM Java Operation Servers S Internet Sun/UNIX Windows 2000 Windows XP D J • Static pages (S) • Dynamic pages (D) • Java applets (J) Mac OS HTML page Java applet HTML page Java applet HTML Interpreter Java interpreter HTML Interpreter Java interpreter Netscape (Win 2000) Netscape (Mac OS) Windows 2000 OS Mac OS PC Hardware PowerMac Hardware Client environments } same } different SM Java reality check • Highly interactive or too slow (interpretive) • “Nice” programming language • Secure environment • Portable or too limited or (1) not secure enough or (2) too secure (restrictive) only if consistent Java interpreter availabl (Java “dialects”) SM What will happen with Java? Y2K problem • Why was this a hard problem? • Was money wasted?