#Testing Please test my code and see if it meet the follwing needs: The class should implement the Comparable interface. Circle one is less than Circle two if the radius of Circle one is less than the radius of Circle two. The two Circles are equal if they have the same radius. Circle one is larger than Circle two if its radius is larger. If circle One.compareTo circle Two <> 0 The Test Class displays a menu that allows the user to: Enter a Circle (the user only needs to enter the radius). Print all Circles (print the toString for each Circle in the ArrayList). Quit To adda a circle Cases: The ArrayList is empty The new circle is less than the first circle, add it at the beginning. The circle is greater than the last circle, add it at the end The new circle belongs somewhere in the middle.   import java.lang.Math;public class Circle implements Comparable<Circle> {private double radius;public Circle(double radius) {this.radius = radius;}public double findArea() {return Math.PI * Math.pow(this.radius, 2);}public double findCircumference() {return 2 * Math.PI * this.radius;}@Overridepublic boolean equals(Object obj) {if (this == obj) return true;if (obj == null || getClass() != obj.getClass()) return false;Circle circle = (Circle) obj;return Double.compare(circle.radius, radius) == 0;}@Overridepublic String toString() {return "Radius: " + this.radius + " Area: " + this.findArea() + " Circumference: " + this.findCircumference();}@Overridepublic int compareTo(Circle c) {return Double.compare(this.radius, c.radius);}}     #Test Class import java.util.ArrayList;import java.util.Scanner;public class CircleTest {private ArrayList<Circle> circles = new ArrayList<>();public void start() {Scanner scanner = new Scanner(System.in);int choice;do {System.out.println("\n1. Enter a Circle\n2. Print all Circles\n3. Quit");choice = scanner.nextInt();switch (choice) {case 1:System.out.println("Enter the radius of the Circle:");double radius = scanner.nextDouble();Circle circle = new Circle(radius);addCircle(circle);break;case 2:printCircles();break;case 3:System.out.println("Quitting...");break;default:System.out.println("Invalid choice. Please try again.");}} while (choice != 3);scanner.close();}private void addCircle(Circle circle) {if (circles.isEmpty()) {circles.add(circle);} else if (circle.compareTo(circles.get(0)) < 0) {circles.add(0, circle);} else if (circle.compareTo(circles.get(circles.size() - 1)) > 0) {circles.add(circle);} else {for (int i = 0; i < circles.size(); i++) {if (circle.compareTo(circles.get(i)) < 0) {circles.add(i, circle);break;}}}}private void printCircles() {for (Circle circle : circles) {System.out.println(circle);}}public static void main(String[] args) {CircleTest test = new CircleTest();test.start();}}

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

#Testing

Please test my code and see if it meet the follwing needs:

The class should implement the Comparable interface. Circle one is less than Circle two if the radius of Circle one is less than the radius of Circle two. The two Circles are equal if they have the same radius. Circle one is larger than Circle two if its radius is larger. If circle One.compareTo circle Two <> 0

The Test Class displays a menu that allows the user to:

  1. Enter a Circle (the user only needs to enter the radius).
  2. Print all Circles (print the toString for each Circle in the ArrayList).
  3. Quit

To adda a circle

Cases:

  • The ArrayList is empty
  • The new circle is less than the first circle, add it at the beginning.
  • The circle is greater than the last circle, add it at the end
  • The new circle belongs somewhere in the middle.
 
import java.lang.Math;

public class Circle implements Comparable<Circle> {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

public double findArea() {
return Math.PI * Math.pow(this.radius, 2);
}

public double findCircumference() {
return 2 * Math.PI * this.radius;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Circle circle = (Circle) obj;
return Double.compare(circle.radius, radius) == 0;
}

@Override
public String toString() {
return "Radius: " + this.radius + " Area: " + this.findArea() + " Circumference: " + this.findCircumference();
}

@Override
public int compareTo(Circle c) {
return Double.compare(this.radius, c.radius);
}
}

 
 
#Test Class
import java.util.ArrayList;
import java.util.Scanner;

public class CircleTest {

private ArrayList<Circle> circles = new ArrayList<>();

public void start() {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n1. Enter a Circle\n2. Print all Circles\n3. Quit");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the radius of the Circle:");
double radius = scanner.nextDouble();
Circle circle = new Circle(radius);
addCircle(circle);
break;
case 2:
printCircles();
break;
case 3:
System.out.println("Quitting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);
scanner.close();
}

private void addCircle(Circle circle) {
if (circles.isEmpty()) {
circles.add(circle);
} else if (circle.compareTo(circles.get(0)) < 0) {
circles.add(0, circle);
} else if (circle.compareTo(circles.get(circles.size() - 1)) > 0) {
circles.add(circle);
} else {
for (int i = 0; i < circles.size(); i++) {
if (circle.compareTo(circles.get(i)) < 0) {
circles.add(i, circle);
break;
}
}
}
}

private void printCircles() {
for (Circle circle : circles) {
System.out.println(circle);
}
}

public static void main(String[] args) {
CircleTest test = new CircleTest();
test.start();
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education