CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
Road Map Introduction to object oriented programming. Classes
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
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.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
Understanding class definitions Looking inside classes 3.0.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Understanding class definitions Looking inside classes.
1 Lecture 4 Objects and Classes Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
1 Warm-Up Problem Just like with primitive data types (int, double, etc.), we can create arrays of objects. ex: bankAccount employees[100]; Problem: It’s.
Understanding class definitions
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
1 10/15/04CS150 Introduction to Computer Science 1 Reading from and Writing to Files Part 2.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
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.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Lecture 3 John Woodward.
Chapter Three - Implementing Classes
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
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Object Oriented Programming
More on Classes and Objects
CS100J Lecture 11 Previous Lecture This Lecture
CS2011 Introduction to Programming I Objects and Classes
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
JAVA CLASSES.
Introduction to Object-Oriented Programming
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Previous Lecture: Today’s Lecture: Reading (JV):
Chapter 5 Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases –Reading: n Lewis & Loftus, Chapter 4 and Section 5.1 n Savitch, Chapter 4 n This Lecture –Programming Concepts n Classes, Objects, and Methods --- reiterated n Accessors n Encapsulation –Java Constructs n return statement n Visibility modifiers –public –private

CS 100Lecture72 Class Definition A class is a named group of declarations and methods A class is a named group of declarations and methods n Class definition class class-name { declarations methods} A method is a named parameterized group of declarations and statements A method is a named parameterized group of declarations and statements n Method definition return-type method-name( parameter-list ) { declaration-and-statement-list declaration-and-statement-list }

CS 100Lecture73 Class Definition Example class Account { int balance; // current balance int balance; // current balance int deposits; // deposits to date int deposits; // deposits to date int withdrawals; // withdrawals to date int withdrawals; // withdrawals to date // deposit d to account // deposit d to account void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; }}

CS 100Lecture74 Object Instantiation The value of expression The value of expression new class-name() is a reference to a new object of the given class-name

CS 100Lecture75 Object Instance Example n An object is a collection of instance variables (also known as fields) and methods n An object can be referred to as a unit balance deposits withdrawals deposit withdraw fields methods

CS 100Lecture76 Access to Fields and Methods n If –r refers to object o of class c, –f is a field of o –m is a method of o n then –r.f is a variable of object o –r.m is a method of o

CS 100Lecture77 Application Setting n Process bank account transactions: –The input consists of zero or more non-zero integers signifying “transactions”, followed by 0. –A positive value is a deposit. –A negative value is a withdrawal. n Assume the account has an initial balance of 0, process all transactions, and output: –Final balance –Total of all deposits –Total of all withdrawals n Sample input data n Sample output Balance: 250 Deposits: 300 Withdrawals: 50

CS 100Lecture78 Application Example int amount; Account account = new Account(); /* Read and process all transactions. */ amount = in.readInt(); amount = in.readInt(); while ( amount != 0 ) while ( amount != 0 ) { if ( amount >= 0 ) if ( amount >= 0 ) account.deposit(amount); account.deposit(amount); else else account.withdraw(-amount); account.withdraw(-amount); amount = in.readInt(); amount = in.readInt(); } /* Output summary information. */ System.out.println( System.out.println( ”Balance: ” + account.balance ”Balance: ” + account.balance ); ); System.out.println( System.out.println( ”Deposits: ” + account.deposits ”Deposits: ” + account.deposits ); ); System.out.println( System.out.println( ”Withdrawals: ” + account.withdrawals ”Withdrawals: ” + account.withdrawals ); );

CS 100Lecture79 Methods that Return Values n Method invocation object-expression. method-name(expression-list) n Method execution –initialize the parameters with the values of the argument expressions –execute the statement-list of the method –thereafter, continue where you were before Non- void methods with return-type t can return a value of type t using the statement Non- void methods with return-type t can return a value of type t using the statement return expression ;

CS 100Lecture710 Class Definition Example, Version 2 class Account { int balance; // current balance int deposits; // deposits to date int withdrawals; // withdrawals to date // deposit d to account void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; } // accessors int getBalance(){ return balance; } int getDeposits(){ return deposits; } int getWithdrawals(){ return withdrawals; } }

CS 100Lecture711 Application Example, Version 2 int amount; Account account = new Account(); /* Read and process all transactions. */ amount = in.readInt(); amount = in.readInt(); while ( amount != 0 ) while ( amount != 0 ) { if ( amount >= 0 ) if ( amount >= 0 ) account.deposit(amount); account.deposit(amount); else else account.withdraw(-amount); account.withdraw(-amount); amount = in.readInt(); amount = in.readInt(); } /* Output summary information. */ System.out.println( System.out.println( ”Balance: ” + account.getBalance() ”Balance: ” + account.getBalance() ); ); System.out.println( System.out.println( ”Deposits: ” + account.getDeposits() ”Deposits: ” + account.getDeposits() ); ); System.out.println( System.out.println( ”Withdrawals: ” + account.getWithdrawals() ”Withdrawals: ” + account.getWithdrawals() ); );

CS 100Lecture712 Field Modifiers A field declarations and method definitions can have a modifier A field declarations and method definitions can have a modifier n Declarations modifier type name ; modifier type name ; n Method definitions modifier modifier return-type method-name( parameter-list ) return-type method-name( parameter-list ) { statement-list statement-list } Possible modifiers are: Possible modifiers are:publicprivate others later A private field or method is not visible from outside the class definition. A private field or method is not visible from outside the class definition.

CS 100Lecture713 Class Definition Example, Version 3 class Account { private int balance; // current balance private int balance; // current balance private int deposits; // deposits to date private int deposits; // deposits to date private int withdrawals; // withdrawals to date private int withdrawals; // withdrawals to date // deposit d to account void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; } // accessors int getBalance(){ return balance; } int getDeposits(){ return deposits; } int getWithdrawals(){ return withdrawals; } }

CS 100Lecture714 Class Definition Example, Version 4 class Account { private int deposits; // deposits to date private int deposits; // deposits to date private int withdrawals; // withdrawals to date private int withdrawals; // withdrawals to date // deposit d to account // deposit d to account void deposit(int d) { deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) { withdrawals = withdrawals + w; } // accessors int getBalance() { return deposits - withdrawals; } int getDeposits(){ return deposits; } int getWithdrawals(){ return withdrawals; } }

CS 100Lecture715 Encapsulation and Information Hiding n The users of a class and its objects are called its clients. The author of a class is called its implementor. The author of a class is called its implementor. Information hiding principle. Implementors hide implementation details inside a class definition so clients can’t see them. In particular, they make all fields private. Information hiding principle. Implementors hide implementation details inside a class definition so clients can’t see them. In particular, they make all fields private. n The methods of a class provide services to clients. The interface of a class is the contract in which the implementor commits to deliver certain services to the clients. n Implementors are free to change the implementation of a class provided the interface doesn’t change, i.e. provided clients can’t see any change in services.