The University of Western Australia
School of Computer Science and Software Engineering
 
 

School of Computer Science and Software Engineering

UWA Java Tools

BankAccount Laboratory Exercise

In this Java lab you will gain experience using Java statements, expressions and conditionals to write a Java class. You will also be introduced to the BlueJ development environment, JUnit4 test environment and Checkstyle style checker.

Tasks

  1. Open BlueJ and use the "Project New Project" menu options to create a new project. Call your project week03 or something similar.
  2. Create a New Class called "BankAccount" using the New Class button or "Edit New Class" menu. Make sure your capitals and spelling are exactly as shown here.
  3. Open your "BankAccount" class (double click on it) and fill in your name and date in lines 5 and 6.
  4. Add all the following fields (instance variables) to your class. The purpose of each field will be made clear in the methods. For each field you should also include a getter method that returns information about the state of an object by giving the current value of the field.
        private int balance;
        private String accountName;
    

        public int getBalance() {
            return balance;
        }
    
        public String getAccountName() {
            return accountName;
        }
    
  5. Add a constructor to create BankAccount objects. The constructor should be placed between the field declarations and any other methods (i.e. your getter methods). Your task is to complete this constructor by adding code (between the braces) that initialises the fields.
        /**
         * Construct a bank account with given initial balance and account name
         * @param accName unique identifying string
         * @param accBalance opening balance of account, assumed non-negative
         */
        public BankAccount(String accName, int accBalance) {
            //TODO add code to initialise fields here
        }
    
  6. Next, add some new functionality to the class by implementing code for each of the following method signatures. Use the method signature, its Javadoc comment and the test cases in BankAccountBasicTest to tell you what the contract behaviour is for each method.
        /**
         * Deposit amount by adding to balance
         * @param amount must be non-negative
         */
        public void deposit (int amount) {
           //TODO add your code here
        }
    
        /**
         * Withdraw amount by decreasing balance
         * @param amount must be non-negative
         */
        public void withdraw (int amount) {
          //TODO add your code here
        }
    
        /**
         * Is the account overdrawn with a negative balance ?
         * @return boolean true if the acccount balance is strictly less than 0 and false otherwise
         */
        public boolean isOverdrawn() {
          //TODO add your code here
        }
    
  7. Download the JUnit4 test class BankAccountBasicTest.java into your project. The simplest way to do this is to download it to your Desktop and drag the file into BlueJ.
  8. Run the test (using the Run Tests button) or right clicking on the test class to check that your code behaves as intended. If you see the JUnit green line you are done. Otherwise, if you see a red line, read the error message for each failed test or look at the test source code to see what is wrong. Modify your code and run the tester again. Ask a lab demonstrator or lecturer for help, or post a message to help1200 if you get stuck.
  9. Your code should also satisfy the CS-readability Checkstyle style rules. The week 2 introductory lab and Checkstyle help describe how to to run these checks.

    You can either submit now (for a pass grade) or go on to add some more advanced functionality to the BankAccount for a credit or distinction grade.

  10. For a credit grade, add the following fields to your BankAccount class. Remember fields go at the beginning of the class. Add getter methods for these fileds at the end of the class. The first two fields are intended to remember the total values of deposits or withdrawals made so far, while the last two remember the maximum and minimum balance that the account has ever held.
        private int valueDeposits;
        private int valueWithdrawals;
        private int maximumBalance;
        private int minimumBalance;
    

        public int getValueDeposits() {
            return valueDeposits;
        }
    
        public int getValueWithdrawals() {
            return valueWithdrawals;
        }
    
        public int getMinimumBalance() {
            return minimumBalance;
        }
    
        public int getMaximumBalance() {
            return maximumBalance;
        }
    
  11. Download BankAccountStandardTest.java and BankAccountAdvancedTest.java, into the same directory as the basic tester.

    Run the BankAccountStandardTest. You will notice that some tests fail. Check the test cases to see what additional functionality is needed. You do not need to add any new methods for this part of the contract. But you do need to add more code to existing methods.

  12. A distinction grade will be awarded for code that passes all three test suites: Basic, Standard and Advanced. Use BankAccountAdvancedTest requires one new method that calculates interest to be credited or debited to a BankAccount. Add the following method and implement the behaviour specified by the signature and test contract.
        /**
         * Calculate and update interest
         * Interest should be credited if balance is positive and
         * debited if the balance is negative
         * @param rate must be double greater than 0
         */
        public void applyInterest(double rate) {
           //TODO add your code here
        }
    
    In this case, when you compile your code after adding the code for applying the interest to the balance, you may notice that the BlueJ will give you the error message at the bottom of the BlueJ window as "possibe loss of precision found: double required : int". This is because the parameter rate was given the type of double in the signature of the method applyInterest. However, the type of balance was int (you can see this at the beginning of the class). To make the task simple, you may need to use the cast operator. The cast operator is used to convert numeric values from one numeric type to another.In order to convert type double to integer. You can use the following code:
        balance = (int)(balance * rate) + balance;
    
    to casting the value with type of double to int.

      Submission and Assessment

      Submit (only) a single file BankAccount.java to cssubmit by the due date (see the CITS1200 Laboratory work web page). You can submit as many times as you wish. Minimal validation checks will be run when you submit but you are responsible for ensuring that your program passes all the JUnit test cases for the grade you have chosen.

      You can obtain feedback on your work at any time by asking a lab demonstrator, asking a lecturer, or using the feedback tools in the laboratory and help1200.

      Final Check Before you Submit

      Before you submit, make sure you have used the following JUnit4 tests and Checkstyle rules to ensure your code meets the contract for this laboratory.
      JUnit4 Test Suites
      Checkstyle Style Rules
      CS-readability.xml a submission that has no style violation errors for CS-readability.xml rule set will gain bonus marks