Array code not producing, thoughts?
OutPut is supposed to be: Size: 3
first account number is 1008
last account number is 1729
Sub
Code :
import java.util.ArrayList;
public class BankAccount2{
public BankAccount2(int anAccountNumber) {
accountNumber = anAccountNumber;
balance = 0;
}
public BankAccount2(int anAccountNumber, double initialBalance){
accountNumber = anAccountNumber;
balance = initialBalance;
}
public int getAccountNumber(){
return accountNumber;
}
public void deposit(double amount){
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount){
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance(){
return balance;
}
private int accountNumber;
private double balance;
}
Main
Code :
public class ArrayListTester {
public static void main(String[] args) {
ArrayList<BankAccount2> accounts = new ArrayList<BankAccount2>();
accounts.add(new BankAccount2(1001));
accounts.add(new BankAccount2(1015));
accounts.add(new BankAccount2(1729));
accounts.add(1,new BankAccount2(1008));
accounts.remove(0);
System.out.println("size" + accounts.size());
BankAccount2 first = accounts.get(0);
System.out.println("first account number is: " + first.getAccountNumber());
BankAccount2 last = accounts.get(accounts.size()-1);
System.out.println("last account number is: " + last.getAccountNumber());
}
}
This program is in my notebook, was testing professor's program but it isn't functioning..
Re: Array code not producing, thoughts?
Quote:
Originally Posted by
r19ecua
OutPut is supposed to be: Size: 3
first account number is 1008
last account number is 1729
Sub
Code :
import java.util.ArrayList;
public class BankAccount2{
public BankAccount2(int anAccountNumber) {
accountNumber = anAccountNumber;
balance = 0;
}
public BankAccount2(int anAccountNumber, double initialBalance){
accountNumber = anAccountNumber;
balance = initialBalance;
}
public int getAccountNumber(){
return accountNumber;
}
public void deposit(double amount){
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount){
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance(){
return balance;
}
private int accountNumber;
private double balance;
}
Main
Code :
public class ArrayListTester {
public static void main(String[] args) {
ArrayList<BankAccount2> accounts = new ArrayList<BankAccount2>();
accounts.add(new BankAccount2(1001));
accounts.add(new BankAccount2(1015));
accounts.add(new BankAccount2(1729));
accounts.add(1,new BankAccount2(1008));
accounts.remove(0);
System.out.println("size" + accounts.size());
BankAccount2 first = accounts.get(0);
System.out.println("first account number is: " + first.getAccountNumber());
BankAccount2 last = accounts.get(accounts.size()-1);
System.out.println("last account number is: " + last.getAccountNumber());
}
}
This program is in my notebook, was testing professor's program but it isn't functioning..
You can add "=Java" to the first code tag to highlight the Java elements.
Can you post the output of the program?
Re: Array code not producing, thoughts?
Dude, I got the exact output you wanted when I ran this program.
Size: 3
First account number is: 1008
Last account number is: 1729
What is not working for you?
Re: Array code not producing, thoughts?
Oh I think I know what it is...
You've declared "import java.util.ArrayList" in the BankAccount2 class and not in the ArrayListTester class. I presume since you have BankAccount2 as a public class, its not in the same file as ArrayListTester which then would arise a problem since the ArrayListTester would have no clue what an ArrayList is.... :P
Does that fix it for you...? Code works fine for me. I've compiled it both as two seperate java files and one big file and they both produce the expected outcome :)