In: Computer Science
public class Classroom
{
// fields
private String roomNumber;
private String buildingName;
private int capacity;
/**
* Constructor for objects of class Classroom
*/
public Classroom()
{
this.capacity = 0;
}
  
/**
* Constructor for objects of class Classroom
*
* @param rN the room number
* @param bN the building name
* @param c the room capacity
*/
public Classroom(String rN, String bN, int c)
{
setRoomNumber(rN);
setBuildingName(bN);
setCapacity(c);
}
  
/**
* Mutator method (setter) for room number.
*
* @param rN a new room number
*/
public void setRoomNumber(String rN)
{
this.roomNumber = rN;
}
  
/**
* Mutator method (setter) for building name.
*
* @param bN a new building name
*/
public void setBuildingName(String bN)
{
this.buildingName = bN;
}
  
/**
* Mutator method (setter) for capacity.
*
* @param c a new capacity
*/
public void setCapacity(int c)
{
this.capacity = c;
}
  
/**
* Accessor method (getter) for room number.
*
* @return the room number
*/
public String getRoomNumber()
{
return this.roomNumber;
}
  
/**
* Accessor method (getter) for building name.
*
* @return the building name
*/
public String getBuildingName()
{
return this.buildingName;
}
  
/**
* Accessor method (getter) for capacity.
*
* @return the capacity
*/
public int getCapacity()
{
return this.capacity;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CODE TO COPY
public class Classroom implements Comparable<Classroom>
{
// fields
   private String roomNumber;
   private String buildingName;
   private int capacity;
   /**
   * Constructor for objects of class Classroom
   */
   public Classroom() {
       this.capacity = 0;
   }
   /**
   * Constructor for objects of class Classroom
   *
   * @param rN the room number
   * @param bN the building name
   * @param c the room capacity
   */
   public Classroom(String rN, String bN, int c) {
       setRoomNumber(rN);
       setBuildingName(bN);
       setCapacity(c);
   }
   /**
   * Mutator method (setter) for room number.
   *
   * @param rN a new room number
   */
   public void setRoomNumber(String rN) {
       this.roomNumber = rN;
   }
   /**
   * Mutator method (setter) for building name.
   *
   * @param bN a new building name
   */
   public void setBuildingName(String bN) {
       this.buildingName = bN;
   }
   /**
   * Mutator method (setter) for capacity.
   *
   * @param c a new capacity
   */
   public void setCapacity(int c) {
       this.capacity = c;
   }
   /**
   * Accessor method (getter) for room number.
   *
   * @return the room number
   */
   public String getRoomNumber() {
       return this.roomNumber;
   }
   /**
   * Accessor method (getter) for building name.
   *
   * @return the building name
   */
   public String getBuildingName() {
       return this.buildingName;
   }
   /**
   * Accessor method (getter) for capacity.
   *
   * @return the capacity
   */
   public int getCapacity() {
       return this.capacity;
   }
   @Override
   public int compareTo(Classroom o) {
       return
Integer.compare(getCapacity(), o.getCapacity());
   }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TestComparable.java
import java.util.ArrayList;
import java.util.Comparator;
public class TestComparable {
  
   public static void main(String[] args) {
       ArrayList<Classroom> ar = new
ArrayList<Classroom>();
       ar.add(new Classroom("r1", "b1",
10));
       ar.add(new Classroom("r2", "b2",
40));
       ar.add(new Classroom("r3", "b3",
50));
       ar.add(new Classroom("r4", "b4",
20));
       ar.add(new Classroom("r5", "b5",
5));
      
       System.out.println("Before
Sorting");
       for (int j = 0; j < ar.size();
j++) {
          
System.out.println(ar.get(j).getRoomNumber()+" :
"+ar.get(j).getCapacity());
       }
      
      
ar.sort(Comparator.comparing(Classroom::getCapacity));
      
       System.out.println("\nAfter
Sorting");
       for (int j = 0; j < ar.size();
j++) {
          
System.out.println(ar.get(j).getRoomNumber()+" :
"+ar.get(j).getCapacity());
       }
   }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
EXPLANATION
CODE TO COPY
public class Classroom implements Comparable<Classroom> { // IMPLEMENTING COMPARABLE INTERFACE
// SINCE WE ARE IMPLEMENTING THIS METHOD WE HAVE TO ADD A
COMPARETO METHOD
// fields
   private String roomNumber;
   private String buildingName;
   private int capacity;
   /**
   * Constructor for objects of class Classroom
   */
   public Classroom() {
       this.capacity = 0;
   }
   /**
   * Constructor for objects of class Classroom
   *
   * @param rN the room number
   * @param bN the building name
   * @param c the room capacity
   */
   public Classroom(String rN, String bN, int c) {
       setRoomNumber(rN);
       setBuildingName(bN);
       setCapacity(c);
   }
   /**
   * Mutator method (setter) for room number.
   *
   * @param rN a new room number
   */
   public void setRoomNumber(String rN) {
       this.roomNumber = rN;
   }
   /**
   * Mutator method (setter) for building name.
   *
   * @param bN a new building name
   */
   public void setBuildingName(String bN) {
       this.buildingName = bN;
   }
   /**
   * Mutator method (setter) for capacity.
   *
   * @param c a new capacity
   */
   public void setCapacity(int c) {
       this.capacity = c;
   }
   /**
   * Accessor method (getter) for room number.
   *
   * @return the room number
   */
   public String getRoomNumber() {
       return this.roomNumber;
   }
   /**
   * Accessor method (getter) for building name.
   *
   * @return the building name
   */
   public String getBuildingName() {
       return this.buildingName;
   }
   /**
   * Accessor method (getter) for capacity.
   *
   * @return the capacity
   */
   public int getCapacity() {
       return this.capacity;
   }
   @Override
   public int compareTo(Classroom o) { // COMPARE TO
METHOD
       return
Integer.compare(getCapacity(), o.getCapacity()); // COMPARING THE
INTEGERS AND RETURNING -1 IF LESS 0 IF EQUAL AND 1 IF GREATER THAN
THE OBJECT CALLED
   }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TestComparable.java
import java.util.ArrayList; //FOR CREATING ARRAYLIST
import java.util.Comparator; // FOR CREATING COMPARATOR
public class TestComparable {
  
   public static void main(String[] args) {
       ArrayList<Classroom> ar = new
ArrayList<Classroom>(); //CREATING A ARRAYLIST OF
CLASSROOM
       ar.add(new Classroom("r1", "b1",
10)); // ADDING ALL THE CLASSROOM OBJECTS
       ar.add(new Classroom("r2", "b2",
40));
       ar.add(new Classroom("r3", "b3",
50));
       ar.add(new Classroom("r4", "b4",
20));
       ar.add(new Classroom("r5", "b5",
5));
      
       System.out.println("Before
Sorting"); //PRINTING STATEMENT
       for (int j = 0; j < ar.size();
j++) { // FOR LOOP TO PRINT THE ROOM NUMBER AND CAPACITY
          
System.out.println(ar.get(j).getRoomNumber()+" :
"+ar.get(j).getCapacity());
       }
      
      
ar.sort(Comparator.comparing(Classroom::getCapacity)); // SORTING
THE ARRAYLIST BASED ON CAPACITY
      
       System.out.println("\nAfter
Sorting"); //PRINTINTG STATEMENT
       for (int j = 0; j < ar.size();
j++) {
          
System.out.println(ar.get(j).getRoomNumber()+" :
"+ar.get(j).getCapacity()); //PRINTING ROOM NUMBER AND
CAPACITY
       }
   }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IF YOU HAVE ANY PROBLEM REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL DEFINETLY HELP YOU
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////