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 Black Boxes 4.1.2 Designing the Public Interface of a Class 4.1.3 Commenting the Public Interface 4.1.4 Instance Fields 4.1.5 Implementing Constructors and Methods 4.1.6 Testing a Class

3 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

4 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

5 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)

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

7 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

8 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.getBal ance());

9 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 { }

10 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() {... }

11 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

12 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

13 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

14 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);

15 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?

16 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

17 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 }

18 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 }

19

20 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

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

22 4.1.4 Instance Fields Cont. Example: public class BankAccount {... private double balance;... }

23 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

24 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

25 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

26 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);

27 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

28 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);

29 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

30 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

31 4.1.6 Testing a Class 1.Construct one or more objects of the class that is being tested 2.Invoke one or more methods 3.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

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

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

34 Reference: Big Java 2 nd 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)


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

Similar presentations


Ads by Google