© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.

Slides:



Advertisements
Similar presentations
Classes and objects Learning objectives By the end of this lecture you should be able to: explain the meaning of the term object-oriented; explain the.
Advertisements

MSc IT Programming Methodology (2). number name number.
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.
Implementing Classes Learning objectives By the end of this lecture you should be able to: design classes using the notation of the Unified Modeling Language.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
© The McGraw-Hill Companies, 2006 Chapter 8 Extending classes with inheritance.
Road Map Introduction to object oriented programming. Classes
VBA Modules, Functions, Variables, and Constants
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
© The McGraw-Hill Companies, 2006 Chapter 6 Classes and objects.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Writing Classes (Chapter 4)
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
MSc IT Programming Methodology (2). Oblong EasyScanner BankAccount.
Object-Oriented Programming in C++
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
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:
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 & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Chapter 9 Introduction to Arrays Fundamentals of Java.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Chapter Three - Implementing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object Oriented Programming in java
Defining Classes and Methods
Chapter 9 Introduction To Classes
String Objects & its Methods
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes

© The McGraw-Hill Companies, 2006 Inside the Oblong class when we design a class we must consider: –what data the class needs to hold (the attributes); –what methods are needed to access that data. the Oblong class will need to hold two items of data: –the length of the oblong; –the height of the oblong. these will have to be real numbers, so double would be the appropriate type; the methods that we shown in table 6.1.

© The McGraw-Hill Companies, 2006 The notation of the Unified Modeling Language (UML) a class is represented by a box divided into three sections.

© The McGraw-Hill Companies, 2006 Analysis of the Oblong class an Oblong object will need attributes to hold values for the length and the height of the oblong, and these will be of type double: the attributes of a class are accessible to all the methods - unlike local variables, which are accessible only to the methods in which they are declared. once the attributes are declared as private (as opposed to public), they cannot be accessed by methods of other classes; the only way to get at them is via the methods of the same class; this is how we achieve encapsulation it Java.

© The McGraw-Hill Companies, 2006 The constructor the method is declared as public so that they can be called by methods of other classes; the above constructor is a user-defined constructor; we are defining it so that when a new Oblong object is created (with the keyword new) it does more than simply space reserved in memory; in this case two assignment statements are executed: –the first assigns the value of the parameter lengthIn to the length attribute; –the second assigns the value of the parameter heightIn to the height attribute.

© The McGraw-Hill Companies, 2006 Note if you don't define your own constructor then a "default" constructor is provided; this does nothing more than reserve space in memory for the new object; it is called by using the constructor name (which is the same as the class name) with empty brackets; once we have defined our own constructors, this default constructor is no longer automatically available; if we want it to be available then we have to re-define it explicitly:

© The McGraw-Hill Companies, 2006 The getLength and getHeight methods the purpose of this method is simply to send back the value of the length attribute: getHeight, behaves in the same way in respect of the height attribute.

© The McGraw-Hill Companies, 2006 The setLength and setHeight methods this method does not return a value, so its return type is void; however, it does require a parameter of type double that it will assign to the length attribute; the body of the method consists of a single line which assigns the value of lengthIn to the length attribute. the setHeight, behaves in the same way in respect of the height attribute.

© The McGraw-Hill Companies, 2006 The calculateArea and calculatePerimeter methods there are no formal parameters, as this method does not need any data in order to do its job; it is of type double since it returns an item of this type; the actual code is just one line, namely the statement that returns the area of the oblong, calculated by multiplying the value of the length attribute by the value of the height attribute; the calculatePerimeter method is similar.

© The McGraw-Hill Companies, 2006 The BankAccount class

© The McGraw-Hill Companies, 2006 The BankAccount class The attributes accountNumber and accountName are declared as Strings; it is perfectly possible for the attributes of one class to be objects of another class.

© The McGraw-Hill Companies, 2006 The constructor: when a new object of the BankAccount class is created, the accountName and accountNumber will be assigned the values of the parameters passed to the method; the balance will be assigned the value zero; this makes sense because when someone opens a new account there is a zero balance until a deposit is made.

© The McGraw-Hill Companies, 2006 The "get-" methods getAccountNumber, getAccountName and getBalance, are all set up so that the values of the corresponding attributes can be read. The deposit and withdraw methods the action consists of adding the deposit to the balance attribute of the BankAccount object. the withdraw method behaves in a similar manner, but the amount is subtracted from the current balance.

