Question

In: Computer Science

Put the ideas into practice by extending the original Room class (from lab 5) to create...

Put the ideas into practice by extending the original Room class (from lab 5) to create an AcademicRoom.java class.

Room.java:

import java.util.*;
public class Room
{
// fields
private String roomNumber;
private String buildingName;
private int capacity;

public Room()
{
this.capacity = 0;
}
  
public int compareTo(final Room o){
return Integer.compare(this.capacity, capacity);
}
/**
* 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;
}
}

  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).
  1. Include a constructor for the AcademicRoom class that takes advantage of the reserved word super. Also include appropriate getter/setter methods for the AcademicRoom.
  1. Add James Farmer B6 to your list as well as two or three other classrooms of your choosing.
  1. 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

Below is your code:

Room.java

import java.util.*;

public class Room {
        // fields
        private String roomNumber;
        private String buildingName;
        private int capacity;

        public Room() {
                this.capacity = 0;
        }

        public int compareTo(final Room o) {
                return Integer.compare(this.capacity, capacity);
        }

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

AcademicRoom.java

public class AcademicRoom extends Room {
        // fields
        private String department;
        private boolean isProjectorAvailable;

        /**
         * Constructor for objects of class AcademicRoom
         *
         * @param rN
         *            the room number
         * @param bN
         *            the building name
         * @param c
         *            the room capacity
         * @param department
         *            the room department
         * @param isProjectorAvailable
         *            the room isProjectorAvailable
         */
        public AcademicRoom(String rN, String bN, int c, String department,
                        boolean isProjectorAvailable) {
                super(rN, bN, c);
                this.department = department;
                this.isProjectorAvailable = isProjectorAvailable;
        }

        /**
         * Accessor method (getter) for department.
         *
         * @return the department
         */
        public String getDepartment() {
                return department;
        }

        /**
         * Mutator method (setter) for department.
         *
         * @param department
         *            a new department
         */
        public void setDepartment(String department) {
                this.department = department;
        }

        /**
         * Accessor method (getter) for isProjectorAvailable.
         *
         * @return the isProjectorAvailable
         */
        public boolean isProjectorAvailable() {
                return isProjectorAvailable;
        }

        /**
         * Mutator method (setter) for isProjectorAvailable.
         *
         * @param isProjectorAvailable
         *            a new isProjectorAvailable
         */
        public void setProjectorAvailable(boolean isProjectorAvailable) {
                this.isProjectorAvailable = isProjectorAvailable;
        }

        //method to get object as String
        public String toString() {
                return "Room Number: " + this.getRoomNumber() + "\nBuilding Name: "
                                + this.getBuildingName() + "\nCapacity: " + this.getCapacity()
                                + "\nDepartment: " + this.department
                                + "\nIs Projector Available: " + this.isProjectorAvailable;
        }
}

