Explore BrainMass

Another Measurer in Java

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

Using a different Measurer object, process a set of Rectangle objects to find the rectangle with the largest perimeter.

You need to supply the following class in your solution:

Use the following class as your tester class:
import java.awt.Rectangle;

This program tests the measurement of rectangles by perimeter.
public class PerimeterTester
public static void main(String[] args)
DataSet data = . . .;

data.add(new Rectangle(5, 10, 20, 30));
data.add(new Rectangle(10, 20, 30, 40));
data.add(new Rectangle(20, 30, 5, 10));

double avg = . . .;
Rectangle max = . . .;

System.out.println("Average perimeter: " + avg);
System.out.println("Expected: ");
System.out.println("Largest perimeter: " + max);
System.out.println("Expected: ");

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;

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;

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);

© BrainMass Inc. brainmass.com October 17, 2018, 12:22 pm ad1c9bdddf

Solution Summary

The solution uses a measurer to find several rectangles' maximum perimeter and average perimeter.

Similar Posting

A Java Length class

Please see the attached file.

? Construct and use Objects
? Design the public interface of a class
? Determine the instance variables necessary for a class
? Implement stubs with comments
? Implement constructors
? Implement simple methods using conditionals
? Access instance fields and local variables
Hand-in Requirements
The project folder should include the following files:
? Length.xml
? Length.java
? LengthTest.java
? the public interface as a text or Word file
Write a program to construct and manipulate Length objects that can handle any of the following units: meters, inches, feet, yards, miles. In addition to modifying the methods, you will include methods for returning the number of meters, for adding two Length objects, and testing whether two Length objects are approximately equal.
Writing the New and Improved Length Class
The following constants should be included in the Length class before the instance fields. These constants should be used by your methods. Do not use the numbers in your methods instead of the named constants.
public static final double INCHES_PER_YARD = 36;
public static final double FEET_PER_YARD = 3;
public static final double METERS_PER_YARD = 0.9144;
public static final double YARDS_PER_MILE = 1760;
public static final double EPSILON = 0.000001;

Instance fields:
myNumber - a double value representing the number of the measurement
myUnit - a String value representing the unit of the measurement
/* The constructor has two parameters: */
/* the number and the unit of the measurement. */
public Length(double number, String unit)

Methods:Three methods will be added, and the behavior of some methods will be changed.
The following methods should be added. The comments contain some hints.

/* getMeters returns the length in meters.
This should be implemented to calculate the appropriate conversion
based on the value of myUnit, e.g.,

if ( myUnit.equals("yards") )
meters = myNumber * METERS_PER_YARD;

If myUnit cannot be recognized, assume the unit is meters.
public double getMeters( )

/* add returns a Length that is the sum of two Lengths, e.g.,
if length1 and length2 are two Length objects, then
length1.add(length2) will return a new Length object.

This can be implemented by using the getMeters method,
e.g., this.getMeters() + other.getMeters(), then returning
new Length(sum, "meters")
public Length add(Length other)

/* aboutEqual returns true if the two lengths are within EPSILON
meters of each other.

First, get this.getMeters() and other.getMeters(). Then, return
true if they are close to each other (within EPSILON).
public boolean aboutEqual(Length other)

The getInches, getFeet, getYards, and getMiles methods should be modified. First get the value of the getMeters method, e.g., this.getMeters(), then convert the number of meters to the desired unit using the named constants, and finally return that value.
Testing your Length class:
1. Ask the user to enter a number as a double from the keyboard. Store the response in number2.
2. Ask the user to enter a unit as a String from the keyboard. Store the response in unit2.
3. If unit2 is not "meters", "inches", "feet", "yards", or "miles", then print a warning message and set unit2 to "meters".
4. Instantiate a Length object named length2 using the input from above, and print length2 using the toString method. Be sure to use a descriptive label.
5. Print the values from calling length2 with the getMeters, getInches, getFeet, getYards, and getMiles methods. Be sure to use descriptive labels.
6. Ask the user to enter another number and another unit from the keyboard. Store the responses in number3 and unit3.
7. If unit3 is not "meters", "inches", "feet", "yards", or "miles", then print a warning message and set unit2 to "meters".
8. Instantiate a Length object named length3 using the input from above, and print length3 using the toString method. Be sure to use a descriptive label.
9. Determine whether length2 is about equal to length3, and assign the result to a boolean variable. Print different messages depending on the value of the variable.
10. Assign the result of adding length2 and length3 to length4, and print length4 using the toString method. Be sure to use a descriptive label.

View Full Posting Details