Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
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.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Intro to Object-Oriented (“OO”) Design. OO Design Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns.
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.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Vector Class. Agenda Design and Implementation of Vector class – add, get, set remove, copy, equals, ensureCapacity Hangman using Vector class.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 8 September 9, 2002.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
SEEM Java – Basic Introduction, Classes and Objects.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Classes, Interfaces and Packages
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
What About Alice? In Alice, we have seen that we have:  Objects (skater, snowman, snowwoman, camera)  Methods (move, turn, roll, move toward)  Properties.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Variables in Java A variable holds either
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Defining Classes II.
Object Oriented Programming in java
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
More About Objects and Methods
Packages & Random and Math Classes
Java Programming Language
Data Structures & Algorithms
Outline Creating Objects The String Class The Random and Math Classes
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Review for Midterm 3.
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may be labeled with names – variables. The variable refers to the data object, and when we set the variable to a value, we replace the value in the data object. When we use the variable in an expression, we retrieve the value stored in the data object.

References For primitive types (int, char, etc.) we can think of the variable has storing the value of the particular type. However, this is different for object variables. An object variable contains a reference to the data object where the values are actually stored. This is an importance difference.

An Analogy You live in a house. The house has an address. The address is not the same as the house, but can be used to refer to the house. If you say 'so-and-so' lives at 1313 Mockingbird Lane you really mean that 'so-and-so' lives at the house at 1313 Mockingbird Lane. We often confuse the address with the actual house. This is not a problem for human beings.

Analogy (cont'd) Similarly, data objects have addresses (for example, the address could be the address of the memory location in RAM where the data object is electronically stored. The address is not the same as the data object, but can be used to refer to the data object and its contents.

So What? A primitive variable can be thought to hold a value of its type. But an object variable holds the address (reference) to the data object of its type. The program will automatically fetch the data object when you use the variable, i.e., go the house and get (or replace) the person there, but the variable really holds the address.

Still, So What? Here's the point: Suppose that we create an object, say a Snowman: Snowman steve = new Snowman(); and then we set another variable to have the same value: Snowman x = steve; Then steve and x hold the same address, that is, they refer to the same data object.

This is What Changing the properties of the Snowman that the variable steve refers to also changes the properties of the Snowman that x refers to (since they are the same Snowman or data object). So, steve.setColor( " red " ); System.out.prinln(x.getColor()); would print out " red ".

Changing Variables It is important to note that changing the properties of the object is different than changing the variable. For example, Snowman dave = new Snowman(); x = dave; would make x refer to the same Snowman the dave refers to.

Summary  Object variables hold references to (addresses of) data objects.  If two object variables refer to the same data object, changing the properties of the data object affects both.  An assignment statement can be used to make an object variable point to a different data object.

Parameters This difference applies to parameters as well. If a parameter is a primitive type, its value is initialize to be the value of the corresponding argument (in the call to the method). The value is copied into the data object which that variable names.

Parameters (cont'd) If a parameter is an object type, the value of the argument is also copied to the parameter. But since the value is a reference to the data object, both the parameter and the argument refer to the same data object.

Changing Values If you change the value of a parameter of a primitive type in the method, it has no effect on the value of the argument, since the parameter has a copy of that value. But, if you change the properties of the data object that a parameter of object type refers to, it changes the properties of the data object that the argument refers to, since they are the same data object.

Example – Primitive type parameter public class Foo { public static void incr(int x) { x = x + 1; }... In main: int n = 5; Foo.incr(n); // n doesn't change.

Example Object Type Parameter public class Foo { public static void change(Snowman x) { x.changeColor( " red " ); }... In main: Snowman steve = new Snowman() Foo.change(steve); // steve changes color

Exercise Write a program, Fraud.java, that allows one to change the balance in their bank account, even though the balance field is private.

Solution To avoid allowing one to change the properties of an object (such as balance), a common trick is to return a copy of the object. Thus changing the copy does not change the original. A copy constructor is often used to make copies of the object. It is a one-argument constructor that takes an object and copies the fields to a new object.

Solution (cont'd) Remember that if fields of the object are themselves objects (example: the snowman's head), then those fields need to be copied as well.

Other pitfalls The equality operator (==) checks if two objects are the same, that is, they are the same data object (in memory). It does not check that they may be two different objects, but having the same properities, i.e., account number, balance, APR. The equals method should be used instead. This method should compare the fields for equality.

The Constant null The constant null is a special constant that may be assigned to a variable of any class type. It does not refer to any object – and that's the point. When we have a variable that doesn't refer to anything, we set it to null. It is a placeholder. For example, if a person did not have a bank account, their bankaccount field would be set to null.

Null (cont'd) This may create a problem. It means that we have to check that an object variable is not null before using the object it refers to. So, many of our methods (equals, compareTo) should check that the argument is not null before trying to process it. If you don't, you may get the dreaded Null Pointer Exception

Packages A package is how we can form libraries of classes. A package is a collection of classes that has been grouped together in a directory and given a package name. A package is made available to another program by using the import statement. For example, import java.util.Scanner; will import one class of a package.

Packages (cont'd) We can import all the classes of a package by using a wildcard: import java.util.*; The import statement must come at the top of the program.

Creating Package To create a package, we put all of the files into one directory (and where the directory is will be important) and add a package line to each file: package java.util; This line must come at the beginning of the file.

Package Names and Directories The name of the package is part of the path name to the directory containing the classes of the package. To find a package, Java needs two things – the CLASSPATH variable and the name of the package. The CLASSPATH variable is an environmental variable that gives the base location to find packages.

Path Names A path name is list of (nested) directories that is used to locate files. In Unix, we use the forward slash “/” to separate the directories, and in Windows, the backward slash, “\”. Unix: /libraries/newlibraries/utilities/numericstuff In Windows: \libraries\newlibraries\utilities\numericstuff