Assignment 1: Bank Account Class

You are hired by a Canadian bank in order to develop a java application for processing withdraw
and deposit transactions. The application enables staff of the bank to create new bank accounts,
to process account transactions and to display the balance of a particular account.
Details

Complete the following parts:
Part 1(6 marks): Draw the UML diagram for the Account class and then implement it.
The class contains:
 A private int data field named id for the account (default 0)
 A private double data field named balance for the account (default 0)
 A private double data field named annualInterestRate that stores the current interest rate.
The annualInterestRate is the same for all accounts.
 A no-argumentconstrutor that creates the defaults account
 A constructor that creates an account with specified id and initial balance
 The accessor ( getter) and mutator (setter) methods for the data fields: id, balance, and
annualInterestRate
 A method named getMontheyIterest() that returns the monthly interest. The monthly
interest = balance * (annualInterestRate/1200)
 A method named withdraw that withdraws a specified amount from the account
 A method named deposit that deposits a specified amount to the account

Part 2( 4 marks): Develop TestAccount Calss that contains:
 A main method that does the following:
o Define an array of three bank accounts
o Creates three bank accounts with the Ids 1,2, and 3 with zero balance
o Gets the annual interest rate which is the same for all accounts
o Gets the initial balance of the three accounts from the user
o Uses the setter methods of the account to set the balance and the interest rate for
the three accounts
o Deposit the amounts 1000, 2000, and 3000 to the three accounts respectively
o Withdraw the amounts 200,450 from accounts 1 and 2
o Prints out the balance of the three accounts

The assignment uses a lot of words for not a lot of work. That's a good thing, because it's very detailed, and all you need to do is put it in UML/code form.

So now for the obligatory "what have you done"? It's against Daniweb rules to post just a homework assignment with the expectation that someone will do it for you.

        public class Account {


            //A private int data field named id for the account (default 0).
            private int id;

            //A private double data field named balance for the account (default 0). 
            private double balance;

            //A private double data field named annualInterestRate that stores the current interest rate. The annualInterestRate is the same for all accounts. 
            private static double annualInterestRate;

            //A no-argumentconstrutor that creates the defaults account.
            public Account()  {
                this.id = 0;
                this.balance = 0;
            }


            //A constructor that creates an account with specified id and initial balance. 
            public Account(int newId, double newBalance) {
                this.id = newId;
                this.balance = newBalance;

            }

            //The accessor ( getter) and mutator (setter) methods for the data fields: id, balance, and annualInterestRate. 

            //This is an accessor method for the id.
            public int getId() {
                return this.id;
            }

            //This is an accessor method for the balance.
            public double getBalance() {
                return this.balance;
            }

            //This is a static accessor method for the annualInterestRate
            public static double getAnnualInterestRate() {
                return annualInterestRate;
            }


            //The following methods will all be the mutator methods, or set methods.
            //  //Mutator method for the account id.
            public void setId(int newId) {
                this.id = newId;
            }

            //Mutator method for the balance
            public void setBalance(double newBalance) {
                this.balance = newBalance;
            }
            //Mutator method for the annualInterestRate
            public static void setAnnualInterestRate(double newAnnualInterestRate) {
                annualInterestRate = newAnnualInterestRate;
            }

            //A method named getMonthlyInterestRate that returns the monthly interest rate. 
            //Accessor method for the monthlyInterest
            public double getMonthlyInterest() {
                return this.balance * (annualInterestRate / 1200.0D);
            }

            //A method named withdraw that withdraws a specified amount from the account
            public void withdraw(double amount) {
                this.balance -= amount;
            }


            //A method named deposit that deposits a specified amount to the account 
            public void deposit(double amount) {
                this.balance += amount;
            }
        }

TestAccount class with main method.

public class TestAccount {

    public static void main(String[] args) {
    // add your code here


    }//main

}//TestAccount

Account

It looks like you forgot to initialize 'id' and 'balance' to 0. Line 5 and line 8.

No problem, they are initialised in both constructors.

(edit) ... but anyway the language guarantees that instance variables are initialised (to 0, 0.0, false, or null as appropriate) anyway. See Java Language Spec:

4.12.5 Initial Values of Variables
...
• Each class variable, instance variable, or array component is initialized with a default value when it is created...
◆ For type byte, the default value is zero, that is, the value of (byte)0.
◆ For type short, the default value is zero, that is, the value of (short)0.
◆ For type int, the default value is zero, that is, 0.
◆ For type long, the default value is zero, that is, 0L.
◆ For type float, the default value is positive zero, that is, 0.0f.
◆ For type double, the default value is positive zero, that is, 0.0d.
◆ For type char, the default value is the null character, that is, '\u0000'.
◆ For type boolean, the default value is false.
◆ For all reference types (§4.3), the default value is null.

Edited 2 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.