CSCI 1100/1202 5.1-5.2, 6.2, 6.4 April 12, 15, 17.

Slides:



Advertisements
Similar presentations
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Advertisements

Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Chapter 6: Arrays Java Software Solutions Third Edition
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Chapter 5: Enhancing Classes
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved5-1 The if-else Statement An else clause can be added to an if statement to make an if-else statement.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 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.
Lecture 5 array declaration and instantiation array reference
Chapter VII: Arrays.
Chapter 5: Enhancing Classes
Parameter Lists & Command Line Arguments
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
More Object Oriented Programming
2D Arrays October 12, 2007 ComS 207: Programming I (in Java)
Chapter 5: Enhancing Classes
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.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Java Classes Aliases & Null & This
Object Oriented Programming Review
Outline Creating Objects The String Class The Random and Math Classes
Arrays.
Presentation transcript:

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17

Enhancing Classes We can now explore various aspects of classes and objects in more detail In Chapter 5, we will cover: –object references and aliases –passing objects as parameters –the static modifier

References Recall from Chapter 2 that an object reference holds the memory address of an object Rather than dealing with arbitrary addresses, we often depict a reference graphically as a “pointer” to an object ChessPiece bishop1 = new ChessPiece(); bishop1

Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2 5

Reference Assignment For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1bishop2 After bishop1bishop2

Aliases Two or more references that refer to the same object are called aliases of each other One object (and its data) can be accessed using different variables Aliases can be useful, but should be managed carefully Changing the object’s state (its variables) through one reference changes it for all of its aliases

Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program It is useless, and therefore called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer has the responsibility for performing garbage collection

Passing Objects to Methods Parameters in a Java method are passed by value This means that a copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) Passing parameters is essentially an assignment When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other

Passing Objects to Methods What you do to a parameter inside a method may or may not have a permanent effect (outside the method) See ParameterPassing.java (page 226)ParameterPassing.java See ParameterTester.java (page 228)ParameterTester.java See Num.java (page 230)Num.java Note the difference between changing the reference and changing the object that the reference points to

The static Modifier In Chapter 2 we discussed static methods (also called class methods) that can be invoked through the class name rather than through a particular object. For example, the methods of the Math class are static To make a method static, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than an object

Static Methods public static int triple (int num) { int result; result = num * 3; return result; } class Helper Because it is static, the method could be invoked as: value = Helper.triple (5);

Static Methods The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static; it is invoked by the system without creating an object Static methods cannot reference instance variables, because instance variables don't exist until an object exists However, they can reference static variables or local variables

Static Variables Static variables are sometimes called class variables Normally, each object has its own data space If a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created as soon as the class in which it is declared is loaded

Static Variables All objects created from the class share access to the static variable Changing the value of a static variable in one object changes it for all others Static methods and variables often work together See CountInstances.java (page 233)CountInstances.java See MyClass.java (page 234)MyClass.java

Arrays as Parameters An entire array can be passed to a method as a parameter Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other Changing an array element in the method changes the original An array element can be passed to a method as well, and will follow the parameter passing rules of that element's type

Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 25 references to String objects String[] words = new String[25]; It does NOT create the String objects themselves Each object stored in an array must be instantiated separately

Command-Line Arguments The signature of the main method indicates that it takes an array of String objects as a parameter These values come from command-line arguments that are provided when the interpreter is invoked For example, the following invocation of the interpreter passes an array of three String objects into main: > java DoIt pennsylvania texas california These strings are stored at indexes 0-2 of the parameter See NameTag.java (page 281) NameTag.java

Arrays of Objects Objects can have arrays as instance variables Therefore, fairly complex structures can be created simply with arrays and objects The software designer must carefully determine an organization of data and objects that makes sense for the situation See Tunes.java (page 282) Tunes.java See CDCollection.java (page 284) CDCollection.java See CD.java (page 286) CD.java

Two-Dimensional Arrays A one-dimensional array stores a simple list of values A two-dimensional array can be thought of as a table of values, with rows and columns A two-dimensional array element is referenced using two index values To be precise, a two-dimensional array in Java is an array of arrays See TwoDArray.java (page 299) TwoDArray.java

Multidimensional Arrays An array can have as many dimensions as needed, creating a multidimensional array Each dimension subdivides the previous one into the specified number of elements Each array dimension has its own length constant Because each dimension is an array of array references, the arrays within one dimension could be of different lengths