Please help in Java Alter the circle class provided here to

Please help in Java:

Alter the circle class provided here to include a compareTo method. Make any other changes you need to the class, but DO NOT change the toString method (or your output will be wrong). Add code to the main method as specified.

public static class Circle implements Comparable<Circle>
{
//attribute
  
private double radius;
  
//constructors
public Circle()
{radius=0.0;
}
public Circle(double r)
{radius=r;
}
//accessors
public double getRadius()
{ return radius;
}
//mutators
public void setRadius(double r)
{ radius = r;
}
//methods
public double circumference()
{ return 2*Math.PI*radius;
}
public double area()
{ return Math.PI*radius* radius;
}
  
public String toString()
{
return \"Circle of radius \" + radius;
}
  
public int compareTo(Circle c)
{ //write your code here
if (radius < c.radius)
return -1;
if (radius > c.radius)
return 1;
else {
return 0;
}
  
}
  
  

}
  
  
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
ArrayList<Circle>myC = new ArrayList<Circle>();
Circle c1;
//System.out.println(\"Number of circles: \");
int howMany = keyboard.nextInt();
//loop howMany times
for (int i=0; i<howMany; i++ ){
double c=keyboard.nextDouble();
Circle c2= new Circle();
//input a double, create a circle, add it to the ArrayList
myC.add(c2);
}
  
for (int i=0; i<myC.size(); i++){
//sort the ArrayList
Collections.sort(myC);
//output the ArrayList
System.out.println(myC.get(i));
}
  
}
}

Solution

Hi,

Your code worked well, I made just 3 changes:

1) Imported the classes which were being used in code.
2) Passed the radius entered by user, to the circle object\'s constructor.
3) There was an extra curly brace (}), which I deleted.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Circle implements Comparable<Circle> {
   // attribute

   private double radius;

   // constructors
   public Circle() {
       radius = 0.0;
   }

   public Circle(double r) {
       radius = r;
   }

   // accessors
   public double getRadius() {
       return radius;
   }

   // mutators
   public void setRadius(double r) {
       radius = r;
   }

   // methods
   public double circumference() {
       return 2 * Math.PI * radius;
   }

   public double area() {
       return Math.PI * radius * radius;
   }

   public String toString() {
       return \"Circle of radius \" + radius;
   }

   public int compareTo(Circle c) {
       //This compareTo method will be useful in sorting circles in ascenting order.
       if (radius < c.radius)
           return -1;
       if (radius > c.radius)
           return 1;
       else {
           return 0;
       }

   }

   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in);
       ArrayList<Circle> myC = new ArrayList<Circle>();
       Circle c1;
       System.out.println(\"Number of circles: \");
       int howMany = keyboard.nextInt();
       // loop howMany times
       for (int i = 0; i < howMany; i++) {
           System.out.println(\"input the radius for circle: \");
           double c = keyboard.nextDouble();
           Circle c2 = new Circle(c);
           // The radius value entered by user is passed as a parameter to the
           // constructor
           myC.add(c2);
       }

       for (int i = 0; i < myC.size(); i++) {
           // sort the ArrayList
           Collections.sort(myC);
           // output the ArrayList in sorted order.
           System.out.println(myC.get(i));
       }

   }
}


Sample run:

Number of circles:
3
input the radius for circle:
5
input the radius for circle:
7.8
input the radius for circle:
4.3
Circle of radius 4.3
Circle of radius 5.0
Circle of radius 7.8

Please help in Java: Alter the circle class provided here to include a compareTo method. Make any other changes you need to the class, but DO NOT change the toS
Please help in Java: Alter the circle class provided here to include a compareTo method. Make any other changes you need to the class, but DO NOT change the toS
Please help in Java: Alter the circle class provided here to include a compareTo method. Make any other changes you need to the class, but DO NOT change the toS

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site