You are on page 1of 2

Java Tutorial 3: Exercises

11th Feb 2009: Patricia Shaw www.dur.ac.uk/p.h.shaw/teaching/java 1. Text Encrpytion Aim: To write a program that will use two arrays of 26 letters in a different order to encode and decode messages by switching the letters in the message for the letters at the same position of the other array. Notes: When an int is cast to a char it takes on the character from the ascii table, so a-z are values 97-122 and A-Z are values 65-90. The space character has value 32. Language features used: Arrays, Casting, conditional-statements, looping and methods on Strings (charAt, length, toLowerCase/toUpperCase) i.e. String API. More details on these methods can be found in the java API documentation. Steps: a. In the letters class or a new class, dene two class variables to hold an array of chars. b. Initialise these either with a for loop or myArray = {a,b,c,...}; c. Write an encrypt method header that takes a message string as a parameter and returns an encrypted string d. Ensuring that the case of the input message matches your arrays, use a for loop to step through the string one character at a time. e. Find the index of the current character in the rst array. This can either be done by searching through the array or by calculating the index where dened in sequence e.g. for lowercase subtract 97 from the int value of the character to get the index. Remember a = 97. f. Retrieve the character at the same index of the second array and append this to the encoded message to be returned. g. Repeat steps c-f to write a decryption method to reverse the encryption. 2. Banking Program Aim: To write a program to simulate a bank which uses a BankAccount object to describe the accounts held by customers. Notes: The bankAccount should contain the customers name and current balance. Accounts can be opened with or without an initial deposit, but a name is always needed. An account should be able to handle deposits, withdrawals and transfers between bank accounts. Validation of actions are important, e.g. deposits shouldnt be negative and conrmation of a successful deposit is appreciated. The bank should maintain a list of all its open accounts with the ability to add and remove customers and obtain their details. Possible extensions: Introduce an overdraft limit that will allow the user to make one withdrawal into that limit which then has to be repaid before they can withdraw anymore. Allow the bank to give out loans to customers. These can be paid back slowly, and customers can still withdraw money whilst paying back loans, provided they are not in their overdraft limit. Language features used: Constructor and method overloading, Object datatypes, ArrayLists, Iterators Steps: BankAccount Object

a. Create a new class in blueJ, removing the auto-generated content, and dene the header for a class BankAccount b. Add eld variables for the customers name and the balance. c. Write constructors that will allow accounts to be opened using just a name or a name and an initial deposit. d. Write accessor methods to get the balance and customer name e. Write mutator methods to deposit and withdraw cash from the BankAccount, making sure the balance is available then conrming the success or failure of the action. f. Making use of the deposit and withdraw methods, write a method to transfer money from this BankAccount to another BankAccount. Bank Object a. Create a second class, clearing out the unnecessary auto-generated content. b. Add a eld variable to store a list of customers. Remember to import the necessary Java classes. c. Add a constructor to initialise the list. d. Add methods to add new customers. Remember that new accounts can be created with or without an initial deposit. e. Write methods to list all the customers, using an iterator, and to get the BankAccount objects of specic customers. f. Using the method to get a bankAccount object for a customer, add a method to make transfers between two customers. g. Finally write a method to handle transactions that will call the relevant deposit or withdraw method of BankAccount depending on whether the transaction is for a positive or negative amount. Remember that when withdrawing cash the BankAccount is still expecting a positive number. h. Add any further methods you think may be useful for the bank to have.

You might also like