how would you call the default constructor for a class called ...€¦ · levels of abstraction: a...

78
Bell Work – 9/19/16: How would you call the default constructor for a class called BankAccount? Agenda: Notes on Chapter 3. Create a class with constructors and methods. Objectives: To become familiar with the process of implementing classes To be able to implement simple methods To understand the purpose and use of constructors

Upload: others

Post on 18-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/19/16:How would you call the default constructor for a class called BankAccount?

Agenda:• Notes on Chapter 3.• Create a class with constructors and methods.

Objectives:• To become familiar with the process of implementing

classes • To be able to implement simple methods • To understand the purpose and use of constructors

Page 2: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Chapter 3

Implementing Classes

Page 3: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Black Boxes

A black box magically does its thing Hides its inner workings Encapsulation: the hiding of unimportant

details What is the right concept for each particular

black box? Continued…

Page 4: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Black Boxes

Concepts are discovered through abstraction Abstraction: taking away inessential features,

until only the essence of the concept remains (details of how something works doesn’t matter to the user)

In object-oriented programming the black boxes from which a program is manufactured are called objects

Page 5: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Levels of Abstraction: A Real-Life Example

Figure 1:Levels of Abstraction in Automobile Design

• Black boxes in a car: transmission, electronic control module, etc.

Page 6: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Levels of Abstraction: A Real- Life Example

Users of a car do not need to understand how black boxes work

Interaction of a black box with outside world is well-defined Drivers interact with car using pedals, buttons, etc. Mechanic can test that engine control module

sends the right firing signals to the spark plugs For engine control module manufacturers,

transistors and capacitors are black boxes produced by an electronics component manufacturer

Continued…

Page 7: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Levels of Abstraction: A Real- Life Example

Encapsulation leads to efficiency (hiding of unimportant details): Mechanic deals only with car components (e.g.

electronic control module), not with sensors and transistors

Driver worries only about interaction with car (e.g. putting gas in the tank), not about motor or electronic control module

Page 8: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Levels of Abstraction: Software Design

Figure 2:Levels of Abstraction in Software Design

Page 9: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Levels of Abstraction: Software Design

Old times: computer programs manipulated primitive types such as numbers and characters

Manipulating too many of these primitive quantities is too much for programmers and leads to errors

Solution: Encapsulate routine computations to software black boxes

Continued…

Page 10: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Levels of Abstraction: Software Design

Abstraction used to invent higher-level data types

In object-oriented programming, objects are black boxes

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

Continued…

Page 11: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Levels of Abstraction: Software Design

In software design, you can design good and bad abstractions with equal facility; understanding what makes good design is an important part of the education of a software engineer

First, define behavior of a class; then, implement it

Page 12: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check

1. In Chapters 1 and 2, you used System.outas a black box to cause output to appear on the screen. Who designed and implemented System.out?

Ans: The programmers who designed and implemented the Java library

Page 13: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Designing the Public Interface of a Class

The public constructors and methods of a class form the public interface of the class.

Behavior of bank account (abstraction): deposit money withdraw money get balance

Page 14: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Designing the Public Interface of a Class: Methods

Methods of BankAccount class:

We want to support method calls such as the following:harrysChecking.deposit(2000);harrysChecking.withdraw(500);System.out.println(harrysChecking.getBalance());

deposit withdraw getBalance

Page 15: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Designing the Public Interface of a Class: Method Definition

access specifier (such as public) return type (such as String or void) method name (such as deposit) list of parameters (double amount for deposit)

method body in { }

Continued…

Page 16: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Designing the Public Interface of a Class: Method Definition

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

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

public double getBalance() { . . . }

Examples

Page 17: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Designing the Public Interface of a Class: Constructor Definition

A constructor initializes the instance variables Constructor name is the same as the class

