Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Interface Help

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Interface Help

    Hey guys. I'm starting to study Interfaces and I am sort of getting the hang of it. However, I am studying a code I got from my book and there is a specific part that I don't understand well. I put a comment besides it to see if someone could explain it to me. It's the part in the DataSet class where the Object x is added to the sum, but it goes though an invocation of the method in the Interface. Could anyone tell me why I have to put measurer.measure(x) ??? Here is the full code:

    /*
     * Describes any class whose objects can measure other objects.
     */
     
    public interface Measurer
    {
    	/**
    	 * Computes the measure of an object.
    	 * @param anObject the object to be measured.
    	 * @return the measure.
    	 */
     
    	double measure (Object anObject);
     
    }

    import java.awt.Rectangle;
     
    /**
     * Objects of this class measure rectangles by area. 
     * @author Jean
     *
     */
     
    public class RectangleMeasurer implements Measurer 
    {
    	public double measure(Object anObject)
    	{
    		Rectangle aRectangle = (Rectangle) anObject;
    		double area = aRectangle.getWidth() * aRectangle.getHeight();
    		return area;
    	}
     
    }

    /**
     * Computes the average of a ser of data values.
     * @author Jean
     *
     */
     
    public class DataSet 
    {
    	private double sum;
    	private Object maximum;
    	private int count;
    	private Measurer measurer;
     
    	/**
    	 * 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;
    	}
     
    	/**
    	 * Adds a data value to the data set.
    	 * @param x a data value.
    	 */
     
    	public void add(Object x)
    	{
    		sum = sum + measurer.measure(x); // DONT UNDERSTAND THE measure(x) PART
    		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 larger of the added data.
    	 * @return the maximum or 0 if no data has been added.
    	 */
    	public Object getMaximum()
    	{
    		return maximum;
    	}
    }

    import java.awt.Rectangle;
     
    /**
     * This program demonstrates the use of a Measurer.
     * @author Jean
     *
     */
     
    public class DataTester 
    {
    	public static void main(String[] args)
    	{
    		Measurer m = new RectangleMeasurer();
     
    		DataSet data = new DataSet(m);
     
    		data.add(new Rectangle(5,10,20,30));
    		data.add(new Rectangle(10,20,30,40));
    		data.add(new Rectangle(20,30,5,15));
     
    		System.out.println("Average area:" + data.getAverage());
    		System.out.println("Expected: 625");
     
    		Rectangle max = (Rectangle) data.getMaximum();
    		System.out.println("Maximum area rectangle: " + max);
    		System.out.println("Expeted:" + "java.awt.Rectangle[x = 10, y = 20, width = 30, height = 40]");
    		}
    }


  2. #2
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Interface Help

    This is the specific method:

    public void add(Object x)
    	{
    		sum = sum + measurer.measure(x); // DONT UNDERSTAND THE measure(x) PART
    		if(count == 0 || measurer.measure(maximum) < measurer.measure(x))
    		{
    			maximum = x;
    		}
    		count++;
    	}

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Interface Help

    What is measurer? It is an instance of RectangleMeasurer. So the code is calling the measure method in the RectangleMeasurer class passing an object as a parameter. What is the object that gets passed. What does the measure method do?
    Improving the world one idiot at a time!

Similar Threads

  1. Interface
    By Samaras in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2012, 05:27 PM
  2. marker interface
    By slicy in forum Java Theory & Questions
    Replies: 2
    Last Post: July 3rd, 2011, 08:56 AM
  3. problem with interface
    By buddy101 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 25th, 2011, 07:47 AM
  4. Interface
    By kaasi in forum Member Introductions
    Replies: 4
    Last Post: April 21st, 2011, 10:45 AM
  5. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM