Question

In: Computer Science

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

Put the ideas into practice by extending the original Room class to create an AcademicRoom.java class.

  1. The AcademicRoom.java should store information about the building name, room number, capacity, academic department associated with the room (CPSC, MATH, BUAD, ...) and also whether or not the room contains a projector. Make sure to utilize inheritance (only add new fields to AcademicRoom).
  2. Include a constructor for the AcademicRoom class that takes advantage of the reserved word super. Also include appropriate getter/setter methods for the AcademicRoom.
  3. Add James Farmer B6 to your list as well as two or three other classrooms of your choosing.
  4. Implement a test program (TestAcademicRoom.java) with a main() that creates an ArrayList of AcademicRooms. After the list is created, allow the user to: (1) add another room to the list, and (2) search for rooms. The program should not add the room if it already exists in the list. The user should search by inputting a building name and optionally a room number. If a room number is not provided, the program should print all rooms in that building. If matches are found in the list, print all the data members associated with the AcademicRoom(s).

Solutions

Expert Solution

ANSWER:

CONSIDERING THE CONDITIONS AND REQUIREMENTS FROM THE QUESTION.

CODE :

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

NOTE : PLEASE UPVOTE ITS VERY MUCH NECESSARY FOR ME LOT. PLZZZ....


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...
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”);...
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 +...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
This lab uses a Student class with the following fields: private final String firstName; private final...
This lab uses a Student class with the following fields: private final String firstName; private final String lastName; private final String major; private final int zipcode; private final String studentID; private final double gpa; A TestData class has been provided that contains a createStudents() method that returns an array of populated Student objects. Assignmen The Main method prints the list of Students sorted by last name. It uses the Arrays.sort() method and an anonymous Comparator object to sort the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT