

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Object Oriented Programming, Lecture Notes - Computer Science - Prof Jonathan Shewchuk.pdf, Computer Science, University of California, USA, Prof. Jonathan Shewchuk, Computer Science, Object Oriented Programming
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CS 61B: Lecture 1Friday, August 27, 2010Prof. Jonathan Shewchuk, [email protected] to prof & all TAs at once (preferred): [email protected]’s reading: Sierra & Bates, pp. 1-9, 18-19, 84.Handout: Course Overview (also available from CS 61B Web page)Also, read the CS 61B Web page as soon as possible!>>> http://www.cs.berkeley.edu/˜jrs/61b^ <<<YOU are responsible for keeping up with the readings and assignments.
I won’t always^ remind^ you.^ The^ course newsgroup is required reading:
ucb.class.cs61b Labs----Labs^ (in^275 Soda)^ start^ Tuesday.^
Discussion sections start Thursday. You^ must^ attend^ your^ scheduled lab (as assigned by Telebears) to:1)^ get^ on^ the^ class^ list so we can give you a grade, and2)^ get^ an^ account^ (needed for Lab 1 and Homework 1).You^ may^ only^ attend^ the^ lab in which you are officially enrolled.
If you are not^ enrolled^ in^ a^ lab^ (on the waiting list or in concurrent enrollment), youmust^ attend^ a^ lab^ that^ has space (show up and ask the TA if there’s room foryou).You^ will^ not^ be^ enrolled^ in the course until you are enrolled in a lab.
If you’re^ on the^ waiting^ list and the lab you want is full, you can change to onethat^ isn’t,^ or^ you^ can^ stay on the waitlist and hope somebody drops.If^ you’re not^ yet^ enrolled in a lab, just keep going to them until you find onethat^ has^ room^ for^ you^ (that week).^
Once you get enrolled in a lab, though, you must^ always^ attend^ the^ one you’re enrolled in.Prerequisites-------------If^ you^ have^ not^ taken^ CS^ 61A or E 7, you have two choices:
drop the class, or^ fill^ out^ an^ appeal^ form (in 393 Soda).
You will probably be dropped automatically;^ see^ the^ drop list outside of 379 Soda.
If you’ve taken a data structures^ course^ before, you might be able to skip CS 61B.
See the Course Overview^ and^ Brian^ Harvey (781 Soda) for details.Textbooks---------Kathy^ Sierra^ and^ Bert^ Bates, Head First Java, Second Edition, O’Reilly, 2005.ISBN^ #^ 0-596-00920-8.Michael^ T.^ Goodrich^ and^ Roberto Tamassia, Data Structures and Algorithms inJava,^ Fifth^ Edition,^ John Wiley & Sons, 2010. ISBN # 0-470-38326-7.(The^ first/third/fourth edition is just as good, but not the second.)We^ will^ use^ Sierra/Bates^ for the first month.
Lay your hands on a copy as soon as^ possible.Buy^ the^ CS^ 61B^ class^ reader at Copy Central, 2483 Hearst.
The bulk of the reader^ is old^ CS^ 61B^ exams, which will not be provided online.
The front of the^ reader^ is^ stuff^ you’ll want to have handy when you’re in lab, hacking.
Grading-------10 pts^ Labs^ There^ are^200
points^ total^ you^ can^ earn^ in^ this^
course, 20 pts^ Homeworks^ broken^ down^ at
left.^ 185+^ points^ is^ an^ A+,^ 175-
is 70 pts^ Projects^ an^ A,^ and^ so^
on^ down^ to^ D-^ (75-84).^ There^ is^ NO
25 pts^ Midterm I^ Late^ homeworks
and^ labs^ will^ NOT^ be^ accepted,^ period. 25 pts^ Midterm II^ Late^ projects
are^ penalized^ 1%^ of^ your^ score^ for
every 50 pts^ Final Exam^ two^ hours^ by^
which^ you^ miss^ the^ deadline. -------200 ptsThere will be three projects,^ worth^
20,^ 30,^ and^20 points^ respectively.
You will do the first project individually,
and^ the^ last^ two^ as^ part^ of^ a^ group
of two or three students.^ You^ may^ not
work^ alone^ on^ the^ last^ two^ projects. All homeworks and projects^ will^ be^
turned^ in^ electronically. Cheating--------...will be reported to the^ Office^ of
Student^ Conduct. 1)^ "No Code Rule":^ Never^ have^ a^ copy
of^ someone^ else’s^ program^ in^ yourpossession and never give your program^ to^ someone^ else. 2)^ Discussing an assignment^ without
sharing^ any^ code^ is^ generally^ okay.Helping someone to interpret a compiler^ error^ message^ is^ an^ example
of permissible collaboration.^ However,
if^ you^ get^ a^ significant^ idea^ from someone, acknowledge them^ in^ your^ assignment.3) These rules apply to homeworks^ and^
projects.^ No^ discussion^ whatsoever
in exams, of course.4) In group projects, you^ share^ code^ freely
within^ your^ team,^ but^ not^ between teams.Goals of CS 61B---------------1) Learning efficient data^ structures^
and^ algorithms^ that^ use^ them. 2)^ Designing and writing large^ programs.3)^ Understanding and designing^ data
abstraction^ and^ interfaces. 4)^ Learning Java.THE LANGUAGE OF OBJECT-ORIENTED^ PROGRAMMING===========================================Object:^ An object is a repository^
of^ data.^ For^ example,^ if^ MyList^ is
a ShoppingList object, MyList^ might^ record...your
shopping^ list. Class:^ A class is a type of^ object.
Many^ objects^ of^ the^ same^ class^ mightexist; for instance, MyList and YourList^ may^ both^ be^ shopping^ lists. Method:^ A procedure or function^ that
operates^ on^ an^ object^ or^ a^ class. A method is associated with^ a^ particular
class.^ For^ instance,^ addItem^ might be a method that adds an^ item^ to^ any
ShoppingList^ object.^ Sometimes^ a^
method is associated with a family^ of^ classes.
For^ instance,^ addItem^ might^ operate on any List, of which a ShoppingList
is^ just^ one^ type. Inheritance:^ A class may inherit^ properties
from^ a^ more^ general^ class.^ For example, the ShoppingList^ class^ inherits
from^ the^ List^ class^ the^ property^ of storing a sequence of items.Polymorphism:^ The ability^ to^ have^ one
method^ call^ work^ on^ several^ different classes of objects, even^ if^ those^ classes
need^ different^ implementations^ of the method call.^ For example,^ one^
line^ of^ code^ might^ be^ able^ to^ call^
the "addItem" method on every^ kind^ of
List,^ even^ though^ adding^ an^ item^ to
a ShoppingList is completely^ different
from^ adding^ an^ item^ to^ a^ ShoppingCart. Object-Oriented:^ Each object^ knows
its^ own^ class^ and^ which^ methods^ manipulateobjects in that class. Each ShoppingList^ and^ each^ ShoppingCart^ knows
which implementation of addItem^ applies^ to
it. In this list, the one thing^ that^ truly
distinguishes^ object-oriented^ languages from procedural languages (C,^ Fortran,
Basic,^ Pascal)^ is^ polymorphism.
Java----Java^ allows^ you^ to^ store^ data in variables, but first you must declare themand^ specify^ their^ type.Scheme:^ (let^ ((x 1)) )^
Java:^ int x;x = 1; This^ declaration^ does^ two things.(1)^ It allocates^ a^ chunk of memory big enough to store an integer, whichJava^ calls^ type^ "int".(2)^ It names^ the^ variable (chunk of memory) "x".Variables are^ used^ not^ just to store numbers, but also to reference objects.There^ are two^ ways^ to^ get classes of objects to play with:(1)^ Use^ one^ defined^ by^ somebody else.
Java has tons of pre-defined classesyou can use. Many come in the "Java standard library" provided with everyJava compiler. (2)^ Define^ your^ own.For^ example,^ Java^ has^ a^ built-in class called String.String^ myString;This^ does not^ create^ a^ String object.
Instead, it declares a variable (chunk of^ memory)^ that^ can^ store a reference to a String object.
I draw it as a box.---myString^ |^ |^ <-- This box is a variable (not an object).---Initially,^ myString^ doesn’t reference anything.
You can make it reference a String^ object^ by^ writing^ an assignment statement.
But how do we get ahold of an^ actual String^ object?^ You can create one.myString^ =^ new^ String();This^ line performs^ two^ distinct steps.
First, the phrase "new String()" is called^ a^ constructor.^ It constructs a brand new String object.
Second, the assignment^ "="^ causes^ myString to reference the object.
You can think of this^ as^ myString^ pointing to the object.---^ ------myString^ |.+---->|^ | a String object---^ ------Unlike^ Scheme^ and^ Matlab, Java programs must be compiled before you can runthem.^ Compilation^ converts your written code to a machine-readable bytecode.The^ advantage^ is^ a^ faster program than one written in Scheme.
The disadvantage is^ that^ you^ have^ to^ wait^ for it to compile.Scheme^
Java------ ----Scheme program (.scm) Java program (.java)| || eval | javacv vAnswer .class files|| javavAnswer
Postscript:^ Anatomy of a Small^ Java
Program^ (for^ discussion^ section) --------------------------------------------Assume the following code is^ in^ a^ file
called^ HelloWorld.java: class HelloWorld {public static void main (String[]
args)^ {System.out.println("Hello, world");} } The classes are "HelloWorld",^ "String",
and^ "System". The objects are "args", "System.out",
and^ the^ string^ "Hello,^ world". (Actually, the first two^ of^ these^ are
references^ to^ objects.) The methods are "main" and^ "println".
The^ println^ method^ prints^ its^ parameter, and the main method prints^ the^ string
"Hello,^ world". You don’t have the knowledge^ to^ completely
understand^ this^ code^ yet,^ but^ don’t worry about it.^ We’ll take^ the^ first
two^ lines^ on^ faith^ as^ the^ standard
way^ to start a Java program.^ They’ll^ become
clear^ in^ the^ coming^ weeks. Let’s look at the innermost^ line^ first;
it^ does^ all^ the^ action.^ "out" references an object whose^ class^ is
PrintStream.^ A^ PrintStream^ is^ a^ path
by which characters can be output^ by^ a
program.^ The^ characters^ that^ are^
sent through "out" find their way^ to^ your
computer^ screen. System is a class which happens^ to^
contain^ the^ variable^ out^ (among^ many
other variables).^ We have to write^ "System.out"
to^ address^ the^ output^ stream, because other classes might^ have^ variables
called^ "out"^ too,^ with^ their^ own meanings."println" is a method (procedure)^ of
the^ class^ PrintStream.^ Hence,^ we^
can invoke "println" from any PrintStream
object,^ including^ System.out.^ "println" takes one parameter, which^ can^ be^ a
string. "main" is a method in the "HelloWorld"
class.^ The^ HelloWorld^ class^ knows
how to do the "main" operation,^ just^ like
the^ PrintStream^ class^ knows^ how^ to
do^ the "println" operation.
|^ ---^ |^ |^ println^ (method)
|^ out |.+-----+-->|^
|^ |^ main^ (method)^ | --System (class)--^ --An^ object^ of
class--^ |^ |PrintStream^ |^ |---HelloWorld^ (class)--- The classes System and PrintStream^
are^ all^ automatically^ provided^ for^
you^ by any Java compiler.^ Somebody^ has^ programmed
them^ for^ you,^ so^ that^ you^ don’t have to figure out for yourself^ how
to^ send^ characters^ to^ the^ terminal.