Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.

Slides:



Advertisements
Similar presentations
Chapter 8: Designing Classes
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter Goals To learn how to choose appropriate classes to implement
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Road Map Introduction to object oriented programming. Classes
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Enhancing classes Visibility modifiers and encapsulation revisited
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Writing Classes (Chapter 4)
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Objects and Classes Mostafa Abdallah
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
A final variable is a constant
Lecture 3 John Woodward.
Chapter 3 – Implementing Classes
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Implementing Classes Yonglei Tao.
7 CHAPTER OBJECTS AND CLASSES.
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Chapter 3 Implementing Classes
Classes & Objects: Examples
د.سناء الصايغ الفصل الأول البرمجة الشيئية
More on Classes and Objects
Basics of OOP A class is the blueprint of an object.
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Dr. R Z Khan Handout-3 Classes
Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access the data of an object. System.out has a println method You do not know how it works What is important is that it does the work you request of it

Classes A class describes a set of objects with the same behavior. Some string objects: "Hello World" "Goodbye” You can invoke the same methods on all strings.

Class vs Object A class is a blue print of a particular classification of objects An object belongs to a class of objects An object is an instance of a class Memory space Class: not allocated Object: allocated Declaration Class: once Object: many times

Building Classes All Java code must be inside classes Class types Utility classes Used to store constants, utility methods, etc. Driver classes Abstract Data Types (ADTs) “blue-prints” to build objects

Defining ADT classes class attributes are normally encapsulated (i.e. private) class methods are normally public avoid using input/output inside the methods of a class static attributes and methods class constants

Public class BankAccount {   private double balance; private int accountNumber; private static int lastAssignedNumber; //automatically initialized with zero public static final double OVERDRAFT_FEE=5; // constant   /*overloaded constructors */ public BankAccount() { balance=0; lastAssignedNumber++; accountNumber= lastAssignedNumber; }   public BankAccount(double initialBalance) { balance= initialBalance; lastAssignedNumber++; accountNumber= lastAssignedNumber; }   public static int showLastAccountNumber(){ return lastAssignedNumber; }   public void deposit(double amount) { double newBalance=balance+amount;//newBalance and amount are local variables balance=newBalance; } public void withdraw(double amount) {// a mutator method double newBalance=balance-amount; balance=newBalance; } Case Study

public double getBalance() { // an accessor method return balance; } public void transfer (double amount, BankAccount other) { // a side effect balance=balance-amount; other.balance=other.balance+amount; } } /* A class to test the BankAccount class */ public class BankAccountTest { public static void main (String[] args) { BankAccount harrysChecking=new BankAccount(); int a=100; BankAccount tomsChecking=new BankAccount(a); a=2000; harrysChecking.deposit(a); harrysChecking.withdraw(500); harrysChecking.transfer(500, tomsChecking); System.out.println(harrysChecking.getBalance()); System.out.println(tomsChecking.getBalance()); System.out.println(BankAccount. showLastAccountNumber()); System.out.println(BankAccount. OVERDRAFT_FEE); } }  

Scope of variables Instance attributes/variables One for each object – dynamically allocated Local variables Parameter variables Static variables and constants Only one copy – belong to the class

Instance and static methods Instance method Belong to the object Called from the name of the object harrysChecking.getBalance() Can call instance/static methods/variables Static method Belong to the class Called from the name of the class BankAccount. showLastAccountNumber() Can only call other static methods/variables

Parameter Passing Pass by value Pass by reference Variables of primitive data types a=2000; harrysChecking.deposit(a); Pass by reference Objects/arrays Checking.transfer(500, tomsChecking);

Other things to consider… Constructors Default constructor Used only to initialize the encapsulated variables to the default values (0, false, null) Accessors and mutators “Getters” and “setters” Static methods Utility methods or Used to handle static variables

Case Study Cash Register public class CashRegister{ public CashRegister() { purchase = 0; payment = 0; }   public void recordPurchase(double amount) { double total = purchase + amount; purchase = total; }   public void enterPayment(double amount) { payment = amount; }   public double giveChange() { double change = payment - purchase; purchase = 0; payment = 0; return change; } private double purchase; private double payment; }   Case Study Cash Register

public class CashRegisterTester{ public static void main(String[] args) { CashRegister register = new CashRegister(); register.recordPurchase(29.50); register.recordPurchase(9.25); register.enterPayment(50); double change = register.giveChange(); System.out.println(change); } }