Question

In: Computer Science

public class Classroom { // fields private String roomNumber; private String buildingName; private int capacity; /**...

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;
}
}

  1. Modify this class so that it implements the Comparable interface. This will involve adding a method called compareTo() to your Room class and making some other changes to the class definition.
  2. Now write a main program (in a new class called TestComparable) that creates an ArrayList of Rooms. Add about 5 Rooms to the list so that they're randomly ordered. Print the list in its current order. Then call the sort() method to sort the Rooms based on capacity. Print the list in the new sorted order.

Solutions

Expert Solution

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Related Solutions

Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class Book{     public String title;     public String author;     public int year;    ...
public class Book{     public String title;     public String author;     public int year;     public String publisher;     public double cost;            public Book(String title,String author,int year,String publisher,double cost){        this.title=title;         this.author=author;         this.year=year;         this.publisher=publisher;         this.cost=cost;     }     public String getTitle(){         return title;     }         public String getAuthor(){         return author;     }     public int getYear(){         return year;     }     public String getPublisher(){         return publisher;...
Convert the Queue to Generic implementation: public class MyQueueImpl implements MyQueue {    private int capacity;...
Convert the Queue to Generic implementation: public class MyQueueImpl implements MyQueue {    private int capacity;    private int front;    private int rear;    private int[] arr;    public MyQueueImpl(int capacity){        this.capacity = capacity;        this.front = 0;        this.rear = -1;        this.arr = new int[this.capacity];    }    @Override    public boolean enQueue(int v) {                  if(this.rear == this.capacity - 1) {                //Perform shift   ...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT