Hi Everyone. I am working on an assignment with the following instructions...

Enhance the DataSet class so that it can either be used with a Measurer object or for processing Measurable objects. Hint: Supply a default constructor that implements a Measurer that processes Measurable objects.

I'm confused about how to implement a Measurer that processes Measurable objects. Since the DataSet class is the only thing I can alter, I attempted to use an inner class within the default constructor. However, it is not quite working. Any help you can give me with the DataSet class would be greatly appreciated.

public class DataSet { 
 
       /** 
      Constructs an empty data set with a given measurer. 
      @param aMeasurer the measurer that is used to measure data values 
   */ 
   public DataSet(Measurer aMeasurer) 
   { 
      sum = 0; 
      count = 0; 
      maximum = null; 
      measurer = aMeasurer; 
   } 
 
    DataSet() { 
        sum = 0; 
        count = 0; 
        maximum = null; 
 
        class Bank implements Measurer{ 
 
            public double measure(Object anObject) { 
                BankAccount aBankAccount = (BankAccount) anObject; 
                double balance = aBankAccount.getBalance(); 
                return balance; 
            } 
 
        } 
    } 
 
   /** 
      Adds a data value to the data set. 
      @param x a data value 
   */ 
   public void add(Object x) 
   { 
      sum = sum + measurer.measure(x); 
      if (count == 0 
            || measurer.measure(maximum) < measurer.measure(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 Object getMaximum() 
   { 
      return maximum; 
   } 
 
 
 
   private double sum; 
   private Object maximum; 
   private int count; 
   private Measurer measurer; 
   private Measurable measurable; 
    
   
}
import java.awt.Rectangle; 
 
/** 
   This program tests a data set that can be used with measurers 
   and measurables. 
*/ 
public class DataSetTester 
{ 
   public static void main(String[] args) 
   { 
      class RectangleMeasurer implements Measurer 
      { 
         public double measure(Object anObject) 
         { 
            Rectangle aRectangle = (Rectangle) anObject; 
            double area = aRectangle.getWidth() * aRectangle.getHeight(); 
            return area; 
         } 
      } 
 
      Measurer m = new RectangleMeasurer(); 
 
      DataSet d = new DataSet(m); 
 
      d.add(new Rectangle(5, 10, 20, 30)); 
      d.add(new Rectangle(10, 20, 30, 40)); 
      d.add(new Rectangle(20, 30, 5, 10)); 
 
      System.out.println("Average area: " + d.getAverage()); 
      System.out.println("Expected: 616.6666667"); 
 
      Object max = d.getMaximum(); 
      System.out.println("Largest area: " + m.measure(max)); 
      System.out.println("Expected: 1200"); 
 
 
      // Test the default Measurer 
 
       
      d = new DataSet(); 
 
      d.add(new BankAccount(2000)); 
      d.add(new BankAccount(200)); 
      d.add(new BankAccount(20000)); 
 
      System.out.println("Average balance: " + d.getAverage()); 
      System.out.println("Expected: 7400"); 
      Measurable max2 = (Measurable) d.getMaximum(); 
      System.out.println("Highest balance: " + max2.getMeasure()); 
      System.out.println("Expected: 20000"); 
   } 
}
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;
public interface Measurable 
{ 
   /** 
      Computes the measure of the object. 
      @return the measure 
   */ 
   double getMeasure(); 
}
public interface Measurer 
{ 
   /** 
      Computes the measure of an object. 
      @param anObject the object to be measured 
      @return the measure 
   */ 
   double measure(Object anObject); 
}

Recommended Answers

All 2 Replies

Your directions seem insufficient. Did you not include all of the project directions?

The only thing I didn't include was a statement to use the DataSetTester as a tester class. Otherwise, I included all of the directions given for the assignment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.