Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Overloading the constructor...help please

807601Apr 4 2008 — edited Apr 4 2008
I am a first year Computer Engineering student and we were given the following assignment:



File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance
and account number, and return a String representation. Modify the file as follows:

*1. Overload the constructor as follows:*
(a) public Account (double initBal, String owner, long number) - initializes the balance, owner, and account number as specified.
(b) public Account (double initBal, String owner) - initializes the balance and owner as specified; randomly generates the account number.
(c) public Account (String owner) - initializes the owner as specified; sets the initial balance to 0 and randomly generates the account number.


*2. Overload the withdraw method with one that also takes a fee and deducts that fee from the account.*

TestAccount.java contains a simple program that exercises these methods. Use it as a test file.

When I attempt to compile and run TestAccount.java, I get several lengthy errors and I'm not quite sure what I'm doing wrong or have left out of the code. If someone could help me out, I would really appreciate it. I'm new at this and VERY confused.

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************

public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts;

//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account (int initBal, String owner, int number)
{
balance = initBal;
name = owner;
acctNum = number;
numAccounts++;
}

//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}

//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}

//----------------------------------------------
// Returns the total number of accounts.
//----------------------------------------------
public static int getNumAccounts()
{
return numAccounts;
}

//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}


//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name:" + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
}


//*******************************************************
// TestAccount.java
//
// A simple driver to test the overloaded methods of
// the Account class.
//*******************************************************

import java.util.Scanner;

public class TestAccount
{
public static void main(String[] args)
{
String name;
double balance;
long acctNum;
Account acct;

Scanner scan = new Scanner(System.in);

System.out.println("Enter account holder's first name");
name = scan.next();
acct = new Account(name);
System.out.println("Account for " + name + ":");
System.out.println(acct);

System.out.println("\nEnter initial balance");
balance = scan.nextDouble();
acct = new Account(balance,name);
System.out.println("Account for " + name + ":");
System.out.println(acct);

System.out.println("\nEnter account number");
acctNum = scan.nextLong();
acct = new Account(balance,name,acctNum);
System.out.println("Account for " + name + ":");
System.out.println(acct);

System.out.print("\nDepositing 100 into account, balance is now ");
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print("\nWithdrawing $25, balance is now ");
acct.withdraw(25);
System.out.println(acct.getBalance());
System.out.print("\nWithdrawing $25 with $2 fee, balance is now ");
acct.withdraw(25,2);
System.out.println(acct.getBalance());

System.out.println("\nBye!");
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 2 2008
Added on Apr 4 2008
11 comments
1,688 views