CSE115 / CSE503 Introduction to Science I Dr. Carl Alphonce 343 Davis Hall [email protected] Office hours: Tuesday 10:00 AM – 12:00 PM* Wednesday 4:00 PM – 5:00 PM Friday 11:00 AM – 12:00 PM OR request appointment via e-mail

*Tuesday adjustments: 11:00 AM – 1:00 PM on 10/11, 11/1 and 12/6 ANNOUNCEMENTS

© Dr. Carl Alphonce ANNOUNCEMENTS Main Bring running are Recitations(Baldy21) www.cse.buffalo.edu/faculty/alphonce/cse115/ course your your UB website: card © Dr. Carl Alphonce Carl © Dr. ANNOUNCEMENTS zyBook ~16% of class not currently getting those those points ofclassgetting not ~16% currently 626 students Courseenrollment: students enrollment: 523 Book your grade).(minimum course of10% grade towards count Exercisesfrom book usage © Dr. Carl Alphonce Carl © Dr. ELECTRONICS: off & away

© Dr. Carl Alphonce ROADMAP Coming up Coming Today timeLast variables in depth variables in diagrams object method calls variables classdefinitions diagrammemory expressions and and objects © Dr. Carl Alphonce Carl © Dr. REVIEW

© Dr. Carl Alphonce Objects Objects OO behaviors: properties: software are of objects. software systems systems interacting breakfast?” able to reply e.g. being to the question did for“What you have these are things objects do foryoue.g. what had breakfast these are things that objects know have Expression evaluation we can remember a reference rememberreference can wea valuestoring inby it evaluating causes a causes produces new example1.BarnYard() new side effect a value value a (which we call a reference) a call we (which (an objectis(an created and initialized) a variable a evaluating a ‘new’ expression When evaluating an expression like ‘new example1.BarnYard()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four bytes for the sake of this example). 107 used Next, new must secure a contiguous block 108 10101010 of memory four bytes large, to store the representation of the object. 109 10101010 110 10101010 Bit strings representing the object are 111 10101010 written into the reserved memory locations. In this example we use “10101010” to 112 available indicate that some bit string was written 113 available into a given memory location; the exact bit 114 available string written depends on the specific details of the object. 115 used

The starting address of the block of memory holding the object’s representation is the value of the ‘new’ expression. This address is called a ‘reference’. Expression evaluation we can remember a reference rememberreference can wea valuestoring inby it evaluating causes a causes produces new example1.BarnYard() new side effect a value value a (which we call a reference) a call we (which (an objectis(an created and initialized) a variable a The variable declaration Examples minimallyof consists Declaration be Variablesmust name type specifiesdeclarationsize specifiesdeclarationencoding scheme example1.Chicken c ; example1.BarnYard declared by by ; before use terminator semicolon The is a ‘;’ . assignment statement Example ASSIGNMENT is‘=’ (it isthe OPERATOR SYNTAX: statement To associate avalue with by by “by “by “by new = example1 is assigne is assigne is assigne : d d d a reference to a new a new to reference a a new to reference a ‘new example1 of expression the value the .BarnYard( = < = a variable, variable, a expression> ); BarnYard example1.BarnYard( use an use object ; ” (example1 is implied) not assignment assignment ) object” object” .BarnYard( ‘equals’!) … or ) ’ ” ” ’ … … or … MOVING ON

© Dr. Carl Alphonce Classes vs. Objects Classes are instantiated Classesareinstantiated An Developers placed files.java placed in object is an isan write write instance instance class definitions. class at at of runtime classa . . What is a class definition? of the ofhave the class will and (variables) A classdefinition behaviors behaviors is a description properties description isa of the . (methods) (methods) that instances instances Our first class definition! For majoridentifyFor parts: wenow the each of inclass. nextand detaildiscussit part Here’s classWe minimal a definition. label will } public class package lab2; } public Farm( Farm Farm ) { { Syntax Package declaration } public class package lab2; } public Farm Farm isshownin Farm( { ) { green: Packages reduces chance of naming naming conflicts reduces chance of packages classbe names to allowsre a package package isan organizational related related are classes together grouped - used different in used mechanism Packages name and its (unqualified) classname: name (unqualified) itsand class’ a analogy: one BarnYard example1.BarnYard area package::class code::phone qualified fully is an unqualified class nameunqualifiedclassisan number is a fully qualified class namefullyclass isa qualified consists of its package package ofits consists Folder structure on disk examples: other within nested packages can each be system the file folder a (directory) each to package corresponds com.sun.accessibility.internal.resources.accessibility javax.swing.text.html.parser java.rmi.registry to nested corresponds diskonfolder i n Syntax The } public class package class definition lab2; } public is shown inisshown Farm Farm Farm( { green ) { : Syntax The class of The consists definitiona } public class package lab2; } public Farm Farm Farm( { ) { header . . . Syntax . . and a } public class package body lab2; } public : Farm Farm( { ) { Syntax constructor definitionconstructor the consists Inbodya example,this of single } public package class lab2; } public Farm : Farm( { ) { Syntax The constructor constructor of The consists definitionsa } public package class lab2; } public Farm Farm( { ) { header . . . Syntax . . and a } public package body class lab2; } public : Farm Farm( { ) { This was the the was This last slide slide last afternoon afternoon lecture. for the the BACK TO OBJECTS method say sometimeswe ‘ object a to ‘send message’ object to an we object behaviors ’ invoke a method a ’ rather than ‘ callamethod call onthe a Object communication > new example1.BarnYard(). method. method iscalled toobjecta onA thewhichusingreference we the call object. example1.Chicken an to reference a with “ the call object, putexample1.BarnYard example1.Chicken objectToinside an an addChicken addChicken ” method of the example1.BarnYard object (new example1.Chicken()) The method call from last time new example1.BarnYard(). new example1.BarnYard(). addChicken (new example1.Chicken()) (new Anatomy of a METHOD CALL . An expression whose value is a reference to to valueobject. anexpressionreference awhoseAnis A period ‘.’, calledA period ‘dot’ ‘.’, or operator’. the ‘member access that the reference before the ‘.’ the‘.’ before refers thethat reference to. bethe for of mustobjectThe type defined method method. The of aname ( < ( expr This is an argument argument This anlist.is Ainside. setwith of expressionparenthesesan > ) > Dissecting a method call new example1.BarnYard(). new example1.BarnYard(). An expression whose value is a reference to to valueobject. anexpressionreference awhoseAnis h mme ces operator’ The ‘member access addChicken The name of method. The of thename (new example1.Chicken()) (new contains one expression.containsone list theexample In this argument list.An