Introduction ObjectOriented Programming Behavioral Abstraction Compilation - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Introduction ObjectOriented Programming Behavioral Abstraction Compilation

Description:

the necessity to visually validate the logic. Today we will give some basic ... A traditional problem has been the necessity to have different versions of a ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 52
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Introduction ObjectOriented Programming Behavioral Abstraction Compilation


1
IntroductionObject-Oriented ProgrammingBehaviora
l AbstractionCompilation
Lecture 17
2
IntroductionObject-Oriented Programming
3
The Journey So Far
Procedural Programming
using Pseudocode
You are here
4
The Destination
Procedural Programming
Object Oriented Programming
using Pseudocode
using Java
5
Introduction
  • We are going to introduce two concepts at the
    same time
  • Object-Oriented Programming
  • Programming in a real language
  • Why?
  • Pseudocode is a good preparation for learning any
    language.
  • The Object Oriented paradigm is a significant
    factor in modern programming
  • We feel that an introductory course should at a
    minimum give students a good taste of real
    programming
  • Many students have asked for exposure to a real
    language
  • Excellent preparation for CS 1312

6
Introduction
  • We will not abandon the principles you have
    learned so far
  • modularity
  • abstraction
  • the necessity to visually validate the logic
  • Today we will give some basic ideas about
  • Object Oriented Programming
  • Working with a real language Java

7
Object Oriented Programming
  • A different programming style
  • A different way of thinking
  • Origins
  • Problems with very large systems becoming
    unmanageable (gt 100,000 lines of code)
  • Graphical User Interfaces (e.g. Macintosh,
    Windows) required a different way of programming
    due to complex interactions of windows, mouse
    clicks, etc.
  • Xerox PARC Smalltalk
  • C as an enhancement to C
  • Java as a Web language

8
To be more specific...
9
The Scenario
  • Recall the concept of a Queue
  • Defined by a set of behaviors
  • Enqueue to the end
  • Dequeue from the front
  • First in, first out

Items
10
Where is the Queue?
  • The logical idea of the queue consisted of
  • Data stored in some structure (list, array, etc.)
  • Modules to act on that data
  • But where is the queue?
  • Wed like some way of representing sucha
    structure in our programs.

11
Why?
  • We would like to have reusable drop-in
    programming components we could use to solve
    problems.
  • We would like to encapsulate the data and the
    behaviors in order to protect it from misuse.
  • We would like clear interfaces between different
    parts of programs
  • We would like a programming system that was
    extensible
  • We would like a programming language that would
    be excellent for modeling real world objects

12
Issues
  • As is, there is no way to protect against
    violating the specified behaviors.

Procedure Enqueue
Procedure Dequeue
The Queue Data
Sneak into Middle
13
Issues
  • If our queue could somehow be packaged we could
    drop in a queue object whenever we needed it.

Queue
Acme Manufacturing
14
Issues
  • Wed like a way to put a wrapper around our
    structure to protect against intrusion.

Sneak into Middle
15
Issues
Contract The party of the first part hereinafter
known as the queue agrees to... The party of the
second part hereinafter known as the application
agrees to...
Clear lines of responsibility
16
Issues
  • I need a queue that can tell me how many items it
    contains...

Queue
Enqueue
Dequeue
Acme Manufacturing
Magic Process
17
Issues
Elevator
Signal Object
Elevator Object
Student Object
Car Object
BankAccount Object
Real World Objects!
18
Questions?
19
Abstraction in Object-Oriented Programming
20
Procedural Abstraction
  • Procedural Abstractions organize instructions.

Function Power Give me two numbers (base
exponent) Ill return to you baseexponent ???
Implementation ???
21
Data Abstraction
  • Data Abstractions organize data.

StudentType
Name (string) GPA (num) Letter Grade
(char) Student Number (num)
22
Behavioral Abstraction
  • Behavioral Abstractions combine procedural and
    data abstractions.

23
The Object-Oriented Paradigm
  • Instances of behavioral abstractions are known as
    objects.
  • Objects have a clear interface by which they send
    and receive messages (communicate).
  • OO is a design and approach issue. Just because
    a language offers object-oriented features
    doesnt mean youre doing OO programming.

24
Information Hiding
  • Information Hiding means that the user has enough
    information to use the interface, and no more

Example Stick shift We dont care about the
inner workings... We just want to get the car
going!
25
Encapsulation
  • Encapsulation is the grouping of related things
    together within some structure

Item 1
Item 2
Item3
26
Encapsulation via Algorithms
  • Algorithms encapsulate modules, data, and
    instructions.


Algorithm
Procedure
Instructions
Function
Data
27
Encapsulating Instructions
  • Modules encapsulate instructions.


Procedure/Function
Instructions
Instructions
Module call
Instructions
28
Encapsulating Data
  • Records allow us to do this with data

Record
Field 1
Field 2
Record
29
Revisiting the Question Where is the Queue?
  • Notice we still have no way of identifying the
    idea were discussing
  • The Queue is still in the ether.
  • Wed like to encapsulate the data (regardless of
    its actual representation) with the behavior
    (modules).
  • Once we do this, weve got a logical entity to
    which we can point and say, there it is!

30
Encapsulating Data with Methods
  • Abstract data types (ADTs) allow us to
    encapsulate logically related data and modules

