Download Review of chapter 5

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Review of Chapter 5
• What is an object? A class?
• What is an instance variable?
 public/private
 How can we use them? (inside/outside class)
 Can we access private instance variables?
• What is a local variable?
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review of Chapter 5
Variables:
• Can we declare variables with the same
name in different methods?
• What does keyword “this” mean/do?
 Is it required?
 Examples
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review of Chapter 5
Variables:
• What is the scope of a variable?
• What is the main difference between
variables of a primitive type and variables
of a class type?
• What is the problem with the assignment
operator (=) with class type variables?
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review of Chapter 5
Two types of methods:
• How do we call methods that do not
return anything?
 Examples?
• What is the difference to methods that
return a value?
 Examples, syntax, logic, usage
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review of Chapter 5
• What is the main method?
• What are accessor and mutator methods?
 Example.
 Why?
• What is a parameter?
 formal/actual
 primitive type/class type
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review of Chapter 5
• What is information hiding?
• What is the interface of a class?
• What is javadoc?
• What is JUnit?
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review of Chapter 5
• Course website → Examples
→ Class definitions
→ Classes, demo programs, & JUnit tests
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• Design a class to simulate a bank account
• Implement the class
• Write a demo program that creates bank
accounts
• Write JUnit tests for the bank account
class
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• What data items are associated with a
bank account?
 these are the instance variables
• What kinds of things can you do with a
bank account?
 these are the methods
 do we need any information to implement the
methods besides instance variables?
• these are parameters to the methods
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
BankAcct
- balance : double
+ getBalance() : double
+ deposit(double amount) : void //positive amount
+ withdraw(double amount) : void //positive amount
+ toString() : String
Implement the class
Write a JUnit test and a demo program
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• Demo programs
 Can be used to test basic functionality
 Don't test a class thoroughly
• JUnit tests
 Test more thoroughly
 Make sure the class always behaves as
expected
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• Make sure BankAcct.java is selected
• File -> New Junit Test Case
 Enter BankAcctTest in the box
• Rename testX to testDeposit1
 Create a BankAcct object called acct
 Deposit 500
 Make sure that getBalance returns 500
assertEquals(500, acct.getBalance(), 0.001);
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• Test method names must start with “test”
• Write a test method called testDeposit2
 Create a BankAcct object
 Deposit a negative amount
 Assert that the balance is still 0:
assertEquals(0, acct.getBalance(), 0.001);
• Write 2 similar tests for the withdraw
method
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• javadoc comment for a method:
 Description of what the method does
 @param tag (one for each parameter)
 @return tag (if the method is not void)
/**
* Deposit positive amount into this account
* @param amount the amount to deposit
*/
public void deposit(double amount)
{
// implementation
}
/**
* Get the balance of this account
* @return this account's balance
*/
public double getBalance()
{
return balance;
}
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• Add a method called transfer that transfers a
positive amount from this account to another account:
/**
* Transfer positive amount from this account to toAcct
* @param toAcct the account to transfer to
* @param amount the amount to transfer
*/
public void transfer(BankAcct toAcct, double amount)
{
}
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved
Review
• Add a method called transfer that transfers a
positive amount from this account to another account:
public void transfer(BankAcct toAcct, double amount) {
if (amount >= 0) {
withdraw(amount); // this.withdraw(amount);
toAcct.deposit(amount);
} else {
System.out.println("Error: unable to transfer “ +
“negative amount.");
}
}
• Write tests for the transfer method
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch
ISBN 0132162709 © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved