Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use an array of Circle and Rectangle objects to test in attached Sort method. 1. Circle and Rectangle class must implement Comparable. 2. You must

Use an array of Circle and Rectangle objects to test in attached Sort method.

1. Circle and Rectangle class must implement Comparable.

2. You must handle the error exception by try catch for invalid radius in Circle object and width and height in Rectangle object.

3. Test in Project3.java with list of Circles and list of Rectangle.

GenericSort.java public class GenericSort { public static void main(String[] args) { // Create an Integer array Integer[] intArray = {new Integer(2), new Integer(4), new Integer(3)}; // Create a Double array Double[] doubleArray = {new Double(3.4), new Double(1.3), new Double(-22.1)}; // Create a Character array Character[] charArray = {new Character('a'), new Character('J'), new Character('r')}; // Create a String array String[] stringArray = {"Tom", "Susan", "Kim"}; // Sort the arrays sort(intArray); sort(doubleArray); sort(charArray); sort(stringArray); // Display the sorted arrays System.out.print("Sorted Integer objects: "); printList(intArray); System.out.print("Sorted Double objects: "); printList(doubleArray); System.out.print("Sorted Character objects: "); printList(charArray); System.out.print("Sorted String objects: "); printList(stringArray); } /** Sort an array of comparable objects */ public static > void sort(E[] list) { E currentMin; int currentMinIndex; for (int i = 0; i < list.length - 1; i++) { // Find the minimum in the list[i..list.length-1] currentMin = list[i]; currentMinIndex = i; for (int j = i + 1; j < list.length; j++) { if (currentMin.compareTo(list[j]) > 0) { currentMin = list[j]; currentMinIndex = j; } } // Swap list[i] with list[currentMinIndex] if necessary; if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin; } } } /** Print an array of objects */ public static  void printList(E[] list) { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } }

RectangleFromSimpleGeometricObject.java

public class RectangleFromSimpleGeometricObject extends SimpleGeometricObject { private double width; private double height; public RectangleFromSimpleGeometricObject() { } public RectangleFromSimpleGeometricObject( double width, double height) { this.width = width; this.height = height; } public RectangleFromSimpleGeometricObject( double width, double height, String color, boolean filled) { this.width = width; this.height = height; setColor(color); setFilled(filled); } /** Return width */ public double getWidth() { return width; } /** Set a new width */ public void setWidth(double width) { this.width = width; } /** Return height */ public double getHeight() { return height; } /** Set a new height */ public void setHeight(double height) { this.height = height; } /** Return area */ public double getArea() { return width * height; } /** Return perimeter */ public double getPerimeter() { return 2 * (width + height); } }

Circle.java

public class Circle extends GeometricObject

implements Cloneable, Comparable {

private double radius;

public Circle() {

}

public Circle(double radius) {

if(radius > 0)

this.radius = radius;

else{

throw new LessThanZeroException();

}

}

/** Return radius */

public double getRadius() {

return radius;

}

/** Set a new radius */

public void setRadius(double radius) {

if ( radius > 0)

this.radius = radius;

else {

throw new LessThanZeroException();

}

}

@Override /** Return area */

public double getArea() {

return radius * radius * Math.PI;

}

/** Return diameter */

public double getDiameter() {

return 2 * radius;

}

@Override /** Return perimeter */

public double getPerimeter() {

return 2 * radius * Math.PI;

}

/* Print the circle info */

public void printCircle() {

System.out.println("The circle is created " + getDateCreated() +

" and the radius is " + radius);

}

@Override

public String toString() {

return super.toString() + " and the radius is " + radius;

}

@Override

public int compareTo(Circle o) {

// TODO Auto-generated method stub

if (getRadius() >o.getRadius())

return 1;

else if (getRadius()

return -1;

else

return 0;

}

@Override

public Object clone() {

Object o = null;

try {

o = super.clone();

} catch (CloneNotSupportedException ex) {

ex.printStackTrace();

}

return o;

}

/*

public Circle clone() {

Circle o = new Circle();

try {

o.setRadius(this.getRadius());

} catch (CloneNotSupportedException ex) {

ex.printStackTrace();

}

return o;

}

*/

}

project3.java

public class Project3 {

public static void main(String[] args) {

Circle [] circles = {

new Circle(3),

new Circle(13.24),

new Circle (1.5),

new Circle(13.22)

};

System.out.println("Before Sorting:");

for (Circle circle: circles) {

System.out.print(circle + " ");

System.out.println();

}

// implement the nextCircle and try to catch the exception

try {

Circle c = new Circle(-1);

} catch (Exception e) {

//TODO: handle exception

System.out.println("Value of the radius is negative please change the value");

// we can also use the printstackTrace.

e.printStackTrace();

}

java.util.Arrays.sort(circles);

System.out.println("After Sorting:");

for (Circle circle: circles) {

System.out.print(circle + " ");

System.out.println();

}

Circle circle = new Circle(4);

Circle circle1 = circle;

Circle circle2 = (Circle) circle.clone();

System.out.println( "Original circle " +circle);

System.out.println("Clone circle " +circle2);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

More Books

Students also viewed these Databases questions