Bank Account Static (Required Exercise) PDF

Title Bank Account Static (Required Exercise)
Course Programming Fundamentals
Institution University of Technology Sydney
Pages 4
File Size 45.7 KB
File Type PDF
Total Downloads 39
Total Views 117

Summary

required ex in week 8, one of two required exercises on this document

This exercise is similar to the "Calling Methods" exercise, but flipped. Here we will be filling out methods so that they function as specified. The code here is also a little "simpler", in that it is contained in a s...


Description

/** * This "BankAccount" class is a simple introduction to * methods and variables / fields / private data members. * * @author Raymond Lister * @version April 2015; a "static" version for a Lab Test */ /* * HINT: You should NOT need to memorise much to complete * this skeleton. The type of the variables * "accountNumber", "accountName" and "balance" * (i.e. int, String and double respectively) * provide useful information. */ public class BankAccountStatic { /* * The variables follow. These are also * called "fields" or "?"private data members". */ private static int accountNumber = 0; private static String accountName = "no name"; private static double balance; // eg. 1.27 means $1.27 public static void main(String[] args) { System.out.println("The initial value for account number is " + accountNumber + ". Look at line 24."); System.out.println("The initial value for account name is " + accountName + ". Look at line 26."); System.out.println("The initial value for balance is " + balance + ". Look at line 28."); BankAccountStatic.setAccountNumber(12345678); BankAccountStatic.setAccountName("Samantha"); //the above two lines should update the values stored in accountNumber and accountName to the given value. Let's test it: System.out.println("The new account number is 12345678. Your program's new account number is " + accountNumber +". If these two numbers don't match then something is not right...");

System.out.println("The new account name is Samantha. Your program's new account name is " + accountName+". If these two names don't match then something is not right..."); //lets put some money in the account BankAccountStatic.deposit(3.70); //the new balance should be 3.70. Let's check it: System.out.println("New balance should be 3.70. Your program's new balance is " + balance+". If these two numbers don't match then something is not right..."); //let's put some more money in our bank account: BankAccountStatic.deposit(100.00); //the new balance should be 103.70. Let's check it: System.out.println("New balance should be 103.70. Your program's new balance is " + balance+". If these two numbers don't match then something is not right..."); //let's withdraw some money now. the new balance should be 93.20. Let's check it: System.out.println("After this line, the new balance should be 93.20. Your program's balance after calling withdraw(10.50) is " + BankAccountStatic.withdraw(10.50) + ". If these two numbers don't match then something is not right..."); //let's attempt to withdraw more than what we have in the account. The result of print statement should be zero: System.out.println("Attempting to withdraw 1000.00 dollars from this bank accoutn should return a -1.0. Your program's return value after calling withdraw(1000.00) is " + BankAccountStatic.withdraw(1000.00) + ". If these two numbers don't match then something is not right..."); } /** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static int getAccountNumber() { return accountNumber; }

/**

* This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static void setAccountNumber(int newAccount) { accountNumber = newAccount; }

/** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static String getAccountName() { return accountName; }

/** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static void setAccountName(String newNumber) { accountName = newNumber; }

/** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static double getBalance() { return balance;

}

/** * This method needs to be completed. It needs: * - a proper return type * - a proper list of parameters (maybe empty) * - code to actually do what it should */ public static double deposit(double depositedamount) { balance = balance + depositedamount; return balance; }

/** * @param amount To be subtracted from the balance * * @return the updated account balance or -1.0 if the withdrawal is refused * * The withdrawal is refused when the withdrawal would * have resulted in a negative balance. * * Note: This method "withdraw" has been provided in * its entirety. You do NOT have to make any * changes to it. */ public static double withdraw(double amount) { if ( balance >= amount ) { balance = balance - amount; return balance; } else return -1.0; } } // class BankAccountStatic...


Similar Free PDFs