Popular in Course
verified elite notetaker
Popular in Computer Information Technology
This 48 page Class Notes was uploaded by Kathleen Cartwright on Monday September 28, 2015. The Class Notes belongs to CIS120 at University of Pennsylvania taught by Staff in Fall. Since its upload, it has received 15 views. For similar materials see /class/215376/cis120-university-of-pennsylvania in Computer Information Technology at University of Pennsylvania.
Reviews for PROGLANG&TECHI
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/28/15
Programming Techniques Jugg mg bject referermes lntmductim WRAPPING UP ARRAYS 12510 CIS 120 Review Java arrays I Index elements from O I The ith element of array a ai I Create an array a of size n with elements of type type type a new typen int a new int4 a2 7 1 1711 O 1 2 3 12510 CIS 120 For loop details initialization loop condition update for int i 0 i lt alength i total ai loop body tame 590 Array processing recipe I Given an array I Goal apply a processing step to each array element I Ingredients I an array index number of elements I a for loop for int index O i lt array length index process arrayindex 12510 CIS 120 Array bounds check I Access to a i requires 0 Silta length I Java checks each access and reports an exception if the index is out of bounds gt int a 1 2 gt a2 3 javalangArrayIndexOutOfBoundsException 2 at I There39s a lot more to learn about exceptions 12510 CIS 120 Array details I Empty array convenient for boundary cases double empty new doubleO emptylength 2 0 I Array initializer int oneTonhree 1 2 3 oneTonhreelength gt 3 oneTonhree1 gt 2 12510 CIS 120 Arrays of any type I Arrays can be made for any Java type int 3 1 3 5 7 5 Point p new Point12 a E p new Point34 O 1 2 3 O 1 String s quotaquot quotbquot quotcquot Point Point E x 10 x 30 s y 20 y 40 0 1 2 quotaquot quotbquot quotCquot 12510 CIS 120 8 Arrays are reference types I Assignment does not copy arrays int a 1 3 5 7 intba 1957 b 012 3 a19 12510 CIS 120 12510 JUGGLING OBJECT REFERENCES CIS 120 Objectoriented programming I More than just data structuring and memory management I Tie computation to the objects it works on Real world Program Entity Object Entity attributesstate Instance variables State functions Accessor query methods State change Mutator command methods 12510 CIS 120 12 12510 Objectoriented modeling I Data interconnected objects I Behaviors organized as methods in the classes of these objects I Program a collection of interacting classes I Goal Recognize and model useful relationships among object classes CIS 120 State encapsulation I The idea of GOP is that all instance variables are accessed and modified through methods I An object encapsulates a set of variables that change state in coordination 12510 CS120 14 Java is a compromise I Values of primitive types are not objects I Everything else is I Reason objects are expensive C designed later made the other decision public class Integer private int value public Integerint val value v 1 public int getValue return value Cl l Integer value 1 Integer i new Integer1 12510 CIS 120 Methods review I Invoked on an object instance of the class in which they are defined Have a reference to this object I Can refer to instance variablesmethods of the entire class without qualification 12510 CS120 16 Classes ReviewPoint public class Point private double x y public Pointdouble anX double aY x anX y aY public double getX return x public double getY return y public void movedouble dx double dy xdx y dy 12510 CS120 17 Invoking Methods Point p new Point00 pmove 10 Systemoutprintln II II 12510 CIS 120 Referring to instance variables Within a method this refers to the object on which the method was invoked public void movedouble dx double dy x dx y dy Really means public void movedouble dx double dy thisx dx thisy dy 12510 CS120 19 If time permits Returning an object reference I Example a more flexible version of move public Point movedouble dx double dy x dx y dy return this Point p new Point11 pmove1lmove22getX2340 I Read xyz as xyz pmove11move22getX 12510 CIS 120 20 12510 MEMORY CIS 120 21 What is memory I Abstract picture of the current state of the machine I What level of abstraction How detailed should the model be 12510 CIS 120 22 12510 Randomaccess memory The fundamental data model of modern computers A linear array of locations memory cellswordsbytesl Each location has the same size number of bitsbytesl Each location is designated by an integer address 0memory size1 Load from an address Store at an address CIS 120 23 12510 Memory details theoretical randomaccess memory 2345678 0 1 actual randomaccess memory 2P2 2P1 quot39Dj 012345678 Real memory isn t infinite Modern computers have memory sizes that are multiples of4 bytes or 8 bytes CIS 120 25 Naming memory I Programming in terms of actual memory addresses is very painful machine language I Give names to particular locations variables I Arrays indices name locations relative to array I Objects instance variables name locations relative to the object s location public class Point public float x y float a14 Point p2 p2 y refers to location 5 0 1 x y 1o 05 00 20 1051 O 1 2 3 4 5 6 7 8 Memory management I How are new objects allocated I Language s runtime system I Manages pool of available memory I Finds free memory blocks to hold new objects I Returns memory blocks of discarded objects to the free pool I Garbage collection automatically discover objects that nothing refers to and discard them I Java has garbage collection built in I C for example does not 12510 CIS 120 27 Where are variables stored I Instance variables I belong to objects I last as long as the object lasts I can be accessed by all methods of the class I different instances of a class use different copies of a given instance variable I Local variables I belong to method invocations I last an invocation of a method I can only be accessed within the method that declares them I different invocations of the same method use different copies of a given local variable 12510 CIS 120 28 Activation records I Keep track of method invocation data I quotthisquot I Parameter values I Method local variables I Where to return to I What to return I Dynamic storage areas I Heap objects I Stack activation records 12510 CIS 120 29 A common pattern swap I A juggler holds a ball in each hand I Swap the balls I First buggy attempt public class Juggler private Ball left private Ball right public void swap left right right left Why 12510 CIS 120 30 A common pattern swap I A juggler holds a ball in each hand I Swap the balls public class Juggler private Ball left W W private Ball right public void swap Ball middle left IeT W left right right middle C public void juggleint n left W while n gt O swap O O 12510 CIS 120 left quotgm obj new Juggler stack heap malxmw 12510 CIS 120 objjuggle2 Activation record of method invoca on 12510 stack jugge heap CIS 120 33 whilen gt 0swap stack heap juggle uwa z l 12510 CIS 120 12510 Ball middle left stack Juggler CIS 120 35 left right stack Juggler 12510 CIS 120 12510 right middle stack Juggler CIS 120 37 12510 Returning from swap stack jugge heap CIS 120 38 whilen gt 0swap stack heap juggle mwa a IE 12510 CIS 120 12510 Ball middle left stack Juggler CIS 120 40 left right stack Juggler 12510 CIS 120 12510 right middle stack Juggler CIS 120 42 12510 Returning from swap stack Juggle heap CIS 120 43 Returning from juggle stack heap 12510 CIS 120 Complex relationships between objects I Many realworld situations involve I Multiple interacting parts I Object attributes that Derive from other object attributes Depend on features of the object and its associates 12510 CIS 120 45 Point again public class Point private double x y public Pointdouble anX double aY x anX y aY public double getX return x public double getY return y public void movedouble dx double dy x dx y dy 12510 CIS 120 46 Building on Point I A circle is defined by its center a point and its radius a double public class Circle private Point center A circle has a center private double radius public CirclePoint center double radius thiscenter center this radius radius public Point getCenter return center public double getRadius return radius 12510 CIS 120 47 12510 The has a pattern I A circle has a center I A juggler has a left ball and right ball I A person has an address I A song has a performer An object with an instance variable that refers to another object CIS 120 48 Circle has a Point Point p new Point1 2 Circle 0 new Circlep 5 Pi E 7 Point x 10 c EIN Circle y 20 center radius 05 cgetCentergetX gt10 12510 CIS 120 49 Moving a Circle l Delegate the move to the circle s center public class Circle private Point center private double radius public void movedouble dx double dy center mmweldx dy 12510 CIS 120 50
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'