Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."— Presentation transcript:

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

2 Week 4 Topics 4.1.1 Instance Variables 4.1.2 Encapsulation 4.1.3 Specifying the Public Interface of a Class 4.1.4 Commenting the Public Interface 4.1.5 Providing the Class Implementation 4.1.6 Unit Testing

3 4.1.1 Instance Variables An object stores its data in instance variables A variable is a term for a storage location in memory An instance of a class is an object of that class Thus, an instance variable is a storage location that is present in each object of the class (also denoted as an instance field)

4 4.1.1 Instance Variables Cont. An instance variable declaration usually consists of the following parts: An access specifier (usually private ) The type of the instance variable (such as double ) The name of the instance variable (such as balance )

5 4.1.1 Instance Variables Cont. Example: public class BankAccount {... private double balance;... }

6 4.1.1 Instance Variables Cont. Each object of a class has its own set of instance variables Instance variables 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

7 4.1.1 Instance Fields Cont. For example, the BankAccount balance instance variable can be accessed by the BankAccount deposit method, but not by the main method of another class Since the instance variables are private, all data access must occur through the public methods This approach is one way object-oriented languages implement encapsulation

8 4.1.2 Encapsulation 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

9 4.1.2 Encapsulation 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

10 4.1.2 Encapsulation 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)

11 4.1.2 Encapsulation 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.

12 4.1.2 Encapsulation 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

13 4.1.3 Specifying 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.getBal ance());

14 4.1.3 Specifying 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 { }

15 4.1.3 Specifying 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() {... }

16 4.1.3 Specifying 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

17 4.1.3 Specifying 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

18 4.1.3 Specifying 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

19 4.1.3 Specifying the Public Interface of a Class Cont. accessSpecifier ClassName(parameterType parameterName,...) { constructor body } public BankAccount(double initialBalance) {... } Example: BankAccount harrysChecking = new BankAccount(250.00);

20 4.1.4 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?

21 4.1.4 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

22 4.1.4 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 }

23 4.1.4 Commenting the Public Interface Cont. /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { // implementation filled in later }

24

25 4.1.5 Providing the Class Implementation 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

26 4.1.5 Providing the Class ImplementationCont. public BankAccount() { this.balance = 0; } public BankAccount(double initialBalance) { this.balance = initialBalance; } What does this do? BankAccount harrysChecking = new BankAccount(1000);

27 4.1.5 Providing the Class ImplementationCont. 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

28 4.1.5 Providing the Class ImplementationCont. public void deposit(double amount) { double newBalance = this.balance + amount this.balance = newBalance; } What does this do? harrysChecking.deposit(500);

29 4.1.5 Providing the Class ImplementationCont. 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

30 4.1.5 Providing the Class ImplementationCont. public double getBalance() { return this.balance } double d = harrysChecking.getBalance(); return statement: specifies the value that a method returns, and exits the method immediately

31 4.1.6 Unit Testing 1.Construct one or more objects of the class that is being tested 2.Invoke one or more methods 3.Invoke one or more assert methods to compare expected to actual Test Driven Development is our primary approach and JUnit is the framework we use. Compare using JUnit to a traditional appoach (next slide).

32 4.1.6 Unit Testing 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()); }

33 4.1.6 Unit Testing 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); }

34 Reference: Big Java 4th Edition by Cay Horstmann 4.1.1 Instance Variables (section 3.1 in Big Java) 4.1.2 Encapsulation (section 3.2 in Big Java) 4.1.3 Specifying the Public Interface of a Class (section 3.3 in Big Java) 4.1.4 Commenting the Public Interface (section 3.4 in Big Java) 4.1.5 Providing the Class Implementation (section 3.5 in Big Java) 4.1.6 Unit Testing (section 3.6 in Big Java)


Download ppt "Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham."

Similar presentations


Ads by Google