有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为继承层次结构建模。。。JAVA

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.

有人能告诉我一些关于你是否认为我以正确的方式处理这个问题的建议吗。我遇到的主要问题是计算机方法。我不知道如何确定旅馆、床上早餐和招待所的住宿价格。我试着使用super,但我不完全确定这是否是我应该做的

//父类

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

public double computeRate(int numberOfNights){

    price *= numberOfNights;
    return sum;
}

//子类

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;   
    }  
}  

//子类

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;
    } 
}

//子类

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;

下面是给定的框架代码

  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

共 (1) 个答案

  1. # 1 楼答案

    您的每个类都有房间,因此我会将其移动到父类,并将其添加到构造函数中

    另外,Lodging是一个抽象概念,因此您不能创建new Lodging(),您需要一个特定的实例

    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;
        }
    }
    

    那么,对于Hostel示例,super(rooms, 20)没有什么问题。它正确地设置了父类的字段,每个子类将继承超类的computeRate()方法。问题描述并不表示需要覆盖它