Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Chapter 10 Introduction to Arrays
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Peyman Dodangeh Sharif University of Technology Spring 2014.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
CISC124 - Notices Assn 2 due this Friday. You already know what you need to do this assignment: –Another procedural program. –Uses text file I/O (see Exercise.
objects CIS 421 Kutztown University
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects in Java
CISC/CMPE320 - Prof. McLeod
Java Review: Reference Types
Winter 2018 CMPE212 9/18/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Advanced Programming Behnam Hatami Fall 2017.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CISC101 Reminders Slides have changed from those posted last night…
CISC124 Quiz 1 marking nears completion!
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Object-Oriented Programming Using C++ Second Edition
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CMPE212 – Reminders The other four assignments are now posted.
Introduction to Data Structure
CMPE212 – Reminders Assignment 2 sample solution is posted.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Presentation transcript:

Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1

Passing Arrays by Reference Summary of PassingDemo.java: –Primitive types are passed by value. –Only element by element changes in arrays will “stick”. –Re-assigning the array to a pointer that has local scope in a method will not “stick”. –If you make element by element changes using an aliased local pointer (like the parameter), changes will “stick”. –Strings are immutable, so this does not apply. You cannot make elemental changes inside a String, even though a String is passed by reference. Winter 2016CMPE212 - Prof. McLeod2

Passing Arrays by Reference, Cont. So, mutable Objects (like arrays) can be passed into and out of a method through the parameter list. If a method changes the contents of a mutable Object passed into it – those changes “stick” even when the method is complete. Winter 2016CMPE212 - Prof. McLeod3

Aside - Comparing Objects Testing arrays and Objects for equality (with the “==“ boolean operator) is also interesting: –This test will only give a true when both objects have been aliased, using the assignment operator “=“. –So, even if both arrays have identical contents, “==“ will return false, unless both arrays point to the same location. –This means that comparing Objects with “==“ only compares pointers, not contents. Winter 2016CMPE212 - Prof. McLeod4

Pointers – A Question So, which way is better to declare a 3 by two-dimensional array?: int[][] wayOne = new int[3][10000]; int[][] wayTwo = new int[10000][3]; Or, it makes no difference? Winter 2016CMPE212 - Prof. McLeod5

How Much Memory Does a Pointer Use? String aString = “Hello!”; How much memory does aString consume? Not the string itself, just the pointer to the string? 32 bits for 32 bit Java, 64 bits for 64 bit Java (unless you are using compressed pointers…) Winter 2016CMPE212 - Prof. McLeod6

null Pointer or null Reference null is not a keyword in Java – more like a literal constant. What is a null pointer? What is a null pointer error? Does null have a type? Can you test a pointer to see if it is null ? How? Why would you want to? Winter 2016CMPE212 - Prof. McLeod7

null Pointer or null Reference, Cont. What is the difference between string1, string2 and string3 ?: String string1 = ""; String string2 = null; String string3; Is an unassigned primitive type variable null ? What are the contents of an uninitialized array? Winter 2016CMPE212 - Prof. McLeod8

null References, Cont. The idea of a null reference was first introduced into ALGOL W back in 1965 by C.A.R. Hoare (also known as the inventor of Quicksort). See: References-The-Billion-Dollar-Mistake-Tony-Hoare Winter 2016CMPE212 - Prof. McLeod9

Using null in Java You can test to see if a pointer is null (See file input code, for example. This is how you detect the end of the file.) Sometimes, to widen the scope of a variable you need to declare it before you can instantiate it. In this case you must assign the variable pointer to null. “Bad things” will happen if you do not! A NullPointerException is probably the most frustrating error to encounter in Java! Winter 2016CMPE212 - Prof. McLeod10

What Is An Object? A definition that would apply to any programming language: An entity that exists in an operating computer program that has: –State –Behaviour –Identity Winter 2016CMPE212 - Prof. McLeod11

What Is An Object?, Cont. The State of an Object is the collection of information held in that object. This information may change over time, as a result of operations carried out on the Object. The Behaviour is the collection of operations that an Object supports. The Identity of an Object allows the program access to a specific Object. Winter 2016CMPE212 - Prof. McLeod12

What Is An Object?, Cont. An Object represents real or abstract entities. An Object representing a real entity, for example: –The State is awake, hungry and purring. –The Behavior is scratchingSofa. –The Identity is ginger. Winter 2016CMPE212 - Prof. McLeod13

What is a Class? You can have many (possibly infinite!) different Objects with different values for each State category (or attribute!). But if each of these Objects has the same set of possible behaviours then you can group these Objects together into a Class. For the example, the class would be “Cat”. ginger, felix, sylvester, belle, etc. are all Objects and are instances of Cat. Most (but not all) OO programming languages support the definition of classes. Winter 2016CMPE212 - Prof. McLeod14

What is a Class?, Cont. A class is defined in the source code of a program. It defines: –The operations that are allowed on instances of this class (the methods). –The possible categories of behavior that are allowed for instances of this class (the attributes). Instances of a class are created when the program is running. Winter 2016CMPE212 - Prof. McLeod15

What is a Class?, Cont. The State of an Object instance can be partly or completely defined when the instance is created. The State will likely change when operations are carried out on the instance. However, attributes cannot be added or removed and behavior cannot be added or removed. These are defined in the Class. Winter 2016CMPE212 - Prof. McLeod16

Object Categories In a program Objects will probably fall into one of these general categories: –Tangible things (ex: Cat ) –Agents ( StringTokenizer ) –Events and transactions ( MouseEvent ) –Users and roles ( Administrator ) –Systems ( MailSystem ) –System interfaces and devices ( File ) –Foundational classes ( String ) Winter 2016CMPE212 - Prof. McLeod17

Object Extremes Two extremes of object structure: –Utility classes: All static methods and attributes –The Math class, for example. –You do not instantiate these classes – there is no point. –Customizable classes: All non- static methods and attributes. –Attribute values (some or all) must be set at the time of instantiation before the class can be used. –Scanner class for example. Winter 2016CMPE212 - Prof. McLeod18

Objects, Cont. And many classes fall in-between these two extremes: –A mix of static and non- static methods. –static methods have nothing to do with the attributes and so can be used without instantiation of the class. –Non- static methods depend on the attributes which must be set through instantiation. –Wrapper classes for example: Double, Integer, etc. Winter 2016CMPE212 - Prof. McLeod19

Encapsulation Encapsulation is the process of defining a Class that has at least one customizable attribute. Encapsulation is about the abstraction or containment of the attributes defined in the class. In Java, methods and attributes must be encapsulated or contained in a class definition. We have not yet built any classes with attributes that must be set upon instantiation. Time to start! Winter 2016CMPE212 - Prof. McLeod20

Winter 2016CMPE212 - Prof. McLeod21 Encapsulation, Cont. The Best Object Design: –Supports the re-usability of code. –Provides a well-defined interface to other objects and the user(s). –Builds into an Object the code that ensures the integrity of the data stored in the Object by: Making sure that initial data values are “legal”. Controlling (or preventing) changes to data. Preventing “privacy leaks” when data is returned out of an Object. –Works well with modular design practices making code easier to write, test and debug.

Winter 2016CMPE212 - Prof. McLeod22 Encapsulation, Cont. This is the driving principle for the design of all Objects in the Java API! –So, maybe you should design your own Objects using the same principle? Java API Your class

An Example Consider the general model (or “pattern”...) of a database: Winter 2016CMPE212 - Prof. McLeod23 record 2 record 1 record 3 record 4 record 5 record Collection DatabaseUser Interface Add Delete Edit Sort Find Save Load... Edit Forms Queries Reports... User Each record is an instance of an Object, too.

An Example, Cont. For now, we will focus on just how to define one of the many objects needed for the database – the object that will be used to define the record structure – a “Foundational” Object. If each record only held one piece of data, we would not need an object for each record – the Collection could just be an array. But – more typically – each database record will hold several pieces of data. Winter 2016CMPE212 - Prof. McLeod24

Winter 2016CMPE212 - Prof. McLeod25 An Example, Cont. Suppose you want to create a database to store records of Halloween visits. (Maybe we are going to build a model to predict the number of visitors for given weather conditions so we can predict how much candy to buy!) For each year, you wish to record: –The year. –Number of munchkins banging at your door. –Outdoor temperatures (one reading per hour) in deg C. –Weather condition – “rain”, “snow”, or “clear”.

Winter 2016CMPE212 - Prof. McLeod26 An Example, Cont. What would you do without objects?: You could create four arrays, one for each of the fields. –Difficult to code when moving records around. –Suppose some record swap or delete is messed up and the records get out of sync – how would you know? year numbertempcondition a record: 2D array?

Winter 2016CMPE212 - Prof. McLeod 27 An Example, Cont. It would be easier to design a Class that contains (or abstracts) these four fields and then design another class to collect instances of this Class. –No way the fields could get out of sync since moving a record moves all fields at once. year number temp array condition record 2 record 1 record 3 record 4 record 5 record Collection