Previous Lecture: Today’s Lecture: Reading (JV):

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Static members Based on Java tutorial by Oracle: svars.html
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
SEEM Java – Basic Introduction, Classes and Objects.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
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];
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.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
© 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.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
GC211 Data structure Lecture 3 Sara Alhajjam.
Lecture 3: Introduction to Object and Classes
Creating and Using Objects, Exceptions, Strings
Lecture 3 John Woodward.
Static data members Constructors and Destructors
Examples of Classes & Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Writing Classes Chapter 4.
Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. Chapter 4 focuses on: class definitions.
Intro To Classes Review
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
More Object Oriented Programming
Object Oriented Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Classes & Objects: Examples
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Sit next to someone. Today, we do some work in pairs.
Anatomy of a Method.
Writing Classes.
Chapter 4 Writing Classes.
More on Classes and Objects
JAVA Constructors.
Sit next to someone. Today, we do some work in pairs.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Object Oriented Programming Review
Packages & Random and Math Classes
JAVA CLASSES.
Introduction to Object-Oriented Programming
Chapter 9 Objects and Classes Part 01
OO Programming Concepts
Chapter 9 Introduction To Classes
Corresponds with Chapter 5
Chapter 7 Objects and Classes
Presentation transcript:

Previous Lecture: Today’s Lecture: Reading (JV): CS100M Lecture 17 Previous Lecture: Iteration—the for loop, nesting String objects Today’s Lecture: Object oriented programming Objects and classes Methods Modifiers Reading (JV): Sec 4.1-4.6 the this reference on p. 230-231 Sec 5.2 November 1 Lecture 17 November 1, 2001

Object: contains variables (fields, instance variables) and methods Variables: “state” or “characteristics” e.g., name, age Methods: “behavior” or “action” e.g., yell, bounce Class: blueprint (definition) of an object No memory space is reserved for object data Imagine a class Cookie. To make a whole lot of cookies, you may want to Make a cookie cutter—define the class Need to actually stamp out the cookie—instantiate an object November 1 Lecture 17

Variables Class Definition TWO main types of variables: Primitive type Reference to object Some variables with different properties: Local: live and die inside a method Instance variable: owned by and accessed through individual instances (objects) Static variable: class variable shared by all instances—only one copy in a class public class class-name { declaration (and initialization) constructor methods } Class Definition November 1 Lecture 17

Class definitions: declarations public class Account { private int balance; // current bal. private int deposits; // deposits to // date private int withdrawals; //withdrawal //to date } Declarations of a class define fields (instance variables) of the class Each class is a type. Classes are not primitive types. November 1 Lecture 17

Variables and values revisited Variable: named place to hold a value name value Values of primitive types are held directly in variables count 0 an int variable Values of non-primitive types are references to objects shown graphically by arrows account1 an Account variable balance deposits withdrawals account2 balance deposits withdrawals another Account variable November 1 Lecture 17

Declarations Revisited Syntax: type name; Examples: int count; Account account1; Account account2; Instance variables have default initial values int variables: 0 Non-primitive (reference) variables: null Value null signifies that no object is referenced November 1 Lecture 17

Declaration with initialization Syntax: type name = expression; Examples: int count = 0; Account account1 = new Account(); Account account2 = new Account(); Object instantiation: An expression of the form new class-name() computes a reference to a newly created object of the given class November 1 Lecture 17

Manipulating field of an object Let f be a field defined for class c Let r refer to an object o of class c Then r.f is a variable of object o The dot (.) means “follow the arrow” Example: // deposit d into account1 account1.balance = account1.balance + d; account1.deposits=account1.deposits + d; // shortcut account1.balance += d; account1.deposits += d; November 1 Lecture 17

References are values Suppose you declare a to be an Account reference variable: Account a; Then you can assign an reference to an Account object into variable a Example: // if k is 1, deposit d into account1 // otherwise deposit d into account2 if (k==1) a = account1; else a = account2; // deposit d to Account a a.balance = a.balance + d; a.deposits = a.deposits + d; November 1 Lecture 17

References are values, cont’d k 2 an int variable a null an Account variable account1 an Account variable Account2 an Account variable balance deposits withdrawals balance deposits withdrawals November 1 Lecture 17

Methods A method is a named, parameterized group of statements Syntax return-type method-name ( parameter-list ) { statement-list } return-type void means nothing is returned from the method November 1 Lecture 17

Example class definition public class Account { private int balance; // current bal. private int deposits; // deposits to // date private int withdrawals; //withdrawal //to date // deposit d to account public void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account public void withdraw(int w) { balance -= w; withdrawals += w; November 1 Lecture 17

Method use Let m be a method in objects of class c Let r refer to object o of class c Then expression r.m( expression-list ) or statement r.m( expression-list ); Invokes method m in object o When method m is executed, field names signify the instance variables in object o Example: account1.deposit(200); account1 an Account variable balance deposits withdrawals 200 November 1 Lecture 17

Example use public class Account { private int balance; // current balance private int deposits; // deposits to date private int withdrawals; // withdrawals // deposit d to account public void deposit(int d) { balance+=d; deposits+=d; } // withdraw w from account public void withdraw(int w) { balance-=w; withdrawals+=w; // test harness for class public static void main(String[] args) { Account a = new Account(); //create obj. int amt = Keyboard.readInt(); a.deposit(amt); System.out.println(“balance”+a.balance); November 1 Lecture 17

Class variables and methods Reserved word: static Only one copy of a class variable exists (contrast with instance variables, where there are as many copies as there are instances) In the Account class, may want to keep track of the number of account objects that have been created private static int numAccts; November 1 Lecture 17

Example use public class Account { private static int numAccts; // # of accts private int balance; // current balance private int deposits; // deposits to date private int withdrawals; // withdrawals // need some method to update numAccts // as Account objects are instantiated. // Use a CONSTRUCTOR! // deposit d to account public void deposit(int d) { balance+=d; deposits+=d; } // withdraw w from account public void withdraw(int w) { balance-=w; withdrawals+=w;} // test harness for class public static void main(String[] args) { // some statements ... } November 1 Lecture 17