February 28, 2013 COMP 110-003 Introduction to Programming Objects and References Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.

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

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Catie Welsh March 2,  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 41 Defining Classes and Methods Chapter 4.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Values, variables and types © Allan C. Milne v
CPSC150 Spring 2007 Dr. L. Lambert. CPSC150 Overview Syllabus Use Textbook, ask questions, extra thorough, I will post sections covered All information.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
Exam Review 10/01/2014 Happy October. The Exam  Will be in Canvas  Two parts  Part A is recall – closed book, closed notes ◦Quizzes, in class activity.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
CS 100Lecture 221 Announcements P5 due Thursday Final Exam: Tuesday August 10, 8AM - 10AM, Olin 155 Want references? Remember to drop off a picture and.
COMP 110 Some more about objects and references Luv Kohli October 10, 2008 MWF 2-2:50 pm Sitterson 014.
Java: Base Types All information has a type or class designation
COMP Review of Chapter 1 & 2
Computer Organization and Design Pointers, Arrays and Strings in C
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 7 User-Defined Methods.
Java: Base Types All information has a type or class designation
COMP Objects and References
COMP 110 Review for midterm exam
Selenium WebDriver Web Test Tool Training
Java Review: Reference Types
Computers & Programming Languages
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
CS 200 Primitives and Expressions
Object Oriented Programming Review
Java Programming Language
Names of variables, functions, classes
CS Week 2 Jim Williams, PhD.
Happy October Exam Review 10/01/2014.
References Revisted (Ch 5)
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

February 28, 2013 COMP Introduction to Programming Objects and References Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013

Announcements The deadline of Program 3 is extended to March 10, Sunday The midterm is on next Thursday – The sample exam and its solution is online – The sample exam is difficult. The actual exam will be a bit easier – Focus on lecture notes, worksheets and assignments You will receive the grade for Lab 3 and Program 2 by next Tuesday

Review Classes Objects Instance variables Methods – Return types – Parameters and arguments Information hiding and encapsulation – public/private – accessors/mutators

Variables of a Class Type Behave differently from variables of a primitive type – Scanner keyboard = new Scanner(System.in); – Student jack = new Student(); – String unc = “UNC is great!”; At least, you have seen that you can not easily compare two strings – string1 == string2; //BAD – string1.equals(string2); //GOOD

Primitive Variables and Memory When declaring a primitive variable, a certain amount of memory is assigned/allocated based on the declared primitive type int age ; double length ; char letter ; main memory

Primitive Variables and Memory The actual data values are saved in the allocated memory for primitive types int age ; double length ; char letter ; main memory *

Variables of a Class Type What goes in these variables? String s; Student jack; main memory

Variables of a Class Type What goes in these variables? – In a class type variable, the address pointing to the actual object is saved (not the object itself) s jack UNC isG reat! JackSmi th

Variables of a Class Type Contain the memory address of the object named by the variable – NOT the object itself Object is stored in some other location in memory The address to this other location is called a reference to the object Class types are also called reference types

Reference Types Reference types vs. data types s jack UNC isG reat! JackSmi th

Example: Books Assume that we have a class named Book Book jacksBook = new Book(“Java”); Book apusBook = new Book(“Java”); vs. Book jacksBook = new Book(“Java”); Book apusBook = jacksBook;

Example: Books Book jacksBook = new Book(“Java”); Book apusBook = new Book(“Java”); jacks apus Java Java

Example: Books Book jacksBook = new Book(“Java”); Book apusBook = jacksBook; jacks apus Java

Objects in Memory jacksBook apusBook ???? Memory Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“Java”); jacksBook.setPage(137); apusBook.setPage(253); apusBook = jacksBook; apusBook.setPage(509); jacksBook now has 509 pages! ???????? ? Java ? Java ? Java ? Java ? Java 137 Java 253 Java 137 Java 253 Java ? ???? ? This location of memory is out of reach – it will be recycled by the system

Remember Variables of a class type contain memory addresses – NOT objects themselves It is dangerous to use assign operator “=” and equal-to operator “==” on class type variables

== vs. equals() for Strings Explained String is a class type What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1 == s2); strEqual is false! Why? s1 and s2 store different addresses!

== vs. equals() for Strings Explained String is a class type What happens when you have String s1 = new String(“Hello”); String s2 = s1; boolean strEqual = (s1 == s2); strEqual is true! s1 and s2 have the same address!

== vs. equals() for Strings Explained String is a class type What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1.equals(s2)); strEqual is true! Why? String’s.equals() method checks if all the characters in the two Strings are the same

Writing the.equals() method public class Book { private String name; private int page; public boolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); }

.equals() Every class has a default.equals() method if it is not explicitly written – It use “==” to check every pair of instance variables You decide what it means for two objects of a specific class type to be considered equal – Perhaps books are equal if the names and page numbers are equal – Perhaps only if the names are equal – Put this logic inside.equals() method

Parameters of a Primitive Type public void increaseNum(int num) { num++; // num is changed } public void doStuff() { int x = 5; increaseNum(x); System.out.println(x); } Prints 5. Why? num is local to increaseNum method; does not change x

Parameters of a Class Type public void changeBook(Book book) { book = new Book(“Biology”); // book is changed } public void doStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } Prints Java. Why? book is local to changeBook, does not change jacksBook

Parameters of a Class Type public void changeBook(Book book) { book.setName(“Biology”); // who is changed? } public void doStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } Prints Biology. Why? book contains the same address as jacksBook! Pay attention: the value of book is not changed!

Take-Home Message Class type variable is a pointer to the actual object Be extremely careful when you use “=”, “==” to operate class type variables, or passing them to a method