CSE 501N Fall ‘09 01: OO Concepts + Java Basics 27 August 2009 Nick Leidenfrost.
-
Upload
randell-craig -
Category
Documents
-
view
214 -
download
1
Transcript of 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
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
Hard Disk(a.k.a. Hard Drivea.k.a. Virtual Memory)
Random Access Memory(a.k.a. RAMa.k.a. Main Memory)
Central Processing Unit(a.k.a. CPUa.k.a. Processor)
CPU
Cache(On-ProcessorMemory)
Registers(Very small,very fast)
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)
101110101010100100010101010101010100101001
Low level languages (2nd Generation) Assembly
MOV AX 2MOV BX 3ADD AX BXMOV 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);
com
pile
s in
to
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
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
101001110010101011001101011111000101
101011011000010001110110111011000010101011011000101101011
If (myVar > 1) { …}else { …}
Program Design
Program Requirements
Compiler
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
101001111001101011000100011
Interpreter
CompiledInterim Format
(.class, CIL, bytecode)
#(&$)(*&%%^$^#$%*%&(%^&*^%^&*(^&%^#$#$^&*%^&$#%^#)%$%^&*&*(&^%
If (myVar > 1) { …}else { …}
Program Design
Program Requirements
Compiler
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
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 ProgrammingDifferentiating between Classes and Objects
class Student
Attributes:majorGPAtotalCreditscourseLoad
MethodsaddCourse() { … }getGPA() { … }removeCourse() { … }setGrade() { … }
Object-oriented ProgrammingMethods and Attributes
Kenmajor = “BME”
GPA = 3.75totalCredits = 15
courseLoad = 15
Sallymajor = “CS”
GPA = 3.0totalCredits = 33
courseLoad = 12
Instances of the class “Student”
(object)
(object)
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
Student Teacher
Superclass
Subclass
is-a is-a
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
KenObject of type Student
Ron
Object of type Teacher
Person
Can both be used as…
Java Programming Basics
In the Java Programming Language:A program is made up of interacting
objects from one or more classesA class contains one or more members
(fields and methods)A method contains program statementsWe 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
public class Calculator {
}
// comments about the class
public int add (int a, int b) {
}
// comments about the method
return a + b;
statement
statementterminator
Java Programming BasicsProgram Structure
public class Calculator {
}
// comments about the class
class header
class body
Comments can be placed almost anywhere
Java Programming BasicsProgram Structure
public class Calculator {
}
// comments about the class
// comments about the fieldprotected int answer;
Java Programming BasicsProgram Structure
public class Calculator {
}
// comments about the class
public int add (int a, int b) {
}
// comments about the method
method headermethod body
Once we learn Java…Other languages
public int add (int a, int b) {return (a + b);
}
function add ($a, $b) {return ($a + $b);
}
int Calculator::add (int a, int b) {return (a + b);
}
function add (a, b) {return Number(a) + Number(b);
}
Java
C
Javascript
PHP
C++int add (int a, int b) {
return (a + b);}
public void add (int a, int b) {return (a + b);
}
C#