namepublic BankAccount(){

// body--filled in later}

Continued…

Page 18: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Designing the Public Interface of a Class: Constructor Definition

Constructor body is executed when new object is created

Statements in constructor body will set the internal data of the object that is being constructed

All constructors of a class have the same name Compiler can tell constructors apart because

they take different parameters

Page 19: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Purpose of a Constructor

Initialize the private instance fields

Page 20: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Let’s Create the Bank Account Class!

It will have 2 constructors: A default constructor that takes no parameters A constructor that takes in an initial balance

It will have 3 methods: Deposit method Withdraw method Get Balance method

It also needs instance variables and lots of comments!

Page 21: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

BankAccount Public Interface The public constructors and methods of a class

form the public interface of the class.

public class BankAccount{

// Constructorspublic BankAccount(){

// body--filled in later} public BankAccount(double initialBalance){

// body--filled in later}

Continued…

Page 22: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

BankAccount Public Interface

// Methods public void deposit(double amount)

{// body--filled in later

}

public void withdraw(double amount) {

// body--filled in later} public double getBalance() {

// body--filled in later}// private fields--filled in later

}

Page 23: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/20/16:1. How can you use the methods of the public interface of the BankAccount class to empty the harrysChecking bank account?2. Suppose you want a more powerful bank account abstraction that keeps track of an account number in addition to the balance. How would you change the public interface to accommodate this enhancement?

Agenda:• Finish the BankAccount class (well, preliminarily).• Some more notes on Chapter 3.

Objectives:• To understand how to access instance fields and local

variables • To appreciate the importance of documentation comments

Page 24: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Commenting on the Public Interface/**

Withdraws money from the bank account.@param amount the amount to withdraw

*/public void withdraw(double amount){

// implementation filled in later}

/**Gets the current balance of the bank account.@return the current balance

*/public double getBalance(){

// implementation filled in later}

Page 25: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Class Comment

/**A bank account has a balance that canbe changed by deposits and withdrawals.

*/public class BankAccount{

. . .}

• Provide documentation comments for every class every method every parameter every return value.

Page 26: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Javadoc Method Summary

Page 27: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Javadoc Method Detail

Page 28: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check5. Suppose we enhance the BankAccount class

so that each account has an account number. Supply a documentation comment for the constructor BankAccount(intaccountNumber, double initialBalance)

Ans: /**

Constructs a new bank account with a given initial balance.@param accountNumber the account number for this account@param initialBalance the initial balance for this account

*/

Page 29: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check Continued…6. Why is the following documentation comment

questionable?

Ans: The first sentence of the method description should describe the method because it is displayed in isolation in the summary table.

Better: /** Gets the account number of the bank account. … */

/**Each account has an account number.@return the account number of this account.

*/int getAccountNumber()

Page 30: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Instance Fields

An object stores its data in instance fields Field: a technical term for a storage location

inside a block of memory Instance of a class: an object of the class The class declaration specifies the instance

fields:public class BankAccount{

. . .private double balance;

}

Page 31: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Instance Fields

An instance field declaration consists of the following parts: access specifier (such as private) type of variable (such as double) name of variable (such as balance)

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

You should declare all instance fields as private

Page 32: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Instance Fields

Figure 5:Instance Fields

Page 33: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Accessing Instance Fields

The deposit method of the BankAccountclass can access the private instance field:

Continued…

public void deposit(double amount){

double newBalance = balance + amount;balance = newBalance;

}

Or

public void deposit(double amount){

balance += amount;}

Page 34: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Accessing Instance Fields

Other methods cannot:

Encapsulation = Hiding data and providing access through methods

public class BankRobber{

public static void main(String[] args){BankAccount momsSavings = new BankAccount(1000);. . .momsSavings.balance = -1000; // ERROR

}}

Page 35: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check

7. Suppose we modify the BankAccountclass so that each bank account has an account number. How does this change affect the instance fields?

Ans:An instance field needs to be added to the class. What code is needed?private int accountNumber;

Page 36: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check Continued…

8. What are the instance fields of the Rectangle class? Ans:

private int x;private int y;private int width;private int height;

x valuey valuewidthheight

What would the code be?

Page 37: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implementing Constructors

Constructors contain instructions to initialize the instance fields of an object

public BankAccount(){

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

balance = initialBalance;}

Page 38: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Constructor Call Example Create a new object of type BankAccount with $1,000

Calls the second constructor (since a construction parameter is supplied) Sets the parameter variable initialBalance to 1000 Sets the balance instance field of the newly created

object to initialBalance Returns 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

BankAccount harrysChecking = new BankAccount(1000);

Page 39: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implementing Methods

Some methods do not return a value

Some methods return an output value

public void withdraw(double amount){

double newBalance = balance - amount;balance = newBalance;

}

public double getBalance(){

return balance;}

Page 40: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Method Call Example

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

harrysChecking.deposit(500);

Page 41: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Syntax 3.5: The return Statement

return expression;or return;

Example:return balance;

Purpose:To specify the value that a method returns, and exit the method immediately. The return value becomes the value of the method call expression.

Used with a void method to simply exit the method. No actual value is returned.

Page 42: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Use of the Return 01: public class BankAccount 02: { 03: public BankAccount() 04: { 05: balance = 0; 06: } 07: public void deposit(double amount) 08: { 09: if (amount == 0) 10: return; 11: else 12: balance += amount; 13: } 14: public double getBalance() 15: { 16: return balance; 17: } 18:

Used to simply exit method

Used to return a value - balance

Page 43: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

File BankAccount.java01: /**02: A bank account has a balance that can be changed by 03: deposits and withdrawals.04: */05: public class BankAccount06: { 07: /**08: Constructs a bank account with a zero balance.09: */10: public BankAccount()11: { 12: balance = 0;13: }14:15: /**16: Constructs a bank account with a given balance.17: @param initialBalance the initial balance 18: */

Continued…

Page 44: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

File BankAccount.java19: public BankAccount(double initialBalance)20: { 21: balance = initialBalance;22: }23: 24: /**25: Deposits money into the bank account.26: @param amount the amount to deposit27: */28: public void deposit(double amount)29: { 30: double newBalance = balance + amount;31: balance = newBalance;32: }33:34: /**35: Withdraws money from the bank account.36: @param amount the amount to withdraw

Continued…

Page 45: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

File BankAccount.java37: */38: public void withdraw(double amount)39: { 40: double newBalance = balance - amount;41: balance = newBalance;42: }43:44: /**45: Gets the current balance of the bank account.46: @return the current balance47: */48: public double getBalance()49: { 50: return balance;51: }52:53: private double balance;54: }

Page 46: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/21/16:1. The rectangle class has four instance fields: x, y,

width, and height. Give a possible implementation of the getWidth method.

2. Give a possible implementation of the translatemethod of the Rectangle class.

Agenda:• Continue working on Chapter 3 programming assignments.

Objectives:• To understand how to access instance fields and local

variables • To appreciate the importance of documentation comments

Page 47: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/22/16:When you run the BankAccountTester program, how many objects of class BankAccount are constructed? How many objects of type BankAccountTester?

Agenda:• Finish Chapter 3 notes.• Continue working on Chapter 3 programming assignments.

Objectives:• To understand how to access instance fields and local

variables • To appreciate the importance of documentation comments

Page 48: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

• Unit Testing is testing in isolation. This testing verifies that a class works correctly, outside a complete program.

• To test a class, use:1. an environment for interactive testing (like BlueJ), or2. write a tester class.

1. A test class is a class with a main method that contains statements to test another class.

2. It Typically carries out the following steps: • Construct one or more objects of the class that is being tested • Invoke one or more methods (we will test ALL methods)• Print out one or more results (we will print out the results from

each method we test)• Print the expected values

Continued

Unit Test

Page 49: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

01: /**02: A class to test the BankAccount class.03: */04: public class BankAccountTester05: {06: /**07: Tests the methods of the BankAccount class.08: @param args not used09: */10: public static void main(String[] args)11: {12: BankAccount harrysChecking = new BankAccount();13: harrysChecking.deposit(2000);14: harrysChecking.withdraw(500);15: System.out.println(“Harry’s Balance: “ +

harrysChecking.getBalance());16: System.out.println("Expected: 1500.00"); 17: }18: }

Output:Harry’s Balance: 1500.00 Expected: 1500.00

BankAccountTester.java

Page 50: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Categories of Variables

Categories of variables Instance fields (balance in BankAccount) Local variables (newBalance in deposit

method) Parameter variables (amount in deposit

method) An instance field belongs to an object The fields stay alive until no method uses the

object any longer

Page 51: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Categories of Variables

In Java, the garbage collector periodically reclaims objects when they are no longer used

Local and parameter variables belong to a method

Instance fields are initialized to a default value, but you must initialize local variables

Page 52: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Lifetime of Variables

In main method:harrysChecking.deposit(500);

In deposit method:double newBalance = balance + amount;balance = newBalance;

Continued…

Page 53: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Lifetime of Variables

Figure 7: Lifetime of Variables Continued…

Page 54: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Lifetime of Variables

Figure 7: Lifetime of Variables

Page 55: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check

13. What do local variables and parameter variables have in common? In which essential aspect do they differ?

Ans:Variables of both categories belong to methods–they come alive when the method is called, and they die when the method exits. They differ in their initialization. Parameter variables are initialized with the call values; local variables must be explicitly initialized.

Page 56: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

01: /**02: A class to test the BankAccount class.03: */04: public class BankAccountTester05: {06: /**07: Tests the methods of the BankAccount class.08: @param args not used09: */10: public static void main(String[] args)11: {12: BankAccount harrysChecking = new BankAccount();13: harrysChecking.deposit(2000);14: harrysChecking.withdraw(500);15: System.out.println((“Harry’s Balance: “ +

harrysChecking.getBalance());16: System.out.println("Expected: 1500"); 17: }18: }

Output:Harry’s Balance: 1500 Expected: 1500

BankAccountTester.java Refer to this slide in next Self-Check question. (and the BankAccount class on the board)

Page 57: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check Continued…14. During execution of the BankAccountTester

program in the preceding section, how many instance fields, local variables, and parameter variables were created, and what were their names?

Ans: One instance field, named balance. Three local variables, one named harrysCheckingand two named newBalance (in the depositand withdraw methods); two parameter variables, both named amount (in the depositand withdraw methods).

Page 58: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implicit and Explicit Method Parameters

The implicit parameter of a method is the object on which the method is invoked

harrysChecking.deposit(500);

Page 59: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implicit and Explicit Method Parameters

Use of an instance field name in a method denotes the instance field of the implicit parameter

public void withdraw(double amount){

double newBalance = balance - amount;balance = newBalance;

}

Page 60: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implicit and Explicit Method Parameters

balance is the balance of the object to the left of the dot:

means double newBalance = momsSavings.balance - amount;momsSavings.balance = newBalance;

momsSavings.withdraw(500)

Page 61: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implicit Parameters and this Every method has one implicit parameter The implicit parameter is always called this Exception: Static methods do not have an

implicit parameter (more in Chapter 8)

double newBalance = balance + amount;// actually meansdouble newBalance = this.balance + amount;

Page 62: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implicit Parameters and this

When you refer to an instance field in a method, the compiler automatically applies it to the this parameter momsSavings.deposit(500);

Page 63: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Implicit Parameters and this

Figure 8:The Implicit Parameter of a Method Call

Page 64: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check

15. How many implicit and explicit parameters does the withdraw method of the BankAccount class have, and what are their names and types?

Ans:One implicit parameter, called this, of type BankAccount, and one explicit parameter, called amount, of type double.

Page 65: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check Continued…

16. In the deposit method, what is the meaning of this.amount? Or, if the expression has no meaning, why not?

Ans:It is not a legal expression. this is of type BankAccount and the BankAccountclass has no field named amount.

Page 66: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

01: /**02: A class to test the BankAccount class.03: */04: public class BankAccountTester05: {06: /**07: Tests the methods of the BankAccount class.08: @param args not used09: */10: public static void main(String[] args)11: {12: BankAccount harrysChecking = new BankAccount();13: harrysChecking.deposit(2000);14: harrysChecking.withdraw(500);15: System.out.println(harrysChecking.getBalance());16: System.out.println("Expected: 1500"); 17: }18: }

Output:1500 Expected: 1500

BankAccountTester.java Refer to this slide in next Self-Check question. (and the BankAccount class on the board)

Page 67: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Self Check Continued…

17. How many implicit and explicit parameters does the main method of the BankAccountTester class have, and what are they called?

Ans: No implicit parameter – the method is static – and one explicit parameter, called args.

Page 68: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Assignment

Written Exercises Due as per your syllabus R3.1-3.12

Programming Exercises Due as per your syllabus P3.1 – 3.4, 3.6, and 3.7(Remember – do 3.1 and 3.2 as one program)

Programming Project 3.1 Due as per your syllabus

Page 69: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/22/16:Write the 2 constructors for a class called GasPump that has variables gallons and cost per gallon. The first constructor should be the default constructor, and the second should receive two explicit parameters, gallons and cost per gallon.

Agenda:• Continue working on Chapter 3 programming assignments.• On Monday, we will review exercises R3.1-R3.12, which

you should have already done.

Objectives:• To understand how to access instance fields and local

variables • To appreciate the importance of documentation comments

Page 70: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Things to Consider

• For your programs in Chapter 3– You will need to construct a class file as well as a main tester file.  Both should be created under the same project.

– Don’t crowd your code – use proper indentation and add comments to explain what is happening.

– For prog 3.1 and 3.2 – do in the same prog, but show all outputs.

– You should not be using static methods except for the main method.

Page 71: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Things to Consider• More items 

– Create both the class and main file codes.  Your output should contain expected values as well.  Upload .java files to student Completed Works folder.

– When showing output – do not change anything to make it look better.  If it is not correct, you need to adjust your code.

– You should be able to code everything using the skills learned in chapters 1‐3.  If you use anything outside this, make sure you can explain it to me.

Page 72: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Things to Consider

• More yet –– Project 3‐1

• Remember – any tester class you create must test all constructors and methods.

• Create two objects for the first part, create output testing each of the methods and constructors.

• For the second part, add to the existing code – create a new object that will show at least three months worth of transactions.

• Your class should be able to accept any number of free transactions before applying the monthly charge.

• Separate your outputs to make it easier to read by including blank lines or +++++++ or ===========.

Page 73: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/26/16:Find all of the errors in the following code (8 errors):public class SodaMachine{ private int numberOfSodas;

private int costPerSoda;private string typeOfSoda;

public SodaMachine (int nos, int cps, String tos);nos = numberOfSodas;costPerSode = cps;typeOfSoda = tpstotalCost = costPerSoda * numberOfSodas;

}}

Agenda:• Continue working on Chapter 3 programming assignments.

Objectives:• Understand how to implement classes.

Page 74: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/27/16:Find all of the errors in the following code (4 errors, 1 huge):public class fixMe{ private int numberOfItems;

private String fixType;

public void getType(){

return fixType;}public void calculate(int items);{

costPerItem = items / numberOfItems;}

}

Agenda: Field Trip – 10/20!!!• Continue working on Chapter 3 programming assignments.

Objectives:• Understand how to implement classes.

Page 75: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/28/16:No Bell Work

• Chapter 3 assignments due Tuesday.• You will have today and tomorrow in class to finish them;

after that you’re on your own time.• Review Friday• Test Tuesday and Wednesday next week.

Agenda: Field Trip – 10/20!!!• Continue working on Chapter 3 programming assignments.

Objectives:• Understand how to implement classes.

Page 76: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/29/16:No Bell Work

• Chapter 3 assignments due Tuesday.• You will have today in class to finish them; after that

you’re on your own time.• Review Friday• Test Tuesday and Wednesday next week.

Agenda: Field Trip – 10/20!!!• Continue working on Chapter 3 programming assignments.

Objectives:• Understand how to implement classes.

Page 77: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Bell Work – 9/30/16:1) What 5 things define a method?

2) What is the public interface of a class?

3) What are the 3 types of variables and where do they belong?

4) What is garbage collection?

Agenda: Field Trip – 10/20!!!• Chapter 3 assignments due Tuesday.• Review today• Test Tuesday and Wednesday next week.

Objectives:• Understand how to implement classes.

Page 78: How would you call the default constructor for a class called ...€¦ · Levels of Abstraction: A Real-Life Example Figure 1: Levels of Abstraction in Automobile Design • Black

Topics for Chapter 3 Test• Levels of Abstraction – Black Boxes• Specifying the Public Interface of a Class• Commenting the Public Interface• Instance Fields• Implementing Constructors and Methods• Unit Testing• Categories of Variables• Implicit and Explicit Method Parameters