Purchase Solution

Java Object Oriented Programming using Classes and Interfaces

Not what you're looking for?

Ask Custom Question

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;
}
}.

Purchase this Solution

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.

Purchase this Solution


Free BrainMass Quizzes
Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

C# variables and classes

This quiz contains questions about C# classes and variables.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.