Chapter 4: Writing Classes Lab Exercises
Chapter 4: Writing Classes Lab Exercises
Lab Exercises
Topics Lab Exercises
Classes and methods Prelab Exercises
A Bank Account Class
Tracking Grades
A Band Booster Class
Representing Names
Drawing Squares
ii. Deposit a given amount into the account. This changes the account balance, but does not return a value.
iii. Get the balance from the account. This does not change anything in the account; it simply returns the balance.
iv. Return a string containing the account information (name, account number, balance). This does not change
anything in the account.
v. Charge a $ 10 fee. This changes the account balance but does not return a value.
vi. Create a new account given an initial balance, the name of the owner, and the account number. Note that this
will be a constructor, and that a constructor does not have a return type.
2. File ManageAccounts.java contains a shell program that uses the Account class above. Save it to your directory, and
complete it as indicated by the comments.
3. Modify ManageAccounts so that it prints the balance after the calls to chargeFees. Instead of using the getBalance
method like you did after the deposit and withdrawal, use the balance that is returned from the chargeFees method. You
can either store it in a variable and then print the value of the variable, or embed the method call in a println statement.
// *******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, charge a fee to, and print a summary of the account.
// *******************************************************
// ---------------------------------------------
//Constructor -- initializes balance, owner, and account number
// --------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
// --------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
// --------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
// --------------------------------------------
// Adds deposit amount to balance.
52 Chapter 4: Writing Classes
// --------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
// --------------------------------------------
// Returns balance.
// --------------------------------------------
public double getBalance()
{
return balance;
}
// --------------------------------------------
// Returns a string containing the name, account number, and balance.
// --------------------------------------------
public String toString()
{
}
// --------------------------------------------
// Deducts $10 service fee //
// --------------------------------------------
public void chargeFee()
{
}
// --------------------------------------------
// Changes the name on the account
// --------------------------------------------
public void changeName(String newName)
{
}
}