Question

In: Computer Science

(JAVA): The Collection ADT 1. We have a Circle class that includes radius attribute of type...

(JAVA): The Collection ADT

1. We have a Circle class that includes radius attribute of type int, set by the constructor. Write a compareTo method for this class so that circle objects are ordered on the basis of their circumference (? = 2??).

2. We have a Person class that includes name and age attributes of type String and int respectively, both set by the constructor. Write a compareTo method for this class so that person objects are ordered on the basis of their name.

Solutions

Expert Solution


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

   /**
   * @param aRadius
   */
   public Circle(int aRadius) {
       super();
       radius = aRadius;
   }

   /**
   * @return the radius
   */
   public int getRadius() {
       return radius;
   }

   /**
   * @param aRadius the radius to set
   */
   public void setRadius(int aRadius) {
       radius = aRadius;
   }

   @Override
   public int compareTo(Circle c) {
       //comparing the circumference of the both the objects
       return new Double(getCircumference()).compareTo(c.getCircumference());
   }
   //returns the circumference of the circle using 2 PI R formula
   public double getCircumference() {
       return 2 * Math.PI * radius;
   }

}

Answer 2:

public class Person implements Comparable<Person>{
   private String name;
   private int age;
   public Person(String aName, int aAge) {
       super();
       name = aName;
       age = aAge;
   }
   /**
   * @return the name
   */
   public String getName() {
       return name;
   }
   /**
   * @return the age
   */
   public int getAge() {
       return age;
   }
   /**
   * @param aName the name to set
   */
   public void setName(String aName) {
       name = aName;
   }
   /**
   * @param aAge the age to set
   */
   public void setAge(int aAge) {
       age = aAge;
   }
   @Override
   public int compareTo(Person p) {
       //comparing using the name so that Person objects will ordered based on name
       return getName().compareTo(p.getName());
   }
  
}
Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a method void addCoin(String coinName). Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime]. Write a method reverse that reverses the sequence of coins in a purse. Implement a TestPurse class with a main method in it. It will use the toString method to...
Suppose we know that the radius of a circle is increasing at a rate of 10...
Suppose we know that the radius of a circle is increasing at a rate of 10 feet each second, and we want to know how fast the area of the circle is increasing when the radius is 5 feet. What can we do?
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
If a circle C with radius 1 rolls along the outside of the circle x2 +...
If a circle C with radius 1 rolls along the outside of the circle x2 + y2 = 36, a fixed point P on C traces out a curve called an epicycloid, with parametric equations x = 7 cos(t) − cos(7t), y = 7 sin(t) − sin(7t). Graph the epicycloid. Find the area it encloses.
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels,...
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels, filled with a random color at a random location on the screen. When you click the circle, it disappears and a new random color circle is displayed at another random location (see display below). After twenty circles are clicked, display the time spent in the pane. To detect whether a point is inside the circle, use the contains method defined in the Node class....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT