1. Create a new Java Project 2. Create a class named CabinRental that includes a
ID: 3721743 • Letter: 1
Question
1. Create a new Java Project
2. Create a class named CabinRental that includes an integer field for the cabin number and an int field for the weekly rental rate.
3. Include get methods for these fields and a constructor that requires an integer argument representing the cabin number.
4. The constructor sets the weekly rate based on the cabin number. Cabins numbered 1, 2, and 3 are $950 per week, and others are $1,100 per week.
5. Create an extended class named HolidayCabinRental that is used for rentals during weeks that include summer holiday weekends. The constructor for this class requires a cabin number and adds a $150 surcharge to the regular rental rate.
6. Write an application named DemoCabinRental that creates an object of each class, and demonstrate that all the methods work correctly. Save the three files.
Sample Output:
Low cabin #2 rate $950
High cabin #4 rate $1,100
Low holiday #2 rate $1,100
High holiday #4 rate $1,250
Explanation / Answer
class CabinRental:
-------------------------------
import java.util.*;
public class CabinRental {
public static int CabinNo;
public static int RentalRate;
CabinRental(int cabinNo){
CabinNo=cabinNo;
if(cabinNo==1||cabinNo==2||cabinNo==3)
RentalRate=950;
else
RentalRate=1100;
}
public int getCabinNo(){
return CabinNo;
}
public int getRentalRate(){
return RentalRate;
}
}
class HolidayCabinRental:
-----------------------------------
import java.util.*;
public class HolidayCabinRental extends CabinRental {
HolidayCabinRental(int cabinNo){
super(cabinNo); //Here super should be used as first statement in child Class,If we dont use,We get undefinedErr
CabinNo=cabinNo;
RentalRate+=150;
}
}
class DemoCabinRental:
---------------------------------
import java.util.*;
public class DemoCabinRental {
public static void main(String[] args)
{
CabinRental c=new CabinRental(2);
if(c.getCabinNo()>1&&c.getCabinNo()<=3)
System.out.println("Low "+ "cabin #"+c.getCabinNo()+" rate "
+ " $"+c.getRentalRate());
else
System.out.println("High "+ "cabin #"+c.getCabinNo()+" rate "
+ " $"+c.getRentalRate());
CabinRental c1=new CabinRental(4);
if(c1.getCabinNo()>1&&c1.getCabinNo()<=3)
System.out.println("Low "+ "cabin #"+c1.getCabinNo()+" rate "
+ " $"+c1.getRentalRate());
else
System.out.println("High "+ "cabin #"+c1.getCabinNo()+" rate "
+ " $"+c1.getRentalRate());
HolidayCabinRental h=new HolidayCabinRental(2);
if(h.getCabinNo()>1&&h.getCabinNo()<=3)
System.out.println("Low "+ "holiday #"+h.getCabinNo()+" rate "
+ " $"+h.getRentalRate());
else
System.out.println("High "+ "holiday #"+h.getCabinNo()+" rate "
+ " $"+h.getRentalRate());
HolidayCabinRental h1=new HolidayCabinRental(4);
if(h.getCabinNo()>1&&h.getCabinNo()<=3)
System.out.println("Low #"+h.getCabinNo()+" rate "
+ " $"+h.getRentalRate());
else
System.out.println("High #"+h.getCabinNo()+" rate "
+ " $"+h.getRentalRate());
}
}
Hope it helps
Happy Learning
Comment if u have any single doubt..I will make u understand/-
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.