0

Lodgings in Oshkosh can be classified into three kinds – Hotels (modeled by the number of rooms and star rating), Hostels (modeled by the number of rooms and whether bike rentals are available), and Bed-n-Breakfasts (modeled by the number of rooms and whether late arrivals are allowed). Rates per room per night are always fixed and cost $40, $20 and $55 for Hotels, Hostels and Bed-n-Breakfasts respectively.The skeleton code for an inheritance hierarchy that models this scenario is stubbed out below. Complete all missing parts so that the code compiles, models the inheritance hierarchy, and has all the functionality described above. You should decide which classes the appropriate data members are stored in, and the types of those data members. In every class, complete theconstructor and provide a method, computeRate,which takes as parameter the number of nights spent and returns the total room rate for that lodging.No other methods should be provided.

Can someone give me some tips on weather you think I am going about this problem in the right way. the main thing that I am having issues with is the computeRate method. Im not sure how to set the prices of the Lodgings for hotels, Bed-n-Breakfasts, and hostels. I tried using super but im not completely sure if thats what I am supposed to do.

// parent class

public class Lodging
{
    int sum;
    int price;
    public Lodging( int price ) {
        this.price = price;
    }
}

public double computeRate(int numberOfNights){

    price *= numberOfNights;
    return sum;
}

// child class

public class Hostel extends Lodging
{
    private int numberOfRooms;
    private boolean bikeRentals;

    public Hostel( int rooms, boolean rentals) { 
        super(20);
        this.numberOfRooms = rooms;
        this.bikeRentals = rentals;   
    }  
}  

// child class

public class Hotel extends Lodging
{
    private int rooms;
    private int starRating;

    public Hotel( int rooms, int starRating ) {
        super(40);
        this.rooms = rooms;
        this.starRating = starRating;
    } 
}

// child class

public class BednBreakfast extends Lodging
{
    private int numberOfRooms;
    private boolean lateArrivals;

    public BednBreakfast( int rooms, boolean late ){
        super(55);
        this.numberOfRooms = rooms;
        this.late = late;

here is the given skeleton code

  class Lodging
  { 
         public Lodging(                   ) 
         { 

         } 
  }//End class Lodging 

  class Hotel 
  { 
         public Hotel(                    ) 
         {

         } 
  }//End class Hotel 

  class Hostel 
  {
         public Hostel(         ) 
         { 

         } 
  }//End class Hostel 

  class BednBreakfast 
  { 
         public BednBreakfast (       ) 
         { 

         } 
  }//End class BednBreakfast
14
  • 1
    what is the difference between rooms and numberOfRooms? Commented Feb 21, 2018 at 7:39
  • you're not supposed to use 'super', you should provide getters and setters (basic mutators) Commented Feb 21, 2018 at 7:39
  • there is no difference between numberOfRooms and rooms. they are the same thing but i guess i forgot to type numberOfRooms for one of them' Commented Feb 21, 2018 at 7:42
  • 1
    if they are the same, it should be in lodging, not in the other classes. They inherit whatever is in Lodging, since they extend that class Commented Feb 21, 2018 at 7:42
  • 1
    @Stultuske to teach super? Commented Feb 21, 2018 at 8:27

1 Answer 1

1

Each of your classes has rooms, so I would move that to the parent class, and add it to the constructor.

Also, a Lodging is an abstract concept, so you cannot make a new Lodging(), you need a specific instance of one.

public abstract class Lodging {
    private double nightlyRate;
    private int capacity;

    public Lodging( int capacity, double rate ) {
        nightlyRate = rate;
        this.capacity = capacity;
    }

    public double computeRate(int numberOfNights){
        return this.nightlyRate * numberOfNights;
    }
}

Then, there's nothing wrong with super(rooms, 20), for the Hostel example. It's correctly setting up the parent class's fields, and each subclass will inherit the super class's computeRate() method. The problem description doesn't indicate it needs to be overriden.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.