TestAcademicRoom.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestAcademicRoom {
        public static void main(String[] args) {
                List<AcademicRoom> rooms = new ArrayList<>();
                rooms.add(new AcademicRoom("121A", "Oracle", 22, "Engineering", true));
                rooms.add(new AcademicRoom("121B", "Oracle", 22, "Engineering", false));
                rooms.add(new AcademicRoom("221A", "World", 22, "Education", true));
                Scanner sc = new Scanner(System.in);

                int choice = 0;

                while (choice != 3) {
                        System.out.println();
                        printMenu();
                        choice = Integer.parseInt(sc.nextLine());
                        switch (choice) {
                        case 1:
                                System.out.print("Enter a room number: ");
                                String roomNum = sc.nextLine();
                                System.out.print("Enter a building name: ");
                                String buildingName = sc.nextLine();
                                System.out.print("Enter a room capacity: ");
                                int capacity = Integer.parseInt(sc.nextLine());
                                System.out.print("Enter a department name: ");
                                String department = sc.nextLine();
                                System.out.print("Is Projector Available in room (y/n): ");
                                String isProj = sc.nextLine();
                                if (isProj.equalsIgnoreCase("y")) {
                                        rooms.add(new AcademicRoom(roomNum, buildingName, capacity,
                                                        department, true));
                                } else {
                                        rooms.add(new AcademicRoom(roomNum, buildingName, capacity,
                                                        department, false));
                                }
                                break;
                        case 2:
                                System.out.println("Enter a building name: ");
                                String bName = sc.nextLine();
                                System.out.print("Do you want to enter a room number (y/n): ");
                                String isRoomEntered = sc.nextLine();
                                String roomN = "";
                                if (isRoomEntered.equalsIgnoreCase("y")) {
                                        System.out.print("Enter a room Number: ");
                                        roomN = sc.nextLine();
                                }
                                List<AcademicRoom> searchedRooms = new ArrayList<>();
                                if (isRoomEntered.equalsIgnoreCase("y")) {
                                        for (AcademicRoom academicRoom : rooms) {
                                                if (academicRoom.getBuildingName().equalsIgnoreCase(
                                                                bName)
                                                                && academicRoom.getRoomNumber()
                                                                                .equalsIgnoreCase(roomN)) {
                                                        searchedRooms.add(academicRoom);
                                                }
                                        }
                                } else {
                                        for (AcademicRoom academicRoom : rooms) {
                                                if (academicRoom.getBuildingName().equalsIgnoreCase(
                                                                bName)) {
                                                        searchedRooms.add(academicRoom);
                                                }
                                        }
                                }
                                System.out.println("\nSearched rooms are: ");
                                printList(searchedRooms);
                                break;
                        case 3:
                                System.out.println("\nThanks.");
                                break;
                        default:
                                System.out.println("Please enter a correct choice.");
                        }
                }
        }

        public static void printMenu() {
                System.out.println("Please select from below list: ");
                System.out.println("1. Add a new room");
                System.out.println("2. Search a room");
                System.out.println("3. Exit");
                System.out.print("Enter choice: ");
        }

        public static void printList(List<AcademicRoom> rooms) {
                for (AcademicRoom academicRoom : rooms) {
                        System.out.println(academicRoom);
                        System.out.println();
                }
        }
}

Output


Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 1
Enter a room number: 221B
Enter a building name: World
Enter a room capacity: 23
Enter a department name: Education
Is Projector Available in room (y/n): n

Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 2
Enter a building name:
World
Do you want to enter a room number (y/n): n

Searched rooms are:
Room Number: 221A
Building Name: World
Capacity: 22
Department: Education
Is Projector Available: true

Room Number: 221B
Building Name: World
Capacity: 23
Department: Education
Is Projector Available: false


Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 2
Enter a building name:
Oracle
Do you want to enter a room number (y/n): y
Enter a room Number: 121B

Searched rooms are:
Room Number: 121B
Building Name: Oracle
Capacity: 22
Department: Engineering
Is Projector Available: false


Please select from below list:
1. Add a new room
2. Search a room
3. Exit
Enter choice: 3

Thanks.


Related Solutions

i'd like to get extra practice for a class. Create 5 multiple-choice questions covering 5 different...
i'd like to get extra practice for a class. Create 5 multiple-choice questions covering 5 different respiratory disorders. Pick 5 from the following list. Respiratory failure Asthma COPD Cystic Fibrosis Pneumonia TB Influenza. label the correct answer, & explain why
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference. …………………………...…….. …………………………...……. Instructions LAB5 Instructions Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an   equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width....
An exception with finally block lab. Create a new class named ReadArray Create simple array and...
An exception with finally block lab. Create a new class named ReadArray Create simple array and read an element using a try block Catch any exception Add a finally block and print a message that the operation if complete
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String Stack<E> For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5 UML for Stack<E> Stack<E> - top : Node<E> - numElements : int + Stack( ) + push( element : E ) : void + pop( ) : E + size(...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
i just need the Polygon class Shapes In this lab you will create an interface, and...
i just need the Polygon class Shapes In this lab you will create an interface, and then implement that interface in three derived classes. Your interface will be called Shape, and will define the following functions: Return Type Name Parameters Description double area none computes the area of the shape double perimeter none computes the perimeter of the shape Point2d center none computes the center of the shape You will then create 3 implementations of this interface: Rectangle, Circle, and...
Your first lab is to create a simple class that emulates some mathematical functions in relation...
Your first lab is to create a simple class that emulates some mathematical functions in relation to complex numbers. A complex number is a number of the form a + bi, where a is a real number and bi is an imaginary number. Create a class Complex having two private data members real and imag of type double. The class has two constructors, one default (no parameters) and one whose parameters initialize the instance variables. It also need the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT