Introduction to Software Systems Honors Lecture #02: First program in Java Fall 2015 1/26
Programming: early days First steps: Isolated individuals, skillful enough to produce a piece of software Languages: Assembly, Fortran, Cobol, Basic Used a lot of GOTO instructions, which resulted in hard to understand spageti code Fortran was developed between 1954-1956 by John Backus at IBM. The main innovations: use of mathematical notation in expressions subroutines arrays, formatted input and output. A lot of Fortran software is still in use. 2/26
C Over the time, hardware became widely available and the demand for software soared. C language was a major step in creating structured software and improving the productivity of the programming teams. Major programming language in 1980s. 3/26
Also called procedural approach to software design. Structured programming The software is organized around the notion of procedures (subroutines or functions). A procedure is called with some inputs, it returns values. Software consists of main program, procedures, sub procedures and sub-subprocedures. Main Sub 1 Sub 2 Sub 3 Sub 11 Sub 12 Sub 21 Sub 22 Sub 31 4/26
Procedures and data The procedures pass data to each other. Procedures and data are clearly separated. Call with input parameters Returns results Procedure A Access to data Data Call with input parameters Returns results Procedure B Access to data 5/26
Procedural abstraction When you call a procedure you don t care about what is inside the procedure, you are just interested in what comes out. The abstraction of the inner details of a procedure was a powerful procedural abstraction because you could change a procedure, or even replace it as long as in and out parameters stay the same. This was the understanding of abstraction before object orientation came in the late 80 s and early 90s. 6/26
Complex data Procedures and procedural abstraction worked fine when you deal with simple data, like integers, strings or even arrays. But data became quite complex, records or structures were introduced. It became like a mess where all procedures access the complex data all over the place. And the procedures were hard to reuse. 7/26
Need for something new With the increase of software size and complexity, structured programming was pushed to the limits. At certain size, a program becomes so complex that a programmer could not grasp the whole picture. Eventually, structured programming languages could not manage software complexity. There was a need for something new, which could give programmer tools to handle complexity. 8/26
Object oriented idea OO idea was to organize programs around data and to encapsulate them into a single entity. Program Program Program Program data Program Program Program Program 9/26
Object oriented approach Organization of programs around data gave rise to a concept of the class. In programs you represent the data with classes; the procedures are put in the classes. This helps simplify the overall structure of the system, as we are able to develop complex systems but at the same time maintain the understandability of programs. 10/26
Object oriented language: C++ C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell Laboratories in Murray Hill, New Jersey. C++ added features that allow programmers to comprehend and manage larger programs. But, soon C++ was not able to meet the challenges of the new era of computing. Why did we need another OO programming language? World Wide Web and the Internet came in, reached critical mass, and required a new tool. 11/26
Object oriented language: Java Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Originally, Java was not invented for the Internet! The primary motivation was the need for a platform-independent language that could be used to create software to be embedded in various consumer electronic devices. About the time when Java was worked out, the World Wide Web emerged and played a crucial role in the future success of Java. 12/26
What influenced programming languages Hardware Imperative languages model changes in state (e.g., in the values in registers, memory, and on disk). Languages - Algol 60, C, C++, Java, Pascal, Perl, Python, Parallel machines - there are languages for parallel computing Mathematics Lambda calculus - LISP Functional languages view programs as functions - ML, Scheme, LISP: characterized by use of higher-order functions Logic programming view programs as mathematical proofs: a program computes by trying to prove a theorem - Prolog 13/26
Influences Programming Methodology 1960s - larger and larger systems 1970s - top-down design - Pascal late 1970s, early 80s - data-oriented design (ADTs) Ada and Modula 1980s - object-oriented design - C++, Smalltalk 1990s - concurrency; graphics; libraries - Java 2000s - automatic storage management; security; vast amounts of code, good and bad to follow as examples throughout - increasing trend to use of types finally - make the computer do more of the work Special purpose languages Visual languages for designing user interfaces - Visual Basic Markup Languages - HTML, XML, SGML 14/26
Criteria for Evaluating Languages Readability, writeability, reliability, maintainability, security,... Cost: Training. Writing. Compilation (in terms of time and size of produced code) Execution. A dominant cost in some applications. Often, optimization can reduce the cost, at the expense of increasing the cost of compilation/writing/and or training. Language implementation - is the compiler free? Reliability - Failure in produced code/run-time environment can be very costly, think of a program running an X-ray machine. Maintenance - Useful code is always refined, updated, debugged. This cost can overshadow development cost. 15/26
Programming language Java Java was initiated in 1991 by James Gosling at Sun Microsystems as a set-top box project. Java was introduced to the public in 1995 and has gained tremendous popularity since. There are variations of the Java Platform, including the Standard Edition, which is the mainstream version of the language and the associated tools. Java is an object-oriented programming language. Objects are fundamental elements that make up a program. We will explore object-oriented programming concepts by using Java. The Java is accompanied by a library of extra software that we can use when developing programs. This software is referred to as the Java API (Application Programmer Interface) or the standard class library. We will use DrJava development environment for writing Java programs. It is easy to use and provides and intuitive interface and ability to interactively evaluate Java code. 16/26
Download DrJava 17/26
Run DrJava 18/26
A simple program in Java public class Test01 { public static void main (String[] args) { System.out.print( "Hello, world! ); } } You have to save and compile the program. After that you will be able to run it. To save the program, choose a working directory. Execution of a Java program starts with main method, each statement is the main is executed one at a time in sequential order. When the execution reaches the end, the program ends (terminates). > run Test01 Hello, world!> 19/26
Program syntax public class Test01 { public static void main (String[] args) { System.out.print( "Hello, world! ); } } Print statement. The argument is a character string. This is a class. Class name main is entry point main can be called without instantiating the class Semicolon required! IDE color codes the words of the program. 20/26
Different ways to print public class Test01 { public static void main (String[] args) { System.out.print( "Hello, world! ); } } Change the print statement: System.out.print("Hello! ); Hello!> System.out.print("Hello! ); System.out.print("Hello! ); System.out.print("Hello! ); Hello!Hello!Hello!> System.out.println("Hello! ); System.out.println("Hello! ); Hello! Hello! > 21/26
Comments //------------------------------------------------------ // This is my first Java program //------------------------------------------------------ /* Author: Vel Malbasa Date: August 26, 2015 Version: 01 */ public class Test01 { public static void main (String[] args) { System.out.print( "Hello, world! ); } } Comments are important for documenting the program. 22/26
Identifiers and reserved words Identifiers are words used to write a program. Three types of words: Words that we make up (Test01) Words that are in other programs (System, println) Reserved words (class, public, static, void) have special meanings Java is case sensitive. An identifier can not begin with digit. Choose meaningful identifiers! Think about another person reading your program. 23/26
Formatting Have your program look readable. Use the elements of a program that are ignored by compiler: Comments to document the code Blank lines to separate blocks of code Indentation to make a block of code stand out White spaces to increase readability of code 24/26
Argument can be evaluated class Divide { public static void main(string[] args) { System.out.println( 3 / 2 ); } } What will this program print? Arguments 3 and 2 are integers, the program will make an integer division, and the result will be an integer. 25/26
Java can make GUI import javax.swing.jframe; class MyFrame { public static void main(string[] args) { JFrame f = new JFrame(); f.setvisible(true); f.setsize(300, 500); System.out.println( f ); } } The program will create a frame on the screen. > run MyFrame javax.swing.jframe[frame0,0,0,300x500,invalid,lay out=java.awt.borderlayout,title=,resizable,normal,defaultcloseoperation=hide_on_close,rootpane=jav ax.swing.jrootpane[,8,31,124x0,layout=javax.swing.jrootpane$rootlayout,alignmentx=0.0,alignmenty=0.0,border=,flags=16777673,maximumsize=,minimumsiz e=,preferredsize=],rootpanecheckingenabled=true] 26/26