Explore BrainMass

Explore BrainMass

    Java Object Oriented Programming using Classes and Interfaces

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    A person has a name and a height in centimeters. Implement a dataset to process a collection of Person objects. Display the average height and the name of the tallest person.

    Use the following class in your solution:
    /**
    This class represents a person's name and height
    */
    public class Person
    {
    private String name;
    private double height;

    /**
    Constructs a Person object
    @param aName the name of the person
    @param aHeight the height of the person
    */
    public Person(String aName, double aHeight)
    {
    name = aName;
    height = aHeight;
    }

    /**
    Gets the name of the person
    @return name the person's name
    */
    public String getName()
    {
    return name;
    }

    /**
    Gets the height of the person
    @return height the person's height
    */
    public double getHeight()
    {
    return height;
    }
    }

    Use the following class as your tester class:
    /**
    This program tests the measuring of Person objects.
    */
    public class PersonTester
    {
    public static void main(String[] args)
    {
    . . .

    DataSet data = . . .

    data.add(new Person("Joe", 183));
    data.add(new Person("Chrissy", 158));
    data.add(new Person("Bobby", 175));

    double avg = . . .
    Person max = . . .

    System.out.println("Average height: " + avg);
    System.out.println("Expected: 172");
    System.out.println("Name of tallest person: " + max.getName());
    System.out.println("Expected: Joe");
    }
    }

    DO NOT MODIFY THE FOLLOWING:

    /**
    Computes the average of a set of data values.
    */
    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);
    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;
    }
    }

    DO NOT MODIFY THE FOLLOWING
    /**
    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);
    }

    DO NOT MODIFY THE FOLLOWING
    /**
    This class represents a person's name and height
    */
    public class Person
    {
    private String name;
    private double height;

    /**
    Constructs a Person object
    @param aName the name of the person
    @param aHeight the height of the person
    */
    public Person(String aName, double aHeight)
    {
    name = aName;
    height = aHeight;
    }

    /**
    Gets the name of the person
    @return name the person's name
    */
    public String getName()
    {
    return name;
    }

    /**
    Gets the height of the person
    @return height the person's height
    */
    public double getHeight()
    {
    return height;
    }
    }.

    © BrainMass Inc. brainmass.com March 5, 2021, 1:15 am ad1c9bdddf
    https://brainmass.com/computer-science/java/java-object-oriented-programming-using-classes-interfaces-574847

    Solution Summary

    The solution modifies existing code to implement a dataset that can process person objects to find average height, tallest person, etc. in Java. Attached as zip file.

    $2.49

    ADVERTISEMENT