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...
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...
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...
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...
For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;   ...
public class GroceryShopping {    //declared variable    private String vegetableName;    private String fruitName;    private double vegetablePrice;    private double fruitPrice;    private double vegetableOrdered;    private double fruitOrdered;       //declared constants    public static final double SERVICE_RATE =0.035;    public static final double DELIVERY_FEE=5;       public GroceryShopping( String vegetableName, String fruitName, double vegetablePrice, double fruitPrice)    {        this.vegetableName = vegetableName;        this.fruitName = fruitName;        this.vegetablePrice = vegetablePrice;        this.fruitPrice...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT