Create a class named HotelRoom that includes an integer field for the room numbe
ID: 3643683 • Letter: C
Question
Create a class named HotelRoom that includes an integer field for the room number and a double field for the nightly rental rate. Include get methods for these fields and a constructor that requires an integer argument representing the room number. The constructor sets the room rate based on the room number; rooms numbered 299 and below are $69.95 per night, and others are $89.95 per night. Create an extended class named Suite whose constructor requires a room number and adds a $40 surcharge to the regular hotel room rate, which again is based on the room number. Write an application named UseHotelRoom that creates an object of each class, and demonstrate that all the methods work correctly. Save the files as HotelRoom.java, Suite.java, and UseHotelRoom.java.Explanation / Answer
import javax.swing.*;
public class HotelRoom
{
private int roomNumber;
protected double rate;
public void setroomNumber(int num)
{
roomNumber=num;
}
public int getroomNumber()
{
return roomNumber;
}
public void setRate (double r)
{
rate=r;
}
public double getRate()
{
return rate;
}
public HotelRoom(int room)
{
roomNumber=room;
if(room <=299)
rate=69.95;
else
rate=89.95;
}
//I don't know if this next bit is correct
public void displayRate(int room)
{
if(room<=299)
JOptionPane.showMessageDialog(null,
"The room will cost $69.95 per night.");
else
JOptionPane.showMessageDialog(null,
"The room will cost $89.95 per night.");
}
}
public class Suite extends HotelRoom
{
public Suite (int room)
{
super(room);
rate +=40;
}
}
import javax.swing.*;
public class UseHotelRoom
{
public static void main (String [] args)
{
HotelRoom myRoom = new HotelRoom(300);
myRoom.displayRate();
HotelRoom theirRoom = new HotelRoom(201);
theirRoom.displayRate();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.