80 likes | 274 Views
CSH 2009-2010. A Short Review of Creating Objects in Memory using Test Classes. What is a Test Class?. Class used only for the purpose of testing out the methods of another class. Has only one method – main() Like World.myfirstmethod() in Alice. Test class for BankAccount class.
E N D
CSH 2009-2010 A Short Review of Creating Objects in Memory using Test Classes
What is a Test Class? • Class used only for the purpose of testing out the methods of another class. • Has only one method – main() • Like World.myfirstmethod() in Alice
Test class for BankAccount class public class TestBankAccount { public static void main() { //create objects and test them here } }
4 parts to creating and testing objects: Part one Declare (announce) an object reference variable BankAccount JohnsAccount;
4 steps to creating and testing objects: Part two Create an object by calling one of the constructors using the new command. The constructor puts starting values into the private fields of the new object. new BankAccount(500.0);
4 parts to creating and testing objects: Part three Store the memory address of the object created by the constructor in the object reference variable from step one. JohnsAccount = new BankAccount(500.0);
4 parts to creating and testing objects: Part four Use the object reference variable to call methods on the object JohnsAccount.getBalance();
JohnsBankAccount @197bb7 Memory public void getBalance() private double balance 500.0