SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Inheritance And Interfaces
Exercise:
Exercises
Question:8 | ISBN:9780136091813 | Edition: 2

Question

MinMaxAccount. A company has written a large class BankAccount with many methods including:

Design a new class MinMaxAccount whose instances can be used in place of a bank account but include new behavior of remembering the minimum and maximum balances ever recorded for the account. The class should have a constructor that accepts a Startup parameter. The bank account’s constructor sets the initial balance on the basis of the startup information. Assume that only debits and credits change an account’s balance. Include these new methods in your class:

 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package inheritance;

// MinMaxAccount for BankingAccount class
public class MinMaxAccount extends BankingAccount {

	int minBalance;
	int maxBalance;

	public int getMinBalance() {
		return minBalance;
	}

	public int getMaxBalance() {
		return maxBalance;
	}

	public void setMinBalance(int minBalance) {
		this.minBalance = minBalance;
	}

	public void setMaxBalance(int maxBalance) {
		this.maxBalance = maxBalance;
	}

    // create constructor the MinMaxAccount which the Startup Class as argument
	public MinMaxAccount(Startup startup) {
		super(startup);
		minBalance = startup.s_getBalance();
		maxBalance = startup.s_getBalance();

	}

	public void credit(Credit credit) {
		super.Credit(credit);

		int temp = getBalance();
		if (temp < minBalance)
			minBalance = temp;

		if (temp > maxBalance)
			maxBalance = temp;

	}

	public void debit(Debit debit) {
		super.debit(debit);

		int temp = getBalance();

		if (temp < minBalance)
			minBalance = temp;

		if (temp > maxBalance)
			maxBalance = temp;
	}

	public static void main(String[] args) {

	}

}

 

0 0

Discussions

Post the discussion to improve the above solution.