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:10 | ISBN:9780136091813 | Edition: 2

Question

FilteredAccount. A cash processing company has a class called Account used to process transactions:

Account objects interact with Transaction objects, which have many methods including:

Design a new class called FilteredAccount whose instances can be used in place of normal accounts but which
include the extra behavior of not processing transactions with a value of 0. More specifically, the new class should
indicate that a zero-valued transaction was approved but shouldn’t call the process method for it. Your class should
have a single constructor that accepts a parameter of type Client, and it should include the following method:

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

package inheritance;

public class FilteredAccount extends Account {

	int total = 0;
	int zero = 0;

    // FileteredAccount class constructor takes Client object as argument
	public FilteredAccount(Client c) {
		super(c);
	}

    // returts the percent of transactions filtered out
	public double percentFiltered() {
		if (total == 0)
			return 0.0;

		return ((double) zero) * 100 / total;
	}


    // process method takes the Transaction class as argument
	public boolean process(Transaction transaction) {

		total += 1;
		if (transaction.value() == 0) {
			zero++;
			return true;
		}

		return super.process(transaction);
	}

	public static void main(String[] args) {

	}

}

 

0 0

Discussions

Post the discussion to improve the above solution.