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

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

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

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

©The McGraw-Hill Companies, 2006

Chapter 7

Implementing classes

Page 2: © 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.

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

©The McGraw-Hill Companies, 2006

The notation of the Unified Modeling Language (UML)

• a class is represented by a box divided into three sections.

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

©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.

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

©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.

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

©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:

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

©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.

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

©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.

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

©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.

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

©The McGraw-Hill Companies, 2006

The BankAccount class

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

©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.

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

©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.

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

©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.

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

©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.

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

©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.

• addInterest is the method that adds the interest to the customer's balance.

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

©The McGraw-Hill Companies, 2006

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

©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.

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

©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.

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

©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.

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

©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.

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

©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.

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

©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.

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

©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;

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

©The McGraw-Hill Companies, 2006

Analysis of the Bank classWe 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.

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

©The McGraw-Hill Companies, 2006

The search method

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

©The McGraw-Hill Companies, 2006

The add method

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

©The McGraw-Hill Companies, 2006

The getItem method

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

©The McGraw-Hill Companies, 2006

The depositMoney method

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

©The McGraw-Hill Companies, 2006

The remove method

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

©The McGraw-Hill Companies, 2006

Removing an item from a list

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

©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: