DataSet
/**
Computes the average of a set of data values.
*/
public class DataSet
{
/**
Constructs an empty data set.
*/
public DataSet()
{
sum = 0;
count = 0;
maximum = 0;
}
/**
Adds a data value to the data set
@param x a data value
*/
public void add(double x)
{
sum = sum + x;
if (count == 0 || maximum < x) maximum = x;
count++;
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public double getMaximum()
{
return maximum;
}
private double sum;
private double maximum;
private int count;
}
[collapse]
BankAccount
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
private String name;
/**
Constructs a bank account with a zero balance
*/
public BankAccount()
{
balance = 0;
name = " ";
}
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance, String aName)
{
balance = initialBalance;
name = aName;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
}
[collapse]
Coin
/**
A coin with a monetary value.
*/
public class Coin
{
/**
Constructs a coin.
@param aValue the monetary value of the coin.
@param aName the name of the coin
*/
public Coin(double aValue, String aName)
{
value = aValue;
name = aName;
}
/**
Gets the coin value.
@return the value
*/
public double getValue()
{
return value;
}
/**
Gets the coin name.
@return the name
*/
public String getName()
{
return name;
}
private double value;
private String name;
}
[collapse]
BankDataSet
/**
Computes the average of a set of data values.
*/
public class BankDataSet
{
/**
Constructs an empty data set.
*/
public BankDataSet()
{
sum = 0;
count = 0;
maximum = null;
}
/**
Adds a bank account balance to the data set
@param x a data value
*/
public void add(BankAccount bkAcc)
{
sum = sum + bkAcc.getBalance();
if (count == 0 || maximum.getBalance() < bkAcc.getBalance()) maximum = bkAcc;
count++;
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public BankAccount getMaximum()
{
return maximum;
}
private double sum;
private BankAccount maximum;
private int count;
}
[collapse]
CoinDataSet
/**
Computes the average of a set of data values.
*/
public class CoinDataSet
{
/**
Constructs an empty data set.
*/
public CoinDataSet()
{
sum = 0;
count = 0;
maximum = null;
}
/**
Adds a bank account balance to the data set
@param x a data value
*/
public void add(Coin aCoin)
{
sum = sum + aCoin.getValue();
if (count == 0 || maximum.getValue() < aCoin.getValue()) maximum = aCoin;
count++;
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public Coin getMaximum()
{
return maximum;
}
private double sum;
private Coin maximum;
private int count;
}
[collapse]
Measurable
/**
* Interface Measurable: use the implements keyword to indicate that a class
* implements an interface type. Unlike a class, an interface type provides no implementation
* @GE
* @12/15/17
*/
/**
Describes any class whose objects can be measured.
*/
public interface Measurable
{
/**
Computes the measure of the object.
@return the measure
*/
double getMeasure();
}
[collapse]
BankAccount
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount implements Measurable
{
/**
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)
{
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public double getMeasure()
{
return balance;
}
private double balance;
}
[collapse]
Coin
/**
A coin with a monetary value.
*/
public class Coin implements Measurable
{
/**
Constructs a coin.
@param aValue the monetary value of the coin.
@param aName the name of the coin
*/
public Coin(double aValue, String aName)
{
value = aValue;
name = aName;
}
/**
Gets the coin value.
@return the value
*/
public double getValue()
{
return value;
}
/**
Gets the coin name.
@return the name
*/
public String getName()
{
return name;
}
public double getMeasure()
{
return value;
}
private double value;
private String name;
}
[collapse]
DataSet
/**
Computes the average of a set of data values.
*/
public class DataSet
{
/**
Constructs an empty data set.
*/
public DataSet()
{
sum = 0;
count = 0;
maximum = null;
}
/**
Adds a data value to the data set
@param x a data value
*/
public void add(Measurable x)
{
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
maximum = x;
count++;
}
/**
Gets the average of the added data.
@return the average or 0 if no data has been added
*/
public double getAverage()
{
if (count == 0) return 0;
else return sum / count;
}
/**
Gets the largest of the added data.
@return the maximum or 0 if no data has been added
*/
public Measurable getMaximum()
{
return maximum;
}
private double sum;
private Measurable maximum;
private int count;
}
[collapse]
DataSetTest
/**
This program tests the DataSet class.
*/
public class DataSetTest
{
public static void main(String[] args)
{
DataSet bankData = new DataSet();
bankData.add(new BankAccount(3000));
bankData.add(new BankAccount(10000));
bankData.add(new BankAccount(2000));
System.out.println("Average balance = "
+ bankData.getAverage());
Measurable max = bankData.getMaximum();
System.out.println("Highest balance = "
+ max.getMeasure());
//System.out.println("Get bank balance = "
// + max.getBalance());
BankAccount maxAccount = (BankAccount) max;
System.out.println("Get bank balance = "
+ maxAccount.getBalance());
DataSet coinData = new DataSet();
coinData.add(new Coin(0.25, "quarter"));
coinData.add(new Coin(0.1, "dime"));
coinData.add(new Coin(0.05, "nickel"));
System.out.println("Average coin value = "
+ coinData.getAverage());
max = coinData.getMaximum();
System.out.println("Highest coin value = "
+ max.getMeasure());
}
}
[collapse]
Classwork: Create a class Student that realizes the Measurable interface. Add to the driver, DataSetTest a data set of students. Print the average, the max value and the name of the student.
Submit to “Measurable – Interfaces Lesson Practice” post the following files (you can attach them): BankAccount, Coin, Student, DataSet and DataSetTest. Make sure the output is included in the test class as comment.
//********************************************************************
// Student.java Author: Lewis/Loftus/Cocking
//
// Represents a college student.
//********************************************************************
public class Student
{
private String firstName, lastName;
private double GPA; // for Measurable Interface purpose
//-----------------------------------------------------------------
// Sets up this Student object with the specified initial values.
//-----------------------------------------------------------------
public Student (String first, String last, double aGPA)
{
firstName = first;
lastName = last;
GPA = aGPA;
}
public String getName()
{
return firstName + " " + lastName + "\n";
}
//-----------------------------------------------------------------
// Returns this Student object as a string.
//-----------------------------------------------------------------
public String toString()
{
String result;
result = firstName + " " + lastName + "\n";
result += GPA
return result;
}
}
Assignments:
Programming Projects 5.3 and 5.4

