Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

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.
Road Map Introduction to object oriented programming. Classes
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Peyman Dodangeh Sharif University of Technology Fall 2013.
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.
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.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
Defining Your Own Classes II
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.
Lecture 3: Introduction to Object and Classes
Last Time Math class Style and Documentation Loops And Exercises!
Methods Attributes Method Modifiers ‘static’
objects CIS 421 Kutztown University
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
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
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.
CISC/CMPE320 - Prof. McLeod
Advanced Programming Behnam Hatami Fall 2017.
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
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.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
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.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CISC124 Assignment 4 on Inheritance due today at 7pm.
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. 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:
CISC/CMPE320 - Prof. McLeod
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
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,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
CMPE212 – Reminders The other four assignments are now posted.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 3 due next Friday.
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.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
OO Programming Concepts
CISC/CMPE320 - Prof. McLeod
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Chapter 7 Objects and Classes
Presentation transcript:

Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders First quiz in the lab this week, Wednesday section still to write. More info in last Tuesday’s lecture. Assignment 1 due this Friday. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today Continue with Aliasing and Passing by Reference. Winter 2019 CMPE212 5/10/2019 Today Continue with Aliasing and Passing by Reference. Use of null in Java. Start OOP by considering: “What is an Object?” Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Aliasing Objects - Array Example int[] first = {1, 2, 3, 4, 5}; int[] second = {10, 20, 30, 40, 50, 60, 70}; 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0960ff .length 5 .length 7 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. second = first; // Aliasing! 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0480ff .length 5 .length 7 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. // after garbage collection Poof! 1 2 3 4 5 0480ff int[] first 0480ff int[] second 0480ff .length 5 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. first[4] = 500; // second[4] is also 500 1 2 3 4 500 0480ff int[] first 0480ff int[] second 0480ff .length 5 Winter 2019 CMPE212 - Prof. McLeod

Aliasing Objects - Array Example, Cont. Fall 2013 CMPE212 Aliasing Objects - Array Example, Cont. So, setting one array to equal another as in: array1 = array2; sets array1 to point to the same data memory location that was (and still is) pointed to by array2. Now, changing the value of an element in array2 will change that same element in array1, or visa-versa - this makes sense since both array Objects point to the same set of data values in memory! Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Aliasing Objects Passing an Object into a method results in the method’s parameter being aliased to the Object passed. Called “Passing by Reference”! Winter 2019 CMPE212 - Prof. McLeod

Passing Parameters by Reference For example, in main: int[] arrayA = {1, 2, 3, 4, 5}; passArray(arrayA); // invoke passArray The passArray method: public static void passArray(int[] arrayB) { // arrayB is aliased to arrayA from main // making elemental changes to arrayB will // also change elements in arrayA in main arrayB[3] = 400; } // end passArray // arrayA[3] is 400 in main Winter 2019 CMPE212 - Prof. McLeod

Passing Parameters by Reference, Cont. The rule for parameter passing into methods is: Objects are passed by reference, primitive types are passed by value. See PassingDemo.java Has a method with two parameters - an array and an int - which one(s) will stay changed? Instead of going element by element, if you re-assign the array to another array within the method, what happens? Does this rule apply to Strings, as well? Winter 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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

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 2019 CMPE212 - Prof. McLeod

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: http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare Winter 2019 CMPE212 - Prof. McLeod

Using null in Java You can test to see if a pointer is null. Use == null Sometimes, to widen the scope of a variable you need to declare it before you can instantiate it. Often, the compiler will insist that you assign the variable to something, which at this early stage will be null. A NullPointerException is probably the most frustrating error to encounter in Java! Winter 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

What Is An Object?, Cont. An Object represents real or abstract entities. An Object representing a real entity, for example: The State is awake and curious. The Behavior is helping. The Identity is minx. An instance of Cat. Winter 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 state that are allowed for instances of this class (the attributes). Instances of a class are created when the program is running. Winter 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

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 2019 CMPE212 - Prof. McLeod

Object Extremes Two extremes of object structure: Fall 2013 CMPE212 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 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

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 2019 CMPE212 - Prof. McLeod