Queue
Enqueue
Data
Dequeue
31
Abstract Data Types
  • An idea, a concept, an abstraction of the big
    picture
  • It encapsulates the abstract behaviors and data
    implied by the thing being modeled.

32
Achieving Behavioral Abstraction
  • Abstract data types (ADTs) are concepts.
  • We require some way to implement these common
    abstractions so we can write them once, verify
    that they are correct, and reuse them.
  • This would save us from having to re-do work.
    For example, every time we create a queue we did
  • List_Node definesa ...q_front isoftype
    ...q_tail isoftype ...
  • procedure Enqueue(...)
  • procedure Dequeue(...)
  • We need an algorithmic construct that will allow
    us to bundle these things together the class.

33
OO Summary
  • Behavioral abstraction combines data abstraction
    with procedural abstraction.
  • The object-oriented paradigm focuses on the
    interaction and manipulation of objects.
  • An Abstract Data Type (ADT) allows us to think of
    what were representing as a thing regardless of
    its actual implementation.

34
Questions?
35
Compiling and the Java Virtual Machine (JVM)
36
Working with a real language
  • The syntax of Pseudocode is pretty loose
  • visual validation encourages a permissive
    approach
  • emphasized the importance of structure
  • Compilers essentially require perfection.
  • The procedure for writing your algorithms wont
    change (much!)
  • Write a high level approach focusing on
    algorithm.
  • Write the code concentrating on details.
  • Check it by hand before compiling.
  • Because we're introducing a real language at the
    same time as we're introducing the object
    oriented paradigm we will require you to just
    write some code.
  • You may not understand it all immediately
  • Eventually we'll explain!

37
The First Part
  • Initially well use Java to write procedural code
    just to let you get the feel of a real language.
  • In fact, well start by programming some of the
    same types of modules that we used in the
    procedural pseudocode earlier in the semester.
  • Then, as we introduce the Object Oriented
    paradigm well use Java as it was intended.

38
About Java
39
Introduction to Java
  • What Java is
  • A real professional programming language
  • (which is good and bad...)
  • Portable across any hardware platform that has a
    JVM interpreter (more later)
  • An object-oriented language
  • What Java is not
  • The Ultimate Programming Language
  • HTML or another web-content language
  • Only useful for web applets
  • Just Another Vacuous Acronym

40
Real Languages
  • Real languages require the programmer to write
    code using a very specific set of rules, keywords
    and syntax.
  • Then this code is transformed into actual
    instructions that a specific machine can execute.
    Often a complex process
  • A number of strategies have been invented to
    accomplish this process
  • Assemblers
  • Compilers
  • Interpreters
  • A traditional problem has been the necessity to
    have different versions of a program for
    different machines.

41
A New Idea
  • Java was specifically developed to be able to
    write code once and run it anywhere
  • How is this magic accomplished?
  • Using an intermediate language! Byte code.
  • The Byte code is interpreted (executed) using a
    special piece of software (a program) called the
    Java Virtual Machine (JVM)

42
Compilation
Source Code .java
Java compiler
Generic Byte Code .class
OS-specificJVM interpreter
OS-specific Object Code
Execute program
Need one of these for every different OS
43
Structure of Java Programs
  • Initially well write programs that fit in one
    file.
  • Create the file with an editor (e.g. emacs)
  • Compile with javac producing a .class file
  • Execute by running java and sending it the .class
    file
  • Our first (tiny) program will be roughly like a
    cross between an algorithm and a procedure.
  • Lets take a look...

44
Sample Application
We create a file (using an editor) called
HelloWorld.java
  • public class HelloWorld
  • public static void main(String argv)
  • System.out.println(Hello World!)

We compile by typing (at the OS prompt) javac
HelloWorld.java Which produces HelloWorld.class Th
en we execute by typing java HelloWorld
Hello World!
45
Demo
  • gtjavac HelloWorld.java
  • gtjava HelloWorld
  • Hello World!
  • gt

46
Quick Trix
  • The name of the file must match the name of the
    class EXACTLY!!!
  • File Bubba.java
  • Contains
  • Everything must be EXACTLY correct!!!

Capitalization counts
class Bubba ...
47
Eventually...
  • Applications (normal computer programs)
  • Each program consists of multiple files.
  • Each file contains a class.
  • Each class will contain modules which will
    resemble procedures and functions.
  • THE MODULES WILL BE CALLED METHODS
  • The .class file that you send to java must
    contain a method named main
  • It will actually look like this
  • public static void main(String argv ) ..
  • the JVM will use the file naming convention to
    find the other classes required by this main
    program.

As opposed to Applets
48
Some basic syntax issues
  • Your TA is smarter than the java compiler
  • Lines need to terminate with a
  • Easier said than done
  • Braces will be used to indicate "blocks" of code
  • Which essentially act like a single line

public class HelloWorld public static void
main(String argv) System.out.println(He
llo World!)
49
A look ahead...
  • Pseudocode
  • if some_boolean then
  • a lt- a 1
  • else
  • a lt- a 2
  • b lt- 7
  • endif
  • Java
  • if(some_boolean)
  • a a 1
  • else
  • a a 2
  • b 7

Note placement
Note Indentation is used in both cases. Means
nothing to compiler. instead of lt- (more
later) Must have parens in Java
50
Questions?
51
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com