Java II Homework from \"big java early objects, 5th edition\" book End of chapte
ID: 3670566 • Letter: J
Question
Java II Homework from "big java early objects, 5th edition" book
End of chapter programming exercise P10.5 on page 509 of the textbook. Please use the code from section 10.1 as as starting point instead of from 10.4 (as the instruction in the question is not correct)
This is an example on how to implement two interface for one class
http://stackoverflow.com/questions/4546807/implementing-multiple-interfaces-with-java-is-there-a-way-to-delegate
A few hint. you need to create multiple accounts (array maybe?) and run them through the filter method to determine if they qualify or not. ($1000)
Chapter 10 from the book is saved here: https://drive.google.com/file/d/0B0zr-fIeDNG-TU42MzVvbTd6VDQ/view?usp=sharing
All source code for the book is here: https://drive.google.com/file/d/0B0zr-fIeDNG-UHZsVGszMGhfOWs/view?usp=sharing
Not sure how to do this, it's confusing... I need to complete it by 11:59 tonight, thanks
Explanation / Answer
BankAccount.java
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount implements Measurable
{
private double balance;
public static final Filter BalanceGreaterThanEqualTo1000Filter = new Filter() {
public boolean accept(Object x) {
Measurable obj = (Measurable) x;
return obj.getMeasure() >= 1000.0;
}
};
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
balance = balance + amount;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
balance = balance - amount;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public double getMeasure()
{
return balance;
}
}
Country.java
/**
A country with a name and area.
*/
public class Country implements Measurable
{
private String name;
private double area;
/**
Constructs a country.
@param aName the name of the country
@param anArea the area of the country
*/
public Country(String aName, double anArea)
{
name = aName;
area = anArea;
}
/**
Gets the country name.
@return the name
*/
public String getName()
{
return name;
}
/**
Gets the area of the country.
@return the area
*/
public double getArea()
{
return area;
}
public double getMeasure()
{
return area;
}
}
Data.java
public class Data
{
/**
Computes the average of the measures of the given objects.
@param objects an array of Measurable objects
@return the average of the measures
*/
public static double average(Measurable[] objects, Filter balanceGreaterThanEqualTo1000Filter)
{
double sum = 0;
int acceptedCount = 0;
for (Measurable obj : objects)
{
if (balanceGreaterThanEqualTo1000Filter.accept(obj))
{
sum = sum + obj.getMeasure();
acceptedCount++;
}
}
if (acceptedCount > 0) { return sum / acceptedCount; }
else { return 0; }
}
}
Filter.java
/**
Describes any class whose objects can be filtered.
*/
public interface Filter
{
/**
Checks if object is acceptable
@return true if accepted
*/
boolean accept(Object x);
}
Measurable.java
/**
Describes any class whose objects can be measured.
*/
public interface Measurable
{
/**
Computes the measure of the object.
@return the measure
*/
double getMeasure();
}
MeasurableTester.java
/**
This program demonstrates the measurable BankAccount and Country classes.
*/
public class MeasurableTester
{
public static void main(String[] args)
{
Measurable[] accounts = new Measurable[3];
accounts[0] = new BankAccount(700);
accounts[1] = new BankAccount(10000);
accounts[2] = new BankAccount(2000);
double averageBalance = Data.average(accounts, BankAccount.BalanceGreaterThanEqualTo1000Filter);
System.out.println("Average balance: " + averageBalance);
System.out.println("Expected: 6000");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.