week 4 introduction to computer science and object-oriented programming comp 111 george basham

34
Week 4 Introduction to Computer Science and Object- Oriented Programming COMP 111 George Basham

Upload: neal-collins

Post on 30-Dec-2015

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 4

Introduction to Computer Science and Object-Oriented Programming

COMP 111

George Basham

Page 2: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 4 Topics

4.1.1 Black Boxes4.1.2 Designing the Public Interface of a Class4.1.3 Commenting the Public Interface4.1.4 Instance Fields4.1.5 Implementing Constructors and Methods4.1.6 Testing a Class

Page 3: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.1 Black Boxes

• A black box is any device whose inner workings are hidden

• A black box provides encapsulation, hiding unimportant details from people who don’t need them

• In order to achieve this, somebody needs to abstract out the essential concepts (abstraction) and provide a usable interface to the various users

Page 4: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.1 Black Boxes Cont.

• A car can be a black box on several levels

• A driver interfaces with the car using pedals, knobs and buttons and does not need to know about, for example, an engine control module

• A mechanic can test, install and remove an engine control module, but does not need to know about the sensors and transistors that it is comprised of

Page 5: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.1 Black Boxes Cont.

• A car parts manufacturer knows how to assemble an engine control module, but does not need to know about the inner workings of the capacitors and transistors provided to them by the electronic components manufacturer (they are black boxes to the car parts manufacturer)

Page 6: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.1 Black Boxes Cont.

• Encapsulation: Programmer using an object knows about its behavior, but not about its internal structure

• For example, we cannot view the logic/code inside the String class methods

• However, we know the purpose of and how to use length, toUpperCase, replace and other String methods. We know what parameters to pass in (sequence and type) and what the return type is.

Page 7: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.1 Black Boxes Cont.

• To abstract the essential details of a class, we must define its behavior, determine which characteristics are needed to solve our problem, then implement it

• For example, if we design a Car class as part of a video game, we might need to be able to accelerate, turn, shift etc.

• However, such behavior may be irrelevant for a vehicle registration system, so other aspects would be abstracted instead

Page 8: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.2 Designing the Public Interface of a Class

• Behavior (abstraction) we might need for a Bank Account: deposit money, withdraw money, get balance

• Methods of a BankAccount class might be: deposit, withdraw, getBalance

harrysChecking.deposit(2000);harrysChecking.withdraw(500);System.out.println(harrysChecking.getBalance());

Page 9: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.2 Designing the Public Interface of a Class Cont.

Method definitions:

1. access specifier (such as public or private)

2. return type (such as String or void)

3. method name (such as deposit)

4. list of parameters (such as double amount)

5. method body in { }

Page 10: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.2 Designing the Public Interface of a Class Cont.

accessSpecifier returnType methodName(parameterType parameterName, . . .){ method body }

public void deposit(double amount) { . . . } public void withdraw(double amount) { . . . } public double getBalance() { . . . }

Page 11: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.2 Designing the Public Interface of a Class Cont.

• Constructors are similar to methods in that they have an access specifier, parameter list and body

• However, constructors are different in that their job is not to define a behavior but rather to properly initialize an object for later use

Page 12: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.2 Designing the Public Interface of a Class Cont.

• Constructors are always named the same as the class, and constructors never have a return value

• All constructors of a class have the same name

• Compiler can tell constructors apart because they take different parameters

Page 13: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.2 Designing the Public Interface of a Class Cont.

• The constructor body is executed when a new object is created

• Statements in constructor body will set the internal data of the object

• A class is a construct that holds all the methods, constructors, and instance fields needed to model an entity

Page 14: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.2 Designing the Public Interface of a Class Cont.

accessSpecifier ClassName(parameterType parameterName, . . .){ constructor body}

public BankAccount(double initialBalance){ . . . }

Example:

BankAccount harrysChecking = new BankAccount(250.00);

Page 15: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.3 Commenting the Public Interface

• Use documentation comments to describe the classes and public methods of your programs

• If you use the standard documentation form in your class, a program called javadoc can automatically generate a set of HTML pages

• Where have we previously seen javadoc pages?

Page 16: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.3 Commenting the Public Interface Cont.

• Provide documentation comments for every class, every method, every parameter, and every return value

• Javadoc comment are contained within the symbols /** and */

• It is a good idea to write the method comments first, before writing the code in the body of the method

