Chapters 1&2
Chapters 1&2
95-712
MISM/MSIT
Carnegie Mellon University
Lecture 1: Introduction to OOP
Expected Background
“A one-semester college course in
programming.” (MISM admissions policy)
I assume you can write a program in some
language, understand variables, control
structures, functions/subroutines.
The Eckel CD has background on C.
If in doubt, let’s talk.
Course Outline
Week 1: Background, basics of O-O, first
Java program, programming environments
Week 2: Raw materials: types, variables,
operators, program control
Week 3: Classes: declarations, constructors,
cleanup & garbage collection
Week 4: Packages, access specifiers, finals,
class loading
Course Outline (cont.)
Week 5: Polymorphism, abstract classes,
design patterns
Week 6: Interfaces & extends, inner
classes, callbacks via inner classes
Week 7: Arrays, container classes, iterators
Week 8: More on collections, collections of
references, choosing between types
Course Outline (cont.)
Week 9: Exception handling
Week 10: Java I/O
Week 11: Applets, applications, Swing
Week 12: JDBC, threads
Administrative Details
Professor: Stephen F. Roehrig
Office: Hamburg Hall, 2101A
“Business hours”: 10 am to 10 pm
Email: [email protected]
Course Web sites
– www.heinz.cmu.edu/~roehrig/java/syllabus.html
– www.cmu.edu/blackboard
Blackboard points to the real Web site.
Blackboard used for grades, discussion board.
Administrative Details (cont.)
12 weeks
Two consecutive 80-minute lectures per
week, a break in between the lectures.
Course notes in PowerPoint or HTML,
attached to the syllabus.
Course notes available (usually) Sunday
before class.
Send course questions to Blackboard,
personal questions to me.
Administrative Details (cont.)
Everything is attached to the syllabus.
Don’t look for assignments, etc. on
Blackboard. Look at the syllabus!
Homework usually weekly.
Submission instructions with each
assignment, usually printed listings and a
diskette.
Printing slides? Three to a page, at least.
Save a tree!
Administrative Details (cont.)
Text: Bruce Eckel, “Thinking in Java,” 2nd
edition, Prentice Hall.
Teaching assistants are
– Abhi Anantharaman
– Smit Gupta
– Gautam Sampath Kumar
– Jun Lu
– Poornima Makashir
Office hours TBD
Administrative Details (cont.)
Attendance is not required, but…
…you are responsible for everything said in
class.
I encourage you to ask questions in class.
This is America…you are supposed to ask
questions. Don’t guess, ask a question!
My Policy on Cheating
Cheating means “submitting, without
proper attribution, any computer code that
is directly traceable to the computer code
written by another person.”
I give students a failing course grade for
any cheating. Expulsion is also possible.
This doesn’t help your job prospects.
My Policy on Cheating
You may discuss homework problems with
classmates (although it is not to your
advantage to do so).
You can use ideas from the literature (with
proper citation).
You can use anything from the
textbook/notes.
The code you submit must be written
completely by you.
Even More On Cheating
“In addition to any penalties imposed by the
instructor, including failing the course, all
cheating and plagiarism infractions will be
reported in writing to the Associate Dean for the
program, the Associate Dean of Faculty, the Dean
of Student Affairs, and the Dean. They will
review and determine if expulsion should be
recommended. The report will become part of the
student’s permanent record.”
Course Etiquette
Etiquette is “conduct in polite society”
No cell phones
No random comings and goings
If you are sleepy, go home
If you want to read email or surf the Web,
please do it elsewhere
Programming Language Evolution
Machine language
Assembler
“3rd generation” (COBOL, FORTRAN, C)
Specialized (Lisp, Prolog, APL)
“4th generation” (SQL, GAMS,
spreadsheets, Mathematica)
“5th generation” (example, anyone?)
Why So Many Languages?
Bring the language “closer” to the problem.
But 4GLs are typically focused on
specialized domains (e.g., relational
databases).
We want a language that is general purpose,
yet can easily be “tailored” to any domain.
An inspiration from an odd place…
Simula
Designed to simulate job shops, banks, etc.
To code, you create “classes” (like milling
machines) and “instances” (machine #1, etc.).
All milling machines have properties (how much
time to make Part #1).
All milling machines have abilities (mill to depth
n, move part to position x).
Job shop operations simulated by “making” the
machines, and moving material through them.
What’s Cool Here?
Very close to the problem; domain experts
can pitch in easily.
It’s possible to make new types of machines
(e.g., drill press).
Description of the shop floor is transparent:
+open():void
+lift(height:int):void
+tilt(angle:int):void
-topOn:Boolean
+open():void
+lift(height:int):void
+tilt(angle:int):void
+open():void
+lift(height:int):void
+tilt(angle:int):void
Patron
Two Ways of Reusing Classes
Inheritance: One class is a specialized
version of another (indicated by the triangle
“inheritance” symbol).
BottleOfBeer
BottleOfRollingRock
-lowCalorie:Boolean
+open():void
+lift(height:int):void +isLowCalorie():Boolean
+tilt(angle:int):void
Polymorphism
Different subclasses respond to the same
message, possibly with different actions.
Patron
+beerPlease():Polite
+beerPlease():Rude +beerPlease():InGerman
Some Java Code
Patron p1 = new Patron();
Patron p2 = new YankPatron();
Patron p3 = new BritPatron();
Patron p4 = new GermanPatron();
p1.BeerPlease() // polite request
p2. BeerPlease() // rude request
p3.BeerPlease() // polite request
p4.BeerPlease() // request in German (but polite)
When you “speak of” the variable lect, you are “referring to”
the actual LectureNotes object. When lect goes out of scope
it is automatically destroyed. The LectureNotes object lives
on, but nobody can use it…
Java’s Use of Memory
Registers
Stack
Heap
Static variables
Constants
Non-RAM storage
Java’s Primitive Types
Type Size Wrapper type
boolean - Boolean
char 16-bit Character
byte 8-bit Byte
short 16-bit Short
int 32-bit Integer
long 64-bit Long
float 32-bit Float
double 64-bit Double
void - Void
Wrapper Types
Variables of primitive types are
“automatic”, i.e., they are stored on the
stack.
They are automatically deleted when they
go out of scope.
What if you want an object holding a
primitive type? Example:
char c = ‘x’;
Character C = new Character(‘x’);
Really Big Numbers
BigInteger, BigDecimal
These are arbitrary precision, as big as they
need to be.
You can’t use the usual operators (+-*/)
since they are objects. But there are
methods (functions) to do these things.
How might these be implemented?
Creating New Types
class MyNewType {
// definition here
}
Now it’s legal to say
MyNewType m = new MyNewType();
Class Members
Fields (a.k.a. member variables, data
members)
Methods (a.k.a. member functions)
class MyClass {
int a;
YourClass b;
float memberFunction(int x, float f) {
return 0;
}
}
Let’s Write Something
// Our first program. File: helloDate.java
import java.util.*;