Question

In: Computer Science

Define the class HotelRoom. The class has the following private data members: the room number (an...

Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler should display the message “Negative Parameter” [20pts]. Include a toString() function that nicely formats and returns a string that displays the information about the hotel room [10pts].

  1. Write a main function to test the class HotelRoom, create a HotelRoom object. Try to set the room rate to an invalid value to generate an exception. Invoke the toSting() function to display the HotelRoom object. [20pts]
  2. Derive the classes GuestRoom form the base class HotelRoom. The GuestRoom has private data fields and public functions:
    1. The private data field capacity (an Integer) that represents the maximum number of guests that can occupy the room. [5pts]
    2. The private data member status (an integer), which represents the number of guests in the room (0 if unoccupied). [5pts]
    3. An integer data field days that represents the number of days the guests occupies the room. [5pts]
    4. Add constructors and get/set functions to the GuestRoom class. The set function for the status data member must throw an out_of_range exception if it tries to set status to value greater than the capacity. [30pts]
    5. The function calculateBill() that returns the amount of guest’s bill. [10pts]
    6. Redefine the function toString() that formats and returns a string containing all pertinent information about the GuestRoom. [15pts]
  3. Derive the classes MeetingRoom form the base class HotelRoom. The class has the following private data filed sand public functions:
    1. A private data field seats, which represents the number of seats in the room. [5pts]
    2. An integer data field status (1 if the room is booked and 0 otherwise). [5pts]
    3. Add constructors and get/set functions to the GuestRoom class. [10pts]
    4. Redefine the function toSting() to format and return a string containing all pertinent information about the MeetingRoom. [20pts]
    5. The function CalculateBill(), which returns the amount of the bill for renting the room for one day. The function calculates the bill as follows: the number of seats multiplied by 10.00, plus 500.00. [20pts]
  4. Write a main function to test the classes GuestRoom and MeetingRoom. Invoke the calculateBills and toStirng() in each of the objects. [40pts]

Solutions

Expert Solution

Thanks for the question


Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.


Thank You & please do give a thumbs up ;

regards, Friend !

==========================================================================================

public class HotelRoom {

    private int roomNumber;
    private double dailyRate;

    public HotelRoom() {

        roomNumber = 0;
        dailyRate = 0.0;

    }

    //must throw an invalid_argument exception if either one of the parameter values are negative.
    public HotelRoom(int roomNumber, double dailyRate) {
        if (roomNumber < 0 || dailyRate < 0) throw new IllegalArgumentException("Negative Parameter");
        this.roomNumber = roomNumber;
        this.dailyRate = dailyRate;
    }

    public int getRoomNumber() {
        return roomNumber;
    }

    //must throw an invalid_argument exception if either one of the parameter values are negative.
    public void setRoomNumber(int roomNumber) {
        if (roomNumber < 0) throw new IllegalArgumentException("Negative Parameter");
        this.roomNumber = roomNumber;
    }

    public double getDailyRate() {
        return dailyRate;
    }

    //must throw an invalid_argument exception if either one of the parameter values are negative.
    public void setDailyRate(double dailyRate) {
        if (dailyRate < 0) throw new IllegalArgumentException("Negative Parameter");
        this.dailyRate = dailyRate;
    }

    @Override
    public String toString() {
        return "HotelRoom: " +
                "Room Number:" + roomNumber +
                ", Daily Rate: $" + dailyRate;

    }
}

==========================================================================================

public class GuestRoom extends HotelRoom {

    private int capacity;
    private int status;
    private int days;

    public GuestRoom(int capacity, int status, int days) {
        if (status > capacity)
            throw new IllegalArgumentException("Out of Range");
        this.capacity = capacity;
        this.status = status;
        this.days = days;
    }

    public GuestRoom(int roomNumber, double dailyRate, int capacity, int status, int days) {

        super(roomNumber, dailyRate);
        if (status > capacity)
            throw new IllegalArgumentException("Out of Range");
        this.capacity = capacity;
        this.status = status;
        this.days = days;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        if (status > getCapacity())
            throw new IllegalArgumentException("Out of Range");
        this.status = status;
    }

    public int getDays() {
        return days;
    }

    public void setDays(int days) {
        this.days = days;
    }

    public double calculateBill() {
        return getDailyRate() * getDays() * getStatus() / getCapacity();
    }

    @Override
    public String toString() {

        return "Guest Room: " +
                "Room Number:" + getRoomNumber() +
                ", Daily Rate: $" + getDailyRate()+
                ", Room Capacity: " + capacity +
                ", Persons: " + status +
                ", Booked for " + days +" days."
                ;
    }
}

==========================================================================================

public class MeetingRoom extends HotelRoom {

    private int seats;
    private int status;
    private boolean isBooked;


    public MeetingRoom(int seats, int status, boolean isBooked) {
        this.seats = seats;
        // if status is isBooked then we set the status to 0
        this.status = isBooked ? status : 0;
        this.isBooked = isBooked;
    }

    public MeetingRoom(int roomNumber, double dailyRate, int seats, int status, boolean isBooked) {
        super(roomNumber, dailyRate);
        this.seats = seats;
        // if status is isBooked then we set the status to 0
        this.status = isBooked ? status : 0;
        this.isBooked = isBooked;
    }

    public int getSeats() {
        return seats;
    }

    public void setSeats(int seats) {
        this.seats = seats;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public boolean isBooked() {
        return isBooked;
    }

    public void setBooked(boolean booked) {
        isBooked = booked;
    }

    @Override
    public String toString() {
        return "Meeting Room: " +
                "Room Number:" + getRoomNumber() +
                ", Daily Rate: $" + getDailyRate() +
                ", Seat: " + seats +
                ", Persons: " + status +
                ", Booked: " + isBooked;
    }

    public double calculateBill() {

        return getSeats() * 10 + 500;
    }
}

==========================================================================================

public class MainHotel {

    public static void main(String[] args) {

        //Write a main function to test the class HotelRoom, create a
        // HotelRoom object. Try to set the room rate to an invalid value
        // to generate an exception. Invoke the toSting() function to
        // display the HotelRoom object. [20pts]

        HotelRoom hyaat = new HotelRoom(121, 4500);
        //Invoke the toSting() function to
        //display the HotelRoom object.
        System.out.println(hyaat);

        try {
            // Try to set the room rate to an invalid value
            hyaat.setDailyRate(-4000);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }

        //Write a main function to test the classes GuestRoom and MeetingRoom.
        GuestRoom guestRoom = new GuestRoom(122, 599, 4, 3, 10);
        MeetingRoom meetingRoom = new MeetingRoom(145, 200, 20, 15, true);

        //Invoke the calculateBills and toStirng() in each of the objects.
        System.out.println(guestRoom);
        System.out.println(meetingRoom);
        System.out.println("Guess Room Cost: $"+guestRoom.calculateBill());
        System.out.println("Meeting Room Cost: $"+meetingRoom.calculateBill());


    }
}

==========================================================================================

==========================================================================================


Related Solutions

Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Part 1 Create a class named Room which has two private data members which are doubles...
Part 1 Create a class named Room which has two private data members which are doubles named length and width. The class has five functions: a constructor which sets the length and width, a default constructor which sets the length to 12 and the width to 14, an output function, a function to calculate the area of the room and a function to calculate the parameter. Also include a friend function which adds two objects of the room class. Part...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
I. Design and code a class Rectangle that has the following properties: Two private members double...
I. Design and code a class Rectangle that has the following properties: Two private members double length and width A constructor that accepts two parameters for the private members Default constructor that sets both members to 0 Setters/getters for each private member Method area() that returns the area of the rectangle Method Perim() that returns the perimeter of the rectangle Method toString that return the param as a String Method Add that accepts a Rectangle and returns a rectangle with...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT