CSci 2101 Data Structures: Lab 4

The following in-class examples might be helpful: Person and CharStack.

Problem 1: bank account

Your task for this problem is to write the missing methods in the class BankAccount and to test these methods in the class TestBankAccount.

BankAccount class has two instance variables: the amount of money in the account and the PIN code. Unlike a real ATM code, which can only be 4 digits, the code in our example can be any integer. The class currently has the two instance variables (the amount and the code) and a constructor which initializes them. Note that the constructor takes two parameters, and so do two of the three methods that you have to write.

Your task is to write the three methods below according to the way they are described in comments and the testing code for each of these methods in the class TestBankAccount below.

public class BankAccount {
    private double amount; // the amount in the account
    private int code;       // the ATM code (can be any number)

    // Create a bank account with this amount and this code
    // Note that the method parameters theAmount and theCode
    // have different names than the instance variables amount
    // and code. Use this approach to avoid confusion between 
    // the instance variables and method parameters
    public BankAccount(double theAmount, int theCode) {
	amount = theAmount;
	code = theCode;

    // ********************** deposit ********************************
    // a method deposit: 
    // takes two parameters (the additional amount and the code),
    // checks that the code is the same as the one stored in the 
    // instance variable 'code'. If they match, the method
    // adds the additional amount to the instance variable amount,
    // otherwise an error message is printed and the amount doesn't
    // change.

    // ********************** withdraw ********************************
    // a method withdraw: 
    // takes two parameters (the withdrawal amount and the code),
    // checks that the code is the same as the one stored in the 
    // instance variable 'code'. If they match, the method
    // checks if the amount is sufficient for the withdrawal.
    // If yes, the withdrawal amount is subtracted from the instance 
    // variable amount. Error messages should be printed if the code
    // is wrong or the amount is not sufficient


    // ****************** getBalance ************************* 
    // the method getBalance checks the PIN code and, if the code is correct,
    // returns the current account balance. If the code is incorrect,
    // the method prints an error message and returns 0 as the
    // balance. The state of the account doesn't change

public class TestBankAccount {
    public static void main(String [] args) {
	// create a bank account with the initial amount $352.33
	// and the code 123
	BankAccount myaccount = new BankAccount(352.33, 123); 
	// write your testing code here:


Problem 2: jumping frogs

Your task is to implement a class Frog. A frog lives on an a square island with the side of 20 feet. The island is surrounded by water. A frog is initially at the center of the island which is a point with coordinates x = 0, y = 0 (these should be set by the constructor).

The class has three methods:

This is the testing class for the class Frog. The two frogs make random jumps (with the parameters ranging between -5 and 5 for both x and y).

import java.util.*;

public class TestFrog {
    public static void main(String [] args) {
	Frog fred = new Frog();
	Frog frida = new Frog();

	// who will get in water first?

	// a random number generator
	Random r = new Random();
	// Each frog makes 10 random jumps 
	for(int i = 0; i < 10; ++i) {
	    System.out.println("Fred: ");
	    // fred's random jump. x and y increments are randomnly generated
	    // in the range from -5 to 5. 
	    // Why are there 11 possible values? 
	    fred.jump(r.nextInt(11) - 5, r.nextInt(11) - 5);

	    // frida's random jump
	    System.out.println("Frida: ");
	    frida.jump(r.nextInt(11) - 5, r.nextInt(11) - 5);    

Your task is: Extra credit: change the program so that the island is round with the radius 10.
This is a lab from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.