-2

I am writing a java project on a bank. The main class calls another class called Account. I have 10 accounts so I have to make 10 objects.

Is it possible to make an array of such objects? If so How?.

Here is the code for the Account class code:

import java.util.*;
import java.io.*;
class Account
{
private String Name;
private int age;
private long accnum;
private int pin;
private int phone;
private boolean gender;
private double balance;
private double interest;
private int lastpin;
public Account(String nm,int ag,long acc,int phon,boolean gen,double bal,int last)
{
    Name = nm;
    age = ag;
    accnum = acc;
    phone = phon;
    gender = gen;
    balance = bal;
    lastpin = last;
    pin = Random(lastpin);
}
public int Random(int num)
{
    int count = 0;
    num = num * 1103;
    int num1=num;
    int divide = 1;
    test:
    while(true){
    while(num != 0)
    {
        num =(int)(num/10);
        //num /= 10;
        ++count;
    }
    if(count > 4)
    divide =(int)(Math.pow(10,(count - (count - 3))));

    else if(count < 4)
    {
        num1 = num1 * 11;
        continue test;
    }
    break test;
  }
  lastpin = (num1/divide);
  return (num1/divide);      
}
public void Deposit(double amount)
{
    balance = balance + amount;
}
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*public String[] Initnm(String x[]){
   for(int i = 0,j = 0;i < x.length || j < x.length;i++,j++){
       char ch = (char)j;
       x[i] = Character.toString(ch);;
    }
   return x;
}*/
//sha
private long[] Initacc(long x[]){
   for(long i = 0;i < x.length ; i++){
       if(((int) Math.log10(i) + 1) == 1)
       x[(int)i] = Long.parseLong("10105175407".concat("00".concat(Long.toString(i))));
       else if(((int) Math.log10(i) + 1) == 2)
       x[(int)i] = Long.parseLong("10105175407".concat("0".concat(Long.toString(i))));
       else if(((int) Math.log10(i) + 1) == 3)
       x[(int)i] = Long.parseLong("10105175407".concat(Long.toString(i)));
    }
    return x;
 }   
private int[][] InitAgenda(int[][]x)
{
    for(int i = 0;i < x.length;i++)
    {
        x[0][i] = 100;
        x[1][i] = 0;
    }
    return x;
}
private int[] Initpin(int x[]){
    for(int i = 0,j = 1000;i < x.length;i++,j++){
        x[i] = j;
    }
    return x;
}
private double[] Initbal(double x[]){
    for(int i = 0;i < x.length;i++){
        x[i] = 200.00d;
    }
    return x;
}
private double[] Initirst(double x[],char ch){
    for(int i = 0;i < x.length;i++){
        if(ch == 's')
        x[i] = 3.10d ;
        else if(ch == 'c')
        x[i] = 0.0d;
    }
    return x;
}
 }

This is the Account class which handles all the accounts. The main class is too long to be shown. Thank you all.

1 Answers1

0

Yes you can make an array of objects like this: First make the class you want to make objects of

class MyClass {

}

Making an array of objects of type "MyClass", of size n

int n = 10;
MyClass[] myClassArray = new MyClass[n];

In the case of your class the line would be:

Account[] accounts = new Account[10];
Oscar Chambers
  • 690
  • 7
  • 19
  • Could you explain this a little further. – Shashidhar Y Bhat Oct 01 '17 at 09:55
  • What would you like me to elaborate on? :) – Oscar Chambers Oct 01 '17 at 10:35
  • Ok sure. Well beginning with the first part `Account[]` means you're dealing with type `Account`, the `[]` bit is how the compiler knows that it will be an array. The word `accounts` is the name of your array, this can be anything you like, but it cannot start with a number. `= new` tells the program to allocate some memory for the for the array and `Account[10]` says it's going to be 10 Account objects. The reason that part needs `Account` too is to do with inheritance but is outside the scope of this question – Oscar Chambers Oct 13 '17 at 09:04
  • To access methods or variables of the object you need to use the dot operator `.` For example if you would like to access the first object in your array you would say `accounts[0].myFunction(/*arguments*/);` and to access variables you would write `accounts[0].myVariable;`. The first element is index 0, and the last element is the array size - 1 – Oscar Chambers Oct 13 '17 at 09:07
  • Thanks for the explanation. – Shashidhar Y Bhat Oct 13 '17 at 10:29
  • Please feel free to plus 1 my comments if they helped :) – Oscar Chambers Oct 13 '17 at 10:30