Write a class definition for a subclass suite92. A suite92 \"is a\" hotelroom92,
ID: 3856124 • Letter: W
Question
Write a class definition for a subclass suite92. A suite92 "is a" hotelroom92, but use the fact that a suite92 is a hotelroom92 with extras (that cost $30.00 more). The extra $30.00 is the only difference you have to code. Write the constructor for suite92, calling the hotelroom92 constructor to initialize the two inherited data fields and then you add the $30 on to the roomrate. The rest of the suite92 class would be the same as a hotelroom92. The hotelroom92 class definition and the main ex92c for the hotelroom92 are attached.
2c. Add a suite92 object to main ex92c with room number 475 by running the constructor. Use the inherited get methods to print the room number and room rate (like the hotelroom92 object).
Hotelroom class:
Hotel Room main method:
Explanation / Answer
//Suit92 an subclass of hotelroom92
public class suit92 extends hotelroom92 {
//suit92 is a hotelroom92 by extending hotelroom92
protected int roomnum;
protected double roomrate = 30.00;
public suit92(int n)
{
//calculation - room rent = hotelroom92 room rate _ addtional $30.00 chareges for suit92
roomnum = n;
if (roomnum <= 299)
this.roomrate += 69.95;
else
this.roomrate += 89.95;
}
// other characters of suit92 remains same as hotelroom92
public int getroomnum()
{
return roomnum;
}
public double getroomrate()
{
return roomrate;
}
}
// hotelroom92
public class hotelroom92
{
public hotelroom92(){
}
protected int roomnum;
protected double roomrate;
public hotelroom92(int n)
{
roomnum = n;
if (roomnum <= 299)
roomrate = 69.95;
else
roomrate = 89.95;
}
public int getroomnum()
{
return roomnum;
}
public double getroomrate()
{
return roomrate;
}
}
//Execution class with main method
public class ex92c
{
public static void main(String[] args)
{
hotelroom92 h = new hotelroom92(375);
System.out.println("Hotel Room " + h.getroomnum() + " " +
h.getroomrate());
// instantiating suit92 and get the rates
suit92 s = new suit92(475);
System.out.println("Hotel Room Suit-92 room number " + s.getroomnum() + " " +
s.getroomrate());
}
}
All these classes should be saved on the same directory/folder and can be executed. use execution instructions
javac *.java
and then
java ex92c
/* Sample out put as below
* Hotel Room 375 89.95
Hotel Room Suit-92 room number-475 119.95
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.