CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts

Slides:



Advertisements
Similar presentations
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
Road Map Introduction to object oriented programming. Classes
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Understanding class definitions Looking inside classes.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Introduction to Java Programming, 4E Y. Daniel Liang.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
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.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Class Fundamentals BCIS 3680 Enterprise Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Lecture 3: Introduction to Object and Classes
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Road Map Introduction to object oriented programming. Classes
Creating Your OwnClasses
Chapter 4: Writing Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Chapter 6 Objects and Classes
Object Oriented Programming in java
OO Programming Concepts
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Creating and Using Classes
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts Classes, Objects, and Methods --- reiterated Accessors Encapsulation Java Constructs return statement Visibility modifiers public private This Lecture Programming concepts defining your own constructor chaining objects together searching Field and method modifier static Object reference this Conjunction && Reading: Lewis & Loftus, Section 5.2 Savitch, Chapter 5 CS 100 Lecture 8

Problem Setting We wish to represent widgets: Class Widget is a collection of widgets Each widget has a unique serial number (ID#) The assignment of ID# to widgets should be automatic, unique, and immutable, i.e., not changeable. There should be no limit on the number of widgets that can be created. Method Widget idToWidget(int id) returns a reference to the widget with ID# equal to id, or null if there is no such widget. CS 100 Lecture 8

Class Definition /* Collection of widgets. */ import java.io.*; public class Widget { // Each widget w has a unique w.id > 0. // nextId is the ID# of the next widget // to be created. private static int nextId = 1; private int id; // Create a new widget with a unique ID#. public Widget() { id = nextId; nextId = nextId + 1; } CS 100 Lecture 8

Object Instantiation and Constructors Evaluation of the expression new class-name() creates a new object of the given class-name and returns a reference to that new object. A class can define its own constructor. A constructor is like a method, but … Has the same name as the class Is automatically invoked on newly instantiated objects to provide an opportunity for initialization Thus, evaluation of the expression really does three things: Create a new object of the given class-name Invoke any (parameterless) user-defined constructor of the class Return a reference to the new object. CS 100 Lecture 8

Constructors Class definition Constructor definition class class-name { constructors-declarations-and-methods } Constructor definition constructor-modifier class-name( parameter-list ) declaration-and-statement-list where constructor-modifier can be public private (use to be explained later) one other constructor-modifier later CS 100 Lecture 8

Field Declarations A field declaration can have field-modifiers field-modifiers type name ; Possible field-modifiers are: public private static others later A private field is not visible from outside the class definition. A static field is also called a class variable. A class variable is not an instance variable of each object; rather, there is precisely one instance of a class variable no matter how many objects of the class are eventually created CS 100 Lecture 8

Object Instances An object is a collection of instance variables (known as fields) and methods There is one class variable per static field declaration nextId 1 A class variable id fields methods A widget object id fields methods Another widget object id fields methods Another widget object CS 100 Lecture 8

Problem Setting, revisited We wish to represent widgets: Class Widget is a collection of widgets Each widget has a unique ID# Method idToWidget(int id) returns the widget with ID# equal to id, or null if there is no such widget. CS 100 Lecture 8

Method Definitions, revisited method-modifier return-type method-name( parameter-list ) { statement-list } Possible method-modifiers are: public private static others later A private method is not visible from outside the class definition. A static method is also called a class method. A class method is invoked on the class, not on an object of the class, e.g., class-name.method-name(expression-list) CS 100 Lecture 8

Method idToWidget But how are we going to find the widget? // Return the widget with id# id, // or null if there is no such widget. public static Widget idToWidget(int id) { . . . } But how are we going to find the widget? Idea: keep all widgets chained together. Objects can have fields that refer to other objects Needed later... The expression this evaluated in an instance method of object o, or in a constructor for object o, is a reference to o. CS 100 Lecture 8

Object Instances, revisited id previous 1 fields methods A widget object null id previous 2 fields methods Another widget object id previous 3 fields methods Another widget object last A class variable CS 100 Lecture 8

Class Definition, revisited /* Collection of widgets. */ import java.io.*; public class Widget { // Each widget w has a unique w.id > 0. // nextId is the ID# of the next widget // to be created. private static int nextId = 1; private int id; // For each widget w, w.previous is the // widget created immediately before // w was created, or null if w was the // first widget created. The most recent // widget to have been created is last. private static Widget last; private Widget previous; . . . } CS 100 Lecture 8

Constructor Widget, revisited /* Collection of widgets. */ import java.io.*; public class Widget { . . . // For each widget w, w.previous is the // widget created immediately before // r was created, or null if r was the // first widget created. The most recent // widget to have been created is last. private static Widget last; private Widget previous; // Create a new widget with a unique ID# // linked to the previous widget created. public Widget() { id = nextId; nextId = nextId + 1; previous = last; last = this; } CS 100 Lecture 8

Object Instances, revisited nextId 1 A class variable last null A class variable id previous fields methods A widget object null id previous fields methods Another widget object null id previous fields methods Another widget object null CS 100 Lecture 8

Pattern for Searching // Search for something. // Start at the first place to look. r = the first place look; while ( r is not what we are looking for and there are still more places to look ) r = the next place to look ; // Now r is either what we were looking for, // or is an indication that there were no // more places to look. CS 100 Lecture 8

Method idToWidget, revisited // Return the widget with id# id, // or null if there is no such widget. public static Widget idToWidget(int id) { Widget r = last; while (r != null && r.id != id) r = r.previous; return r; } expression && expression is called a conjunction. It is true only when both operands are true. If the left operand is false, the right operand is not evaluated. Note: The order of the conjunction is important! In particular, it would be wrong to use the conjunction r.id != id && r != null Why? CS 100 Lecture 8