programming class 9 lbsc 690 information technology
Post on 19-Dec-2015
218 views
TRANSCRIPT
Agenda
• Questions
• Programming– The mythical person-month– History of programming– Object oriented programming– Programming exercise in Java
Software
• Software models aspects of reality– Input and output represent the state of the world– Software describes how the two are related
• Examples– Ballistic computations– Alta Vista– Microsoft Word
Programming Languages
• Used to specify every detail of the model
• Special purpose– Able to specify an entire class of models
• Spreadsheets (Excell, Quatro Pro, ...)
• Databases (Access, Paradox, ...)
• General purpose– Able to specify any possible model
• Pascal, C, Java, ...
The Mythical Person-Month
• If it would take one person three months, why does it take four people SIX months?
• Four causes– It wouldn’t have taken 3 months anyhow!– Partitioning strategy– Training time– Communications effort
How long will it take?
• Rules of thumb– 1/3 specification– 1/6 coding– 1/2 test planning, testing, and fixing!
• Add time for coding to learn as you go, but don’t take time away from the other parts!– Reread the section on “gutless estimating” if
you are tempted
Training Time
• Simple Example– Full time person = 2,000 hours/year– Part time person = 288 hours per year– No training -> 7 part time people– With training -> 10 part time people
• Learning the specification takes lots of time– Learning organizational “rules” takes longer
Communications
• Sort of like continuous training– Who needs to know what I just learned?
• Can be minimized by good partitioning– Limit the number of interfaces
• Can be facilitated by computers– Asynchronous communication techniques
• Email, BBS, voice mail
History of Programming
• Machine code– Zeroes and Ones
• Assembly language– “Assembler” changes names to machine code
• High-level languages– “Compiler” translates math to machine code
• Independent of machine “architecture”
– FORTRAN, COBOL
History of Programming
• Structured (Modular) Programming– Group instructions into meaningful abstractions– C, Pascal
• Object-oriented programming– Group “data” and “methods” into “objects”– C++, Java
Your First Java Program
• Log in to WAM
• cd ~/../pub
• cp /users/rba/pub/java/* .– DONT FORGET THE DOT AT THE END!!!
• tap java
• javac HelloWorld.java
• java HelloWorld
What You Just Did
• First you got some Java program files– Each .java file specifies one “class”
• Then you “compiled” one using javac– This produced HelloWorld.class
• Then you ran it using java– .class files are a “bytecode” representation
• An “interpreter” (“java”) is needed to run them
– You don’t specify .class when you run it
Java Bytecode
• “Machine language” depends on the machine– Programs compiled for a Sun won’t work on a PC
• Java claims “write once, run anywhere”– Without recompiling
• Traditional interpreters are slow– Because they must read every character every time
• Java compiles to a standardized bytecode– Web browsers include a bytecode interpreter
Changing the Program
• Use pico to edit HelloWorld.java– Change “Hello, world!” to something different– Be careful not to change anything else!
• Use javac to compile it– This produces the bytecode in the .class file
• Use java to run it– It should print whatever you told it to– It won’t work if you include “.class” in the name
Using Java with the Web
• Web browsers render HTML– But HTML lacks control and data structures
• Newer browsers can interpret java bytecode– Java applets are programs designed for the web
• This allows programs to be “rented”– They still execute on the client machine– But some classes can be obtained on the web
Java Applet Example
• Change HelloWorldApplet.java– Make it say whatever you want
• javac HelloWorldApplet.java• Fire up Netscape or Internet Explorer
– http://www.wam.umd.edu/~userid/hello.html
– You should get whatever you told it to say
• You can also change hello.html– Then select “reload” to reload the web page
Object Models
• Represent things in the world as “objects”– The simplest objects are “variables”
• Represent actions with “methods”– The simplest methods are “operations”
• “Classes” group objects with methods– Classes model aspects of reality
• Objects are instances of classes
A Simple Example
• Variables: Height, Weight, Shoe size
• Operations: Multiply, Divide
• Method: Shoe size=4*Weight/Height
• Class: Person
• Object: George
Instances
• Classes model kinds of things– “person” is an example of a class
• A Class may be instantiated– I am an instance of person
• Object-Oriented Programming– Define simple classes with variables & operations– Define more complex classes using simple objects– Invoke a method in some class to start things
Data Types• int
– Like integers, but there is a biggest and smallest
• float– Like real numbers, but there are a finite number
• char– Any character in any language (Unicode)
• boolean– True or false
Arrays
• Lists of elements, each of the same data type– For example, the number of days in each month
• Each element is assigned an integer index– The index is used to refer to the element (x[4])– In Java, the index numbers start at zero
• An string acts like an array of characters
Some Basic Operations
Negate - int produces int
Add int + int produces int
Multiply int * float produces float
Compare int < int produces boolean
Compare char == char produces boolean
Statements
• Simple assignment statements– numberOfBirds = numberOfHawks + numberOfOrioles;
– note difference with comparison ==
• Statements that invoke a method– airForceOne = new Airplane(“747”);
• This is called a “constructor method”
– Altitude = airForceOne.readAltitude();
• Return a value from a method– return numberOfBirds;
Making Methods
• Three ways to combine statements:– Sequential
• {… ; …; …; …}
– Conditional• if (i= =3) then {…} else {…}
• try {…} catch (exception e) {…}
– Loop • do {…} while (i<5)
• for (i=0;i<10;i++) {…}
A Full-Featured Example
• TestDate computes Julian dates– The number of days since January 1
• You type in the year, month, and day– The program expects these to be integers
• It prints out the Julian date– And it knows about leap years
Testing the Date Program
• javac *.java– This will compile everything
• java TestDate.java– TestDate contains a magic incantation to start it
• Answer the questions– If you make a mistake, it should complain
• Try lots of possibilities– February 29, 1900 is particularly interesting
Examining the Classes
• TestDate exercises methods in the Date class– It looks a lot like HelloWorld– Don’t mess with it - it works!
• TermInput reads input from the terminal– Comments at the top describe what it does– The rest is very grungy - don’t mess with it!
• But don’t forget to compile it!
A Full-Featured Example
• ValInt is a class, but it contains only data– Public data can be seen by methods in other classes
• Date has all the really interesting stuff in it.– This is what you will modify for homework– Use “pico” or “more” to read it
Date.java
• Braces and semicolons indicate sequential– Notice how layout is done for readability– Java does not care where line breaks happen
• But not in the middle of a word!
• if (boolean) {;;;} is a conditional
• do {;;;} while (boolean) is a loop– for (;;) {;;;} is another kind of loop
Date.java
• Comments explain what each part does– // indicates a comment to the end of the line
• Lots of attention to error handling– People WILL make mistakes– Many are easily corrected