Chapter 3 Objects and JUnit Asserting Java Rick
Chapter 3 Objects and JUnit Asserting Java ©Rick Mercer
Find real world entities The Bank Teller Specification Implement a bank teller application to allow bank customers to access bank accounts through unique identification. A customer, with the help of the teller, may complete any of the following transactions: withdraw money, deposit money, query account balances, and see the most recent 10 transactions. The system must maintain the correct balances for all accounts. The system must be able to process one or more transactions for any number of customers.
Nouns are possible classes w Potential objects to model a solution Ø bank teller transaction customers most recent 10 transactions bank account we'll consider this one Ø window Ø Ø
A Bank. Account type w Bank. Account models a simple account at a bank w This class could have a collection of methods to allow for operations like these withdraw(50. 00) deposit(152. 27) get. Balance() // return account balance get. ID() // return account identification w The type allows all instances to have state (values) such as · · an ID such as "Kim" or "085551234" a balance such as 507. 99
Constructing objects w The Bank. Account class is not part of Java Ø It is domain specific, as in for a banking application w Construct Bank. Account objects with two values: Ø Ø A String to initialize the ID A number that represents the current balance // Construct Bank. Account objects with two arguments Bank. Account an. Account = new Bank. Account("Kim", 100. 00); Bank. Account acct 2 = new Bank. Account("Daly", 75. 00);
Class and Object Diagrams w Relationship between a class and it objects Ø Each object stores its state with “instance” variables
Preview new Types and Java Classes Methods and Data together (object-oriented) public class Bank. Account { private String ID; private double balance; public Bank. Account(String init. ID, double init. Balance) { ID = init. ID; balance = init. Balance; } public String get. ID() { return ID; } public double get. Balance() { return balance; } public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } }
Constructing objects w Whereas primitives may be initialized with one value int n = 0; double x = 0. 000001; Ø object construction requires new class-name identifier = new class-name(initial-value(s)); w Some classes require 0, 1, 2, 3, or more arguments Bank. Account my. Acct = new Bank. Account("Dakota", 123. 45); String name = new String("Kim Baker"); JButton a. Button = new JButton("A Button"); Font a. Font = new Font("Courier", Font. ITALIC, 24); Gregorian. Calendar epic = new Gregorian. Calendar(2001, 1, 1);
What is an Object? w Object: a bunch of bits in the computer memory w Every object has 1) a name used to locate the object reference variable 2) messages it understands 3) values (data) it "remembers" a. k. a state w The value are often initialized at construction as memory is allocated with new w We send messages (call functions) to objects Ø to get something done Ø to retrieve remembered values
Sending Messages w Each type, implemented as a Java class, has a collection of methods. For example Ø Bank. Account has · Ø withdraw deposit get. ID get. Balance String has · length index. Of char. At to. Lower. Case w A method is invoked via a message w A message has a sender (main e. g. ), a receiver (the object), a message-name and the arguments object name. messsage-name( arguments)
Messages continued w Example Messages // Construct three objects Bank. Account my. Acct = new Bank. Account("Kim", 123. 45); String name = new String("Dakota Baker"); JText. Field one. Line. Editor = new JText. Field("Click me"); // Several a few messages (many more possible) my. Acct. deposit(123. 45); my. Acct. withdraw(20. 00); double balance = my. Acct. get. Balance(); name = name. to. Upper. Case(); // Change name int space. Position = name. index. Of(" "); String button. Text = one. Line. Editor. get. Text(); one. Line. Editor. set. Text("Button now has this text"); button. Text = one. Line. Editor. get. Text();
Messages w Some messages return information about an object's state Ø A message that asks the object to return information: an. Account. get. Balance(); w Other messages tell an object to do something Ø A message that tells the object to do something: an. Account. withdraw(25. 00);
JUnit and the String Type Chapter 3: Objects Asserting Java ©Rick Mercer
JUnit w JUnit: A framework for designing and testing Java types using classes w Can be used standalone, but is part of all Java Development Environments: Eclipse, Dr. Java, Blue. J, Net. Beans, . . . w Virtually all programming languages have a similar testing tool: C++Unit, Py. Unit, HTTPUnit, … w JUnit also provides a convenient way to reason and demonstrate how objects work with assertions
Assertions w Assertion: A statement that is expected to be true Ø if it is not, there is something wrong w Junit provides many assertions such as assert. True, assert. False, and assert. Equals General Form: A few JUnit assertions (may more exist) assert. Equals(int expected, int actual); assert. Equals(double expected, double actual, double error); assert. True(Boolean. Expression); assert. False(Boolean. Expression);
Example Unit Test for Bank. Account import static org. junit. Assert. *; import org. junit. Test; public class Bank. Account. Test { @Test // This is a test method public void test. Deposit() { Bank. Account b 1 = new Bank. Account("Ali", 0. 00); assert. Equals(0. 0, b 1. get. Balance(), 1 e-14); b 1. deposit(123. 45); assert. Equals(123. 45, b 1. get. Balance(), 1 e-14); } } @Test // Another test method public void test. Withdraw() { Bank. Account b 2 = new Bank. Account("Britt", 500. 00); b 2. withdraw(160. 01); assert. Equals(339. 99, b 2. get. Balance(), 1 e-14); }
Object Behavior w JUnit allows a coherent way to observe the how objects work w The next slide shows several things Ø Attempt to withdraw with a negative amount is allowed · · Ø The state of the objects changes We'll fix this when we consider the guarded action pattern Attempt to withdraw more than the balance is allowed · · The state is changed We'll fix this when we consider the guarded action pattern
Demo Behavior (and test)
Assertions w Assertions help show the current state of objects and convey the results of messages w Assertions are one way to become familiar with types such as Bank. Account and String w The next slides show state of Java's String type and the return values of String messages
Java's String type w Java has a class for manipulating String objects Ø Ø The character set could be almost any in the world We're using the ASCII character set w Construct strings with a string literal String str = new String("with new"); w Or let the compiler add new for us // s 2 constructed without new String str 2 = "Don't need new"; w String methods include length char. At substring index. Of to. Upper. Case
public int length() w A length message returns number of characters @Test public void test. Length() { String str 1 = "abcdef"; String str 2 = "012"; // What are the expected values that // makes these assertions pass? assert. Equals(_____, str 1. length()); assert. Equals(_____, str 2. length()); }
public char. At(int index) w A char. At message returns the character located at the index passed as an int argument. w String objects have zero-based indexing Ø The first character is located at index 0, the 2 nd character is at index 1 char. At(str. length()) causes a runtime error @Test public void test. Char. At() { String str = "Zero Based"; assert. Equals('Z', str. char. At(0)); assert. Equals('o', str. char. At(3)); assert. Equals(' ', str. char. At(4)); assert. Equals('d', str. char. At(9)); }
public int index. Of(String sub. String) w An index. Of message returns the index where the argument begins in the String w If not found, index. Of returns -1 @Test public void test. Index. Of() { String str = "Smiles a lot, lots of smiles"; assert. Equals(9, str. index. Of("lot")); assert. Equals(1, str. index. Of("mile")); assert. Equals(22, str. index. Of("smiles")); assert. Equals(-1, str. index. Of("Not here")); }
public String to. Upper. Case() public String to. Lower. Case() w A to. Upper. Case message returns a new string like the original replacing lower case letters with upper case letters w A to. Lower. Case message returns a new string like the original replacing lower case letters with upper case letters @Test public void test. To. Upper. And. To. Lower() { String str 1 = "a. Bc. D"; assert. Equals("ABCD", str 1. to. Upper. Case()); assert. Equals("abcd", str 1. to. Lower. Case()); // Expected? Hint: the state remains the same assert. Equals("______", str 1); ______ }
Summary: Object and Classes w Objects model real world entities that are more complex than numbers or single characters w Objects are "constructed" from an existing class such as String or Bank. Account w A class is a blueprint for constructing many objects w A class is a collection of related methods and data to serve a single purpose w Each object can "remember" (store) its own set of values Ø 1, 000 Bank. Accounts can have 1, 000 unique IDs
- Slides: 25