0

I am just trying to understand something. What is the point of a testClass (z1) object?

The way I understand it is, it is a starting point for all of the other objects. I am really asking what this means, why/how dose a testClass require an instance of its self? and is there another way to achieve the same results?

Code Below:-

public class testBank {  

creditAccount a1 = new creditAccount("Mary Chapple", 2400.41);
creditAccount a2 = new creditAccount("Jim Smith", 2.56);
creditAccount a3 = new creditAccount("Henry A Jones", 700.89);
currentAccount b1 = new currentAccount("Simon Hopkins", 86.01);
currentAccount b2 = new currentAccount("Jack C Whitheridge", 40000.29);
currentAccount b3 = new currentAccount("Bill Sutton", 100.23);
depositAccount c1 = new depositAccount("Theo Gibson", 145.99);
depositAccount c2 = new depositAccount("Jasper Williams", 3000.29);          
depositAccount c3 = new depositAccount("Julie Banks", 1000001.99);      
savingsAccount d1 = new savingsAccount("Burnard White", 2400.42);
savingsAccount d2 = new savingsAccount("Richard Bennett", 203.16);
savingsAccount d3 = new savingsAccount("Bob Robinson", 10000.11);





public testBank()
        //Create an array of objects.//
{
    bankAccount[]theAccounts = {a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3};

    showAccounts (theAccounts);
}
private void showAccounts(bankAccount[] aa)
{ 
    for (int i = 0;i <aa.length;i++)  
    {


        System.out.println("Account Holder: " +aa[i].getAccountName());
         System.out.println("Balance = £" +aa[i].getBalance());
     System.out.println("Balance pluss APR = £" +aa[i].calculateInterest());


    }
}

public static void main(String[]args)
{

    testBank z1 = new testBank();
}

Thanks for any help.

user2292173
  • 27
  • 1
  • 1
  • 5
  • 3
    All other objects start in a testClass object? I don't really understand your question, can you give us an example? – Keppil Apr 23 '13 at 18:34
  • Based on a typical question asked here, my answer would be, that you need an instance to call instance methods. Otherwise you can only call `static` methods from your `main` method. – jlordo Apr 23 '13 at 18:39
  • In my recent assignment I have set up a superClass, various subClasses(A,B,C) and a testClass, In the testClass I have made various subClass objects AClass a1 = new ACLass – user2292173 Apr 23 '13 at 18:42
  • BClass b1 = new Bclass, etc. But at the bottom I have testClass T1 = new testClass; My question is what is the point of this testClass object? – user2292173 Apr 23 '13 at 18:43
  • It's a pity we can't downvote comments! – skuntsel Apr 23 '13 at 18:47
  • @skuntsel what do you want to downvote?? OP: Can you please edit your question and post the code of the example you just mentioned? It's easier if we don't have to visualize your problem by ourselves. – Vincent van der Weele Apr 23 '13 at 18:52
  • 1
    @Heuster Downvote the inherent ambiguity. As is, the question resembles "I have A's that are partly B's, but I also have T's which are mostly V's, so why should we use W's that are roughly Z's" :) – skuntsel Apr 23 '13 at 18:58

3 Answers3

2

The point of your testClass is to test your accounts by printing on the standard output the arguments and method result of the class bankAccount :

Your classes creditAccount, currentAccount, depositAccount and savingsAccount extend the class bankAccount (those classes inherite of the class bankAccount).

If you don't want to use the testBank class, you can also create a method print in the bankAccount class which print those informations

public void print ()
{ 
    System.out.println("Account Holder: " + this.getAccountName());
    System.out.println("Balance = £" + this.getBalance());
    System.out.println("Balance pluss APR = £" + this.calculateInterest());
}

Then you will test your account using this :

public static void main(String[]args)
{
    creditAccount a1 = new creditAccount("Mary Chapple", 2400.41);
    currentAccount b1 = new currentAccount("Simon Hopkins", 86.01);
    depositAccount c1 = new depositAccount("Theo Gibson", 145.99);    
    savingsAccount d1 = new savingsAccount("Burnard White", 2400.42);
    a1.print();
    b1.print();
    c1.print();
    d1.print();
}
Nicolas HENAUX
  • 1,656
  • 1
  • 14
  • 18
  • I understand inheritance and the point of the test class, but I don't see why I need the test class object, it is not called or used in any way but my program will not run without it. ((z1)at the bottom of the code) – user2292173 Apr 23 '13 at 19:42
  • Because in this class there is the main function which is the entry point of your program. – Nicolas HENAUX Apr 23 '13 at 19:47
1

A test class does not actually require an instance of itself, what it usually requires is an instance of whatever class you are testing. Usually you would test a class from a different file and in the test file create and instance that call class being tested.

Dakuwan Locke
  • 67
  • 1
  • 5
0

The testBank() method is a constructor. This function is called when you create a new testBank instance.

You should use this method to initialize your different variables (a1,a2 etc...).

public class testBank
{  
    private creditAccount a1;
    private creditAccount a2;
    private creditAccount a3;
    private currentAccount b1;
    private currentAccount b2;
    private currentAccount b3;
    private depositAccount c1;
    private depositAccount c2;          
    private depositAccount c3;      
    private savingsAccount d1;
    private savingsAccount d2;
    private savingsAccount d3;

    public testBank()
        //Create an array of objects.//
    {
        this.a1 = new creditAccount("Mary Chapple", 2400.41);
        this.a2 = new creditAccount("Jim Smith", 2.56);
        this.a3 = new creditAccount("Henry A Jones", 700.89);
        this.b1 = new currentAccount("Simon Hopkins", 86.01);
        this.b2 = new currentAccount("Jack C Whitheridge", 40000.29);
        this.b3 = new currentAccount("Bill Sutton", 100.23);
        this.c1 = new depositAccount("Theo Gibson", 145.99);
        this.c2 = new depositAccount("Jasper Williams", 3000.29);          
        this.c3 = new depositAccount("Julie Banks", 1000001.99);      
        this.d1 = new savingsAccount("Burnard White", 2400.42);
        this.d2 = new savingsAccount("Richard Bennett", 203.16);
        this.d3 = new savingsAccount("Bob Robinson", 10000.11);
        bankAccount[]theAccounts = {a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3};

        showAccounts (theAccounts);
    }

    private void showAccounts(bankAccount[] aa)
    { 
        for (int i = 0;i <aa.length;i++)  
        {
         System.out.println("Account Holder: " +aa[i].getAccountName());
         System.out.println("Balance = £" +aa[i].getBalance());
         System.out.println("Balance pluss APR = £" +aa[i].calculateInterest());
        }
    }

    public static void main(String[]args)
    {

        testBank z1 = new testBank();
    }
}

So, what is happening there?

when you call testBank z1 = new testBank(); you are creating a new instance of your testBank class. So, the default constructor is called (the testBank() function). Inside your default constructor, all the private variables are initialized, then an array is constructed, and finally, the showAccounts method is called (this method prints the array content).

Pol0nium
  • 1,346
  • 4
  • 15
  • 31