Page 17: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.3 Commenting the Public Interface Cont.

/**

Withdraws money from the bank account.

@param amount the amount to withdraw

*/

public void withdraw(double amount)

{

// implementation filled in later

}

Page 18: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.3 Commenting the Public Interface Cont.

/** Gets the current balance of the bank

account. @return the current balance*/public double getBalance(){ // implementation filled in later }

Page 19: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham
Page 20: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.4 Instance Fields

• An object stores its data in instance fields

• A field is a term for a storage location in memory

• An instance of a class is an object of that class

• Thus, an instance field is a storage location that is present in each object of the class

Page 21: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.4 Instance Fields Cont.

An instance field declaration usually consists of the following parts:

• An access specifier (usually private)

• The type of the instance field (such as double)

• The name of the instance field (such as balance)

Page 22: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.4 Instance Fields Cont.

Example:

public class BankAccount

{

. . .

private double balance;

. . .

}

Page 23: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.4 Instance Fields Cont.

• Each object of a class has its own set of instance fields

• Instance fields are generally declared with the access specifier private

• The private specifier means that they can be accessed only by methods of the same class, not by any other method

Page 24: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.4 Instance Fields Cont.

• For example, the BankAccount balance instance field can be accessed by the BankAccount deposit method, but not by the main method of another class

• Since the instance fields are private, all data access must occur through the public methods

• This approach is how object-oriented languages implement encapsulation

Page 25: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.5 Implementing Constructors and Methods

• We now turn our attention to supplying the bodies of the constructors and methods of a class

• Each body contains a sequence of statements

• Again, a constructor is designed to simply initialize the instance fields of an object

• The BankAccount class has two constructors

Page 26: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.5 Implementing Constructors and Methods Cont.

public BankAccount(){ balance = 0;}

public BankAccount(double initialBalance){ balance = initialBalance;}

What does this do?

BankAccount harrysChecking = new BankAccount(1000);

Page 27: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.5 Implementing Constructors and Methods Cont.

• Create a new object of type BankAccount • Call the second constructor (since a construction

parameter is supplied) • Set the parameter variable initialBalance to

1000 • Set the balance instance field of the newly

created object to initialBalance • Return an object reference, that is, the memory

location of the object, as the value of the new expression

• Store that object reference in the harrysChecking variable

Page 28: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.5 Implementing Constructors and Methods Cont.

public void deposit(double amount){ double mewBalance = balance + amount balance = newBalance;}

What does this do?

harrysChecking.deposit(500);

Page 29: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.5 Implementing Constructors and Methods Cont.

• Set the parameter variable amount to 500 • Fetch the balance field of the object whose

location is stored in harrysChecking • Add the value of amount to balance and store the

result in the variable newBalance • Store the value of newBalance in the balance

instance field, overwriting the old value

Page 30: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.5 Implementing Constructors and Methods Cont.

public double getBalance()

{

return balance

}

double d = harrysChecking.getBalance();

return statement: specifies the value that a method returns, and exits the method immediately

Page 31: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.6 Testing a Class

1. Construct one or more objects of the class that is being tested

2. Invoke one or more methods3. Print out one or more results• For this course, the above approach is a

secondary technique to provide a visual confirmation only; our primary testing approach is to use JUnit to test all class methods

Page 32: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.6 Testing a Class Cont./** A class to test the BankAccount class.*/public class BankAccountTester{ /** Tests the methods of the BankAccount class. @param args not used */ public static void main(String[] args) { BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000); harrysChecking.withdraw(500); System.out.println(harrysChecking.getBalance()); }}

Page 33: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

4.1.6 Testing a Class Cont.

JUnit approach:

/** Test the BankAccount withdraw method.*/public void testWithdraw(){ BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000); harrysChecking.withdraw(500);

assertEquals(1500.0, harrysChecking.getBalance(), .0001);

}

Page 34: Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Reference: Big Java 2nd Edition by Cay Horstmann

4.1.1 Black Boxes (section 3.1 in Big Java)4.1.2 Designing the Public Interface of a Class (section 3.2 in Big Java)4.1.3 Commenting the Public Interface (section 3.3 in Big Java)4.1.4 Instance Fields (section 3.4 in Big Java)4.1.5 Implementing Constructors and Methods (section 3.5 in Big Java)4.1.6 Testing a Class (section 3.6 in Big Java)