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:
public class BankAccountMain {
public static void main(String[] args) {
}
}
public class BankAccount {
// private data (instance fields) here
private double myCurrentBalance;
// Constructors
/**
Constructs a bank account
*/
public BankAccount() {
// initialize current balance to 0
}
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance) {
// initialize current balance to initialBalance
}
// methods
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount) {
// Add amount to balance
}
/**
Withdraws money from the bank account
@param amount the amount to withdraw
*/
public void withdraw(double amount) {
// Subtract amount from balance
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance() {
// Return balance
return 0.0; // This is necessary so the project will compile!!
}
}
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.
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:
public class CarTest {
public static void main(String[] args) {
}
}
public class Car {
private double myMilesPerGallon; // the MPG of the car
private double myGas; // the amount of gas in the car
/**
Constructs a car with gas gallons in the tank and
with milesPerGallon miles per gallon
@param gas - amount of gas in tank
@param milesPerGallon - the miles per gallon this car gets
*/
public Car(double gas, double milesPerGallon) {
// Initialize instance fields with gas and milesPerGallon
}
/**
Adds gas to the tank
@param gas - the amount of gas to be added
*/
public void addGas(double gas) {
// Add gas to myGas
}
/**
Drives the car a certain distance and
updates the amount of gas in the tank
@param miles - the amount of miles driven
*/
public void drive(double miles) {
// Determine how much gas was used
// Subtract this amount from myGas
}
/**
Gets the amount of gas in the tank
*/
public double getGas() {
// Return the number of gallons of gas in the tank
return 0.0;
}
}
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.