280 likes | 471 Views
CSE 501N Fall ‘09 01: OO Concepts + Java Basics. 27 August 2009 Nick Leidenfrost. Lecture Outline . Motivation Some Terminology / History / Architecture Overview From ideas to software Compiled Languages Interpreted Languages Evolution of programming languages
E N D
CSE 501NFall ‘0901: OO Concepts + Java Basics 27 August 2009 Nick Leidenfrost
Lecture Outline • Motivation • Some Terminology / History / Architecture Overview • From ideas to software • Compiled Languages • Interpreted Languages • Evolution of programming languages • Object-Oriented (OO) programming concepts • Basic Java Syntax
Motivation: Why am I here? • 1.) You want to learn to how to program. • 2.) Someone else wants you to learn how to program. • (Hopefully we can turn any #2s out there into #1s) • Even if you never intend to write a computer program after this class, you will leave with: • The core knowledge base to learn and understand almost any programming language • The ability to communicate at a high level and a low level with others about software development
Architecture of a Modern Computer(Simplified) Faster Slower Registers (Very small, very fast) Random Access Memory (a.k.a. RAM a.k.a. Main Memory) CPU Cache (On-Processor Memory) Central Processing Unit (a.k.a. CPU a.k.a. Processor) Hard Disk (a.k.a. Hard Drive a.k.a. Virtual Memory)
The Nuts and Bolts • Bits and Bytes • Who knows what a bit is? • Smallest Unit of storage • 0 or 1 • Who knows hat a byte is? • 8 bits • Capable of storing 256 distinct values • 0 – 255 • -128 – 127 • The storage capacity of any datatype is 2^n, where n is the number of bits in the datatype • Kilobyte: 1024 bytes • Megabyte: 1024 Kilobytes, 1024² bytes • Gigabyte: 1024 Megabytes … etc.
A (very) Brief History of Computing • Once upon a time, shortly after the discovery of fire, people programmed in machine language (binary). • They did this on a number of punch cards that had to be fed, in order, to a computer • Programs were carried around in large boxes • Any rearrangement of these cards could cause errors in computation, extreme panic, and even death.
A (very) Brief History of Computing • Initially, programming in machine language wasn’t too bad, because memory and computing power was limited. • Advancements in hardware enable more complicated programs • Realizing that it was very difficult to program complicated procedures in machine language, we developed High-Level (a.k.a. Human Readable) Programming Languages
Evolution of Programming Languages • Programming in Machine-Level Language (binary 1st Generation) 10111010101010 01000101010101 01010100101001 • Low level languages (2nd Generation) • Assembly MOV AX 2 MOV BX 3 ADD AX BX MOV AX CX • High level languages (3rd Generation) • Procedural languages, e.g., C (Programs are comprised of a collection of methods or functions) int add (int a, int b) { return a+b; } • Object oriented languages, e.g., Java, C++ (Programs are comprised of a collection of interacting Objects and Classes) Math.add(a, b); compiles into
Hardware Vendors Raise the Ante • PC, Mac, Sun, etc • Different CPUs, Different instruction sets • Different Conventions for Memory • Little-Endian / Big-Endian • (where high-order bytes reside in datatypes with multiple bytes) • Other hardware discrepancies
Compiled vs. Interpreted Languages • Compiled (C, C++, VB, etc.) • Human-readable code converted directly to Machine Code (binary) • Faster • Not portable to other platforms • Interpreted (Java, C#) • Human-readable code converted to intermediary instruction set • Common Interface Language (CIL) • .class files (bytecode) • Portable • Non-compiled (scripting) Languages (Perl, PHP, Python, Javascript) • Run inside of a parent application like Interpreted Languages • Do not need to be compiled first, parse code just prior to runtime • Syntax / other errors usually prevent program from running • Cooomparatively Slooooowwww
Program Requirements Program Design From Ideas to SoftwareCompiled Languages (C, C++, VB, etc.) • Code is translated directly to machine executable binary • Each binary result is specific to the type of platform (machine and OS) • E.g., Mac, PC, Unix, Linux • Fast execution • No cross-platform compatibility If (myVar > 1) { … } else { … } Compiler 101001110010101011 001101011111000101 1010110110000100011 1011011101100001010 1011011000101101011 Compiled Program
From Ideas to SoftwareInterpreted Languages (BASIC, Java, C#, etc.) Code is translated to an interim format An interpreter translates between the interim format and machine code Slower execution due to translation Cross-platform compatibility Still require platform specific interpreter Program Requirements Program Design If (myVar > 1) { … } else { … } Compiler #(&$)(*&%%^$^ #$%*%&(%^&*^ %^&*(^&%^#$#$ ^&*%^&$#%^#) %$%^&*&*(&^% Interpreter Compiled Interim Format (.class, CIL, bytecode) 101001111 001101011 000100011
Why Java? • Java is an interpreted Language, so bytecode (.class files) will run anywhere • Java is a high-level Object-Oriented Programming Language (OOPL) • Java has some niceties built into it that make programming easier • Java has some built in “checks” that remove some of the “sharp edges” of other programming languages • Java has Memory management (a.k.a. “Garbage Collection”) • Java has a rich set of online libraries that we can use to make complicated software with relatively little work • Java was around before C# and other peers
Object-oriented ProgrammingHigh-level Entities • Class • Often model real-world objects / relationships • e.g., the class Student • Comprised of Fields and Methods • Defines an internal state • Defines methods to change its internal state • Object • A specific instantiation or example of a class definition • The unit of programming in OO languages • Multiple objects can be created from a single class definition • Ken, Sally are both objects of the class Student • Method (or function) • A means of defining the behavior of the object • May change the state of the object • Is comprised of statements • Accessors / Mutators / Subroutines • Field • Define the state of the object
Object-oriented ProgrammingDifferentiating between Classes and Objects • It is extremely important to be able to distinguish between classes and objects • A class defines the basic features and behavior of a certain type of entity • Think of it as a Cookie Cutter • An object is a specific example of a class • Objects “Ken” and “Sally are instances of the class Student • Key and Sally obey the definition of the general class Student • Internal states of Ken and Sally are independent
Object-oriented ProgrammingMethods and Attributes • Ken • major = “BME” • GPA = 3.75 • totalCredits = 15 • courseLoad = 15 (object) • class Student • Attributes: • major • GPA • totalCredits • courseLoad • Methods • addCourse() { … } • getGPA() { … } • removeCourse() { … } • setGrade() { … } Instances of the class “Student” (object) • Sally • major = “CS” • GPA = 3.0 • totalCredits = 33 • courseLoad = 12
OO Concepts: Abstraction • The act of representing essential features without including how exactly things work internally • Classes use the concept of abstraction to hide implementation details • Abstraction is very important because it allows programmers to… • Write code at a higher level • Not worry about implementation details of sub-modules
OO Concepts: Encapsulation • Increasing stability of a complex program by preventing access to inner workings of its internals • Restricted access means that we can maintain a valid internal state • A “Closed System” would be an extreme example • Encapsulation is a principle benefit of OO Programming, and we have various methods of promoting it
OO Concepts: Inheritance • Basic classes can be used to derive another more complex class definition • Behavior is inherited by subclasses • Behavior can be overridden (changed) • Allow us to reuse code (reduce duplication) • Create class hierarchies • “superclass” = more generic • “subclass” = more specific Person Superclass is-a is-a Teacher Student Subclass
Object of type Student Ken Object of type Teacher Ron OO Concepts: Polymorphism • The ability to interact uniformly with objects of different classes, and have the classes define their behavior • Utilized in conjunction with inheritance to write general and abstract programming solutions Can both be used as… Person
Java Programming Basics • In the Java Programming Language: • A program is made up of interacting objects from one or more classes • A class contains one or more members (fields and methods) • A method contains program statements • We can make “notes” in our code with the use of comments
Java Programming BasicsComments // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */ /** this is a javadoc comment */ /* Comments are ignored by the compiler, and are mainly to help our comprehension of the code. */
Java Programming BasicsProgram Structure // comments about the class public class Calculator { } // comments about the method public int add (int a, int b) { } return a + b; statement statement terminator
Java Programming BasicsProgram Structure // comments about the class public class Calculator { } class header class body Comments can be placed almost anywhere
Java Programming BasicsProgram Structure // comments about the class public class Calculator { } // comments about the field protected int answer;
Java Programming BasicsProgram Structure // comments about the class public class Calculator { } // comments about the method public int add (int a, int b) { } method header method body
Once we learn Java…Other languages C++ C int Calculator::add (int a, int b) { return (a + b); } int add (int a, int b) { return (a + b); } Java public int add (int a, int b) { return (a + b); } C# public void add (int a, int b) { return (a + b); } Javascript function add (a, b) { return Number(a) + Number(b); } PHP function add ($a, $b) { return ($a + $b); }
Conclusion • Questions? • Lab 0 Assigned today, Due Thursday, Sept. 3rd • Sign up for CEC Account • Post to class forum • Compile and Run a Java program with basic tools