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

import java.util.ArrayList;

public class AcademicRoom extends Room {
    String AcademicDepartment;
    Boolean projector;

    public AcademicRoom(String buildingName, String roomNo, int capacity, String department, Boolean projector) {
        super(roomNo, buildingName, capacity);
        setAcademicDepartment(department);
        setProjector(projector);
    }

    public void setAcademicDepartment(String academicDepartment) {
        this.AcademicDepartment = academicDepartment;
    }

    public void setProjector(Boolean projector) {
        this.projector = projector;
    }

    public String getAcademicDepartment() {
        return this.AcademicDepartment;
    }

    public Boolean getProjector() {
        return this.projector;
    }

    @Override
    public String toString() {
        return "Room No:"  + getRoomNumber() + ", Building Name: " + getBuildingName() + ", Capacity: " + getCapacity() + ", Academic Department: " + getAcademicDepartment() + ", Projector Present: " + getProjector();
    }

}
import java.util.ArrayList;
import java.util.Scanner;

public class TestAcademicRoom {

    public TestAcademicRoom() {

    }

    boolean compare(AcademicRoom room, ArrayList<AcademicRoom> roomList) {

        for (int i = 0; i < roomList.size(); i++) {
            AcademicRoom room3 = roomList.get(i);
            if(room3.getProjector().equals(room.getProjector()) && room3.getAcademicDepartment().equals(room.getAcademicDepartment()) && room3.getBuildingName().equals(room.getBuildingName()) && room3.getCapacity() == room.getCapacity() && room3.getRoomNumber().equals(room.getRoomNumber()))
                return true;
        }
        return false;
    }

    ArrayList<AcademicRoom> search(ArrayList<AcademicRoom> roomList, String buildingName, String roomNo, boolean status) {
        ArrayList<AcademicRoom> result = new ArrayList<AcademicRoom>();

        if(status) {
            for(int i = 0; i < roomList.size(); i++) {
                AcademicRoom room1 = roomList.get(i);
                if(room1.getBuildingName().equals(buildingName) && room1.getRoomNumber().equals(roomNo)) {
                    result.add(room1);
                }
            }
        }else {
            for(int i = 0; i < roomList.size(); i++) {
                AcademicRoom room2 = roomList.get(i);
                if(room2.getBuildingName().equals(buildingName))
                    result.add(room2);
            }
        }

        return result;
    }


    public static void main(String[] args) {

        TestAcademicRoom testRoom = new TestAcademicRoom();
        ArrayList<AcademicRoom> roomList = new ArrayList<AcademicRoom>();
        // Adding rooms to list of own choice
        roomList.add(new AcademicRoom("james", "216", 300, "Science", true));
        roomList.add(new AcademicRoom("F2", "219", 305, "Arts", false));
        roomList.add(new AcademicRoom("XDD", "345", 100, "Literature", true));
        roomList.add(new AcademicRoom("GHJHFD", "556", 598, "Commerce", false));
        roomList.add(new AcademicRoom("JJHDG", "789", 200, "Robotics", false));

        Scanner sc = new Scanner(System.in);  
       while(true) {
        System.out.println("1. Add Another Room To the List.\n2. Search For rooms. \n3. Exit.");
        int input = sc.nextInt();
        if(input == 1) {
            String roomno, buildingName, department, projector;
            int capacity;
            System.out.print("Enter Room NO.: ");
            roomno = sc.next();
            System.out.print("Enter Building Name: ");
            buildingName = sc.next();
            System.out.print("Enter Capacity: ");
            capacity = sc.nextInt();
            System.out.print("Enter Department ");
            department = sc.next();
            System.out.print("Projector Present 'y or n': ");
            projector = sc.next();

            boolean p = false;
            if(projector.equals('Y') || projector.equals('y'))
                p = true;
            
            AcademicRoom room = new AcademicRoom(buildingName, roomno, capacity, department, p);

            if(!testRoom.compare(room, roomList)){
                roomList.add(new AcademicRoom(buildingName, roomno, capacity, department, p));
                System.out.println("Room Added....");
            }


        }else if(input == 2) {
            String buildingName, roomNo = "none", status;
            ArrayList<AcademicRoom> rooms;

            System.out.print("Enter Building Name: ");
            buildingName = sc.next();
            System.out.print("Enter Room no 'Y or N' ");
            status = sc.next();

            if(status.equals("Y") || status.equals("y")){
                roomNo = sc.next();
                rooms = testRoom.search(roomList, buildingName, roomNo, true);

                if(rooms.size() < 1)
                    System.out.print("No Rooms to show.");
                else {
                    for(int i = 0; i < rooms.size(); i++) {
                        System.out.println(rooms.get(i));
                    }
                } 
            }else {
                rooms = testRoom.search(roomList, buildingName, roomNo, false);

                if(rooms.size() < 1)
                    System.out.println("No Rooms to show...");
                else{
                    System.out.println("Rooms in the building: " + buildingName);
                    for (int i = 0; i < rooms.size(); i++) {
                        System.out.println(rooms.get(i));
                    }
                }
                
            }
            }else if(input == 3){
                break;
            }
                else {
                System.out.println("Wrong Input. Try Again....");
            }
       }
    }
}

Related Solutions

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...
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...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top...
class ArrayStringStack { String stack[]; int top; public arrayStringStack(int size) { stack = new String[size]; top = -1; } //Assume that your answers to 3.1-3.3 will be inserted here, ex: // full() would be here //empty() would be here... //push() //pop() //displayStack() } 1. Write two boolean methods, full( ) and empty( ). 2. Write two methods, push( ) and pop( ). Note: parameters may be expected for push and/or pop. 3. Write the method displayStack( ) that prints the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT