CS 1063  Week 5:  An Introduction to Objects and Classes 

Designing and Implementing a Class

Objectives

Assignments


The BankAccount Class

Activity 1:  Designing the public interface of the class

You have been ask to  design and implement a BankAccount class.  Essential operations of a bank account such as making a deposit and withdrawal should be furnished.  Information such as a current balance should be furnished.

Step 1:  Consider the kind of operations you want to carry out.  These are actions that will be furnished by your class and should be verbs.  This will give you the methods in the class.

Step 2: Determine instance fields (data) for your class.  What information does an object need to store to do its job?  List beside each method from step 1 the information that is necessary to complete the task.

The current balance is needed in all of the methods so it should be an instance field.  The amount of deposit and withdrawal will change each time the method is called so this information must come from the client of the class; therefore, it will be placed in the parameter list of the method.

private double myCurrentBalance;

Step 3:  Consider how you will need to open a new account.  The purpose of a constructor is to initialize the instance field data.  Consider how this should be done and this will give you the constructor(s)

Overloaded methods are methods with the same name but different parameter types.

Step 4:  Write the public interface by listing your constructors and methods below.  Be sure and document what each method should do.

// Constructors

/**
    Constructs a bank account with a zero balance.
*/
public BankAccount()
// Current balance assigned the value of 0

/**
    Constructs a bank account with a given balance
    @param initialBalance the initial balance
*/
public BankAccount (double initialBalance)
// Current balance assigned the value of initialBalance

// Methods

/**
   Deposits money into the bank account.
   @param amount the amount to deposit
*/
public void deposit (double amount)
// Adds the amount of the deposit to the current balance

/**
    Withdraws money from the bank account
@param amount the amount to withdraw
*/ public void withdraw(double amount) // Subtracts the amount of the withdrawal from the current balance. // Assume there is enough money in the current balance /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() // returns the current balance

Step 5:  Create your project and begin implementing the class using methods without the code.  A method without any code except a return if necessary is referred to as a stub.  Add comments describing what the method will do.

Setup:

Step 6:  Implement the constructors and methods using the comments you wrote for the method stubs.  Run your project to check for any errors.

Step 7:  Test your class.  Write the main method in BankAccount.java.

The Car Class

Activity 2:  Designing the public interface of the class

Design and Implement a Car class.  The operations of the car should be adding gas to the gas tank, driving a certain distance and checking the amount of gas left in the tank.

Step1:  Consider what kind of operations you want to carry out.  These are actions that will be furnished by your class and should be verbs.  This will give you the methods in the class.

Step 2:  Determine instance fields (data) for your class.  What information an object needs to store to do its job.  List beside each method from step 1 the information that is necessary to complete the task.

Step 3:  Consider how you will need to begin your simulation.  This will give you the constructor:

public Car(double gas, double milesPerGallon)
//  initialize the car with gas gallons in the tank
//  miles per gallon necessary to determine gas used when driving

Step 4:  Write the public interface by listing the constructors and methods.  Be sure and document what each method should do.

Step 5:  Create your project and begin implementing the class using method stubs (methods without the code) and comments describing what the methods will do.

Setup:

Step 6:  Implement the constructors and methods using the comments you wrote in the stubs.  Run your project to check for any errors.

Step 7:  Test your class.  Add the following to CarTest.java.

Project 1 Discussion