1Objective Implement javautilComparator to determine the ord
(1)Objective: Implement java.util.Comparator to determine the order of arbitrary objects. (1 points)
Write a java program to determine the order of two shape objects by comparing their area.
For example: determine the order of
(a)A 20x5 rectangular.
(b)A circle with radius 6.
Download Circle.java, Rectangular.java and Shape.java
Codes:
//********************************************************************
// Circle.java
//
//********************************************************************
import java.text.*;
public class Circle extends Shape
{
private int radius, x, y;
//-----------------------------------------------------------------
// Constructor: Sets up this circle with the specified values.
//-----------------------------------------------------------------
public Circle (int size)
{
radius = size;
}
//-----------------------------------------------------------------
// Diameter mutator.
//-----------------------------------------------------------------
public void setRadius (int size)
{
radius = size;
}
//-----------------------------------------------------------------
// Diameter accessor.
//-----------------------------------------------------------------
public int getRadius ()
{
return radius;
}
//---------------------------------------------------------------------------
// Returns the calculated value of the area.
//---------------------------------------------------------------------------
public double computeArea()
{
return 3.1416*(radius)*(radius);
}
//---------------------------------------------------------------------------
// Returns the calculated value of the perimeter.
//---------------------------------------------------------------------------
public double computePerimeter()
{
return 3.1416 * 2 * radius;
}
}
//******************************************************************************
// Rectangle.java Author: Lewis/Loftus
//
// Solution to Programming Project 9.11
//******************************************************************************
import java.text.*;
public class Rectangle extends Shape
{
protected double width;
protected double length;
protected static DecimalFormat form = new DecimalFormat(\"0.##\");
//---------------------------------------------------------------------------
// Sets up the rectangle by entering its width and length.
//---------------------------------------------------------------------------
public Rectangle(double wid, double len)
{
width = wid;
length = len;
}
//---------------------------------------------------------------------------
// Returns the double value of the width.
//---------------------------------------------------------------------------
public double getWidth()
{
return width;
}
//---------------------------------------------------------------------------
// Returns the double value of the length.
//---------------------------------------------------------------------------
public double getLength()
{
return length;
}
//---------------------------------------------------------------------------
// Returns the calculated value of the area.
//---------------------------------------------------------------------------
public double computeArea()
{
return length * width;
}
//---------------------------------------------------------------------------
// Returns the calculated value of the perimeter.
//---------------------------------------------------------------------------
public double computePerimeter()
{
return 2 * (length + width);
}
//---------------------------------------------------------------------------
// Returns pertinent information about the rectangle.
//---------------------------------------------------------------------------
public String toString()
{
return \"Rectangle: width is \" + form.format(width) +
\", length is \" + form.format(length) +
\"\ perimeter is \" + form.format(computePerimeter()) +
\", area is \" + form.format(computeArea());
}
}
//******************************************************************************
// Shape.java
//
//******************************************************************************
public abstract class Shape
{
abstract public double computeArea();
abstract public double computePerimeter();
}
Solution
//******************************************************************************
//Shape.java
//
//******************************************************************************
public abstract class Shape implements Comparable<Shape> {
abstract public double computeArea();
abstract public double computePerimeter();
/*
* two shape objects by comparing their area
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Shape o) {
// TODO Auto-generated method stub
if (this.computeArea() > o.computeArea())
return 1;
else
return 0;
}
}
//********************************************************************
// Circle.java
//
//********************************************************************
import java.text.*;
public class Circle extends Shape {
private int radius, x, y;
protected static DecimalFormat form = new DecimalFormat(\"0.##\");
// -----------------------------------------------------------------
// Constructor: Sets up this circle with the specified values.
// -----------------------------------------------------------------
public Circle(int size) {
radius = size;
}
// -----------------------------------------------------------------
// Diameter mutator.
// -----------------------------------------------------------------
public void setRadius(int size) {
radius = size;
}
// -----------------------------------------------------------------
// Diameter accessor.
// -----------------------------------------------------------------
public int getRadius() {
return radius;
}
// ---------------------------------------------------------------------------
// Returns the calculated value of the area.
// ---------------------------------------------------------------------------
public double computeArea() {
return 3.1416 * (radius) * (radius);
}
// ---------------------------------------------------------------------------
// Returns the calculated value of the perimeter.
// ---------------------------------------------------------------------------
public double computePerimeter() {
return 3.1416 * 2 * radius;
}
public String toString() {
return \"\ Circle: radius is \" + radius + \", \ perimeter is \"
+ form.format(computePerimeter()) + \", area is \"
+ form.format(computeArea());
}
}
//******************************************************************************
//Rectangle.java Author: Lewis/Loftus
//
//Solution to Programming Project 9.11
//******************************************************************************
import java.text.*;
public class Rectangle extends Shape {
protected double width;
protected double length;
protected static DecimalFormat form = new DecimalFormat(\"0.##\");
// ---------------------------------------------------------------------------
// Sets up the rectangle by entering its width and length.
// ---------------------------------------------------------------------------
public Rectangle(double wid, double len) {
width = wid;
length = len;
}
// ---------------------------------------------------------------------------
// Returns the double value of the width.
// ---------------------------------------------------------------------------
public double getWidth() {
return width;
}
// ---------------------------------------------------------------------------
// Returns the double value of the length.
// ---------------------------------------------------------------------------
public double getLength() {
return length;
}
// ---------------------------------------------------------------------------
// Returns the calculated value of the area.
// ---------------------------------------------------------------------------
public double computeArea() {
return length * width;
}
// ---------------------------------------------------------------------------
// Returns the calculated value of the perimeter.
// ---------------------------------------------------------------------------
public double computePerimeter() {
return 2 * (length + width);
}
// ---------------------------------------------------------------------------
// Returns pertinent information about the rectangle.
// ---------------------------------------------------------------------------
public String toString() {
return \"\ Rectangle: width is \" + form.format(width) + \", length is \"
+ form.format(length) + \"\ perimeter is \"
+ form.format(computePerimeter()) + \", area is \"
+ form.format(computeArea());
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestShape {
/**
* @param args
*/
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Circle(6));
shapes.add(new Rectangle(20, 5));
Collections.sort(shapes);
// sorted according to two shape objects by comparing their area
System.out.println(shapes);
}
}
OUTPUT:
[Rectangle: width is 20, length is 5
perimeter is 50, area is 100,
Circle: radius is 6,
perimeter is 37.7, area is 113.1]