© The McGraw-Hill Companies, 2006 The static keyword Static attributes consider the BankAccount class: –say we wanted to have an additional method which added interest, at the current rate, to the customer's balance; –it would be useful to have an attribute called interestRate to hold the value of the current rate of interest; –the interest rate is the same for any customer – if it changes, it should change for every customer in the bank (every object of the class); –we can achieve this by declaring the variable as static. an attribute declared as static is a class attribute; any changes that are made to it are made to all the objects in the class.

© The McGraw-Hill Companies, 2006 Static methods we can access the above attribute without reference to a specific object; we do this by declaring methods such as setInterestRate and getInterestRate as static; this makes a method into a class method - it does not refer to any specific object; we can call a class method by using the class name instead of the object name. the class below has three new methods as well as a new static attribute interestRate ; setInterestRate and getInterestRate are the methods that allow us to read and write to the new attribute they have been declared as static. addInteres t is the method that adds the interest to the customer's balance.

© The McGraw-Hill Companies, 2006

Analysis of Program 7.1 consider these lines: the first of these lines sets the interest rate to 10; because setInterestRate has been declared as a static method, we have been able to call it by using the class name BankAccount2 ; because interestRate has been declared as a static attribute this change is effective for any object of the class; therefore, when we add interest to this account as we do with the next line we should expect it to be calculated with an interest rate of 10, giving us a new balance of 1100.

© The McGraw-Hill Companies, 2006 Initializing attributes Java does not give an initial value to local variables but does initialize attributes; numerical attributes such as int an double are initialized to zero; boolean attributes are initialized to false and objects are initialized to null; character attributes are given an initial Unicode value of zero.

© The McGraw-Hill Companies, 2006 Passing objects as parameters you saw that when a variable is passed to a method it is simply the value of that variable that is passed; the method cannot change the value of the original variable; in the case of an array it is the value of the memory location (a reference) that is passed and consequently the value of the original array elements can be changed by the called method.

© The McGraw-Hill Companies, 2006 What about objects? in program 7.2 a BankAccount object is sent to a method that attempts to deposit money in that account; the output shows that the deposit is made successfully; this is because what was sent to the method was a reference to a BankAccount object; the object in question is located and its methods invoked in the usual way; note that it is very easy inadvertently to allow a method change an object's attributes, so you need to take care.

© The McGraw-Hill Companies, 2006 Collection classes previously we have used an array only as a variable within a method; an array can also be used as an attribute of a class; in declaring an array as an attribute, we can hide some of the inconveniences of the array type (such as remembering to start array indices at zero) by providing our own methods to control array access; a class that contains many items of the same type is said to be a collection class.

© The McGraw-Hill Companies, 2006 Aggregation and composition when one object itself consists of other objects, this relationship is called aggregation; this association, represented in UML by a diamond, is often referred to as a part-of relationship; for example, the association between a car object and the passengers in the car is aggregation; composition (represented as a filled diamond) is a special, stronger, form of aggregation whereby the "whole" is actually dependent on the "part"; for example, the association between a car and its engine is one of composition, as a car cannot exist without an engine; a collection class is an implementation of the aggregation relationship the UML notation for aggregation is a diamond.

© The McGraw-Hill Companies, 2006 The Bank class in program 6.5 three BankAccount objects were created and were held in an array; we can now create a special collection class - Bank - to hold bank accounts;

© The McGraw-Hill Companies, 2006 Analysis of the Bank class We have declared two attributes: –an array of BankAccounts; –an int to keep track of the total. The constructor: accepts an integer value representing the maximum number of accounts allowed, and creates a new array of this size; in this way the user of the class is able to decide on this number.

© The McGraw-Hill Companies, 2006 The search method

© The McGraw-Hill Companies, 2006 The add method

© The McGraw-Hill Companies, 2006 The getItem method

© The McGraw-Hill Companies, 2006 The depositMoney method

© The McGraw-Hill Companies, 2006 The remove method

© The McGraw-Hill Companies, 2006 Removing an item from a list

© The McGraw-Hill Companies, 2006 we do this by repeating this line:list[i] = list[i + 1]; with a different value of i each time. we can achieve this with a for loop. we don't have to bother about any items that come before the one we are deleting (Ling in the above example); we therefore start with the item we are going to delete; the place to stop is one before the end - Adams in this example; since we are using arrays, which start at 0, this will not be total - 1, but total - 2; thus we get the following for loop: