Plain text version | Style: Default, Black and White, Bath

University of Bath Logo
Dr Marina De Vos
Department of Computer Science
Monday, 21-May-2018 09:43:46 BST

Programming I (CM10134 - CM50147): Lab 3

Carry out the following exercises from the BlueJ book, chapter 3 "Object Interaction".

You will need to type up your answers to exercises 3.2, 3.3, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12 (brief answers acceptable).
You will also need to show your code to the toString() question, exercises 3.29 and 3.30, and the BankAccount exercise.

  • Exercises 3.2 - 3.3

  • Exercises 3.5 - 3.12

  • Exercises 3.13 and 3.14
    toString() is a method of the Object class. It is often used for debugging. It's also used automatically in concatenating a string with an object. By default, toString() returns a string containing the receiver's class and address in memory. A more useful alternative is to return a string containing the class and the instance variable names and values. It can therefore be redefined in a class.

    For example, a BankAccount class could redefine the toString() method as follows:
    	public class BankAccount
    	{ ...
    	    public String toString()
    	  	return "BankAccount [ balance = " + balance + "]";
    Add a toString() method to the ClockDisplay class.

  • Exercises 3.16 and 3.17

  • Exercises 3.19 and 3.20

  • Exercises 3.29 and 3.30

  • Section 3.13 (Exercises 3.33 - 3.40)

  • Design and implement a BankAccount class.
    This requires starting a new project and creating a new class. As a minimum the class should contain
    • a field,private double balance;
    • a constructor that takes an initial balance as a parameter,
    • and the methods getBalance(), deposit() and withdraw(). Ensure that the deposit method only accepts positive amounts.
    • a toString() method!
    • add an overdraft limit.
    • make sure there are appropriate accessor and mutator methods for the fields.
    • add two more constructors, one that takes no parameters, and one that takes both the balance and the overdraft as parameters.
    • reject withdrawals that would result in overdrawing beyond the overdraft limit.
    • start using console reader to interact with the classes. The code for the ConsoleReader class can be accessed from here:
      To create a new ConsoleReader, you need to create a new field such as private ConsoleReader console; and add code similar to the following to a constructor:
    • console = new ConsoleReader(;
      System.out.println("Enter .... ");
      int variableName = console.readInt();