インターフェイスの実装を含むプログラミング クラスに参加するための宿題に取り組んでいます。ここでの問題は、私がインターフェイスやその使用目的を完全に理解していないことです(教授はそれを説明するのがあまり得意ではありませんでした)。
割り当てられたのは、「乗り物」のスーパークラスを作成することでした。3 つのサブクラスよりも、「トラック」や「ジープ」のような、それぞれ独自の特性を持つものを作成することでした。「Vehicle」クラスは、私が理解したと思われる同等のインターフェースを実装する必要があり(compareTo()
車両のドアの数を比較する方法が書かれています)、他の1つのクラスも「Hybrid」クラスを実装する必要があります(わかりませんこれが何を意味するか)。toString()
次に、、、equals(Object o)
およびcompareTo(Object o)
メソッドを実装する必要があります。
私はcompareTo()
ダウンを持っていると思いますが、equals()
私にはわかりません。最後に行う必要があるのは、テストする Main を作成することです。これには、Vehicle オブジェクトの配列を作成し、それらを出力し、並べ替えてから再出力する必要があります。また、配列を反復処理してハイブリッド車のプレミアム価格を出力し、equals(Object o)
メソッドを使用して 2 台の車両を比較する必要があります。
これが私の「車両」スーパークラスのコードです
package vehicle;
abstract public class Vehicle implements Comparable {
private String color;
private int numberOfDoors;
// Constructor
/**
* Creates a vehicle with a color and number of doors
* @param aColor The color of the vehicle
* @param aNumberOfDoors The number of doors
*/
public Vehicle(String aColor, int aNumberOfDoors) {
this.color = aColor;
this.numberOfDoors = aNumberOfDoors;
}
// Getters
/**
* Gets the color of the vehicle
* @return The color of the vehicle
*/
public String getColor() {return(this.color);}
/**
* Gets the number of doors the vehicle has
* @return The number of doors the vehicle has
*/
public int getNumberOfDoors() {return(this.numberOfDoors);}
// Setters
/**
* Sets the color of the vehicle
* @param colorSet The color of the vehicle
*/
public void setColor(String colorSet) {this.color = colorSet;}
/**
* Sets the number of doors for the vehicle
* @param numberOfDoorsSet The number of doors to be set to the vehicle
*/
public void setNumberOfDoors(int numberOfDoorsSet) {this.numberOfDoors = numberOfDoorsSet;}
@Override
public int compareTo(Object o) {
if (o instanceof Vehicle) {
Vehicle v = (Vehicle)o;
return this.numberOfDoors - v.getNumberOfDoors();
}
else {
return 0;
}
}
/**
* Returns a short string describing the vehicle
* @return a description of the vehicle
*/
@Override
public String toString() {
String answer = "The car's color is "+this.color
+". The number of doors is"+this.numberOfDoors;
return answer;
}
}
また、サブクラスの1つを投稿します
package vehicle;
abstract public class Convertible extends Vehicle {
private int topSpeed;
// Constructor
/**
* Creates a convertible with a color, number of doors, and top speed
* @param aColor The color of the convertible
* @param aNumberOfDoors The number of doors of the convertible
* @param aTopSpeed The top speed of the convertible
*/
public Convertible (String aColor, int aNumberOfDoors, int aTopSpeed) {
super(aColor, aNumberOfDoors);
this.topSpeed = aTopSpeed;
}
// Getters
/**
* Gets the top speed of the convertible
* @return The top speed of the convertible
*/
public int getTopSpeed() {return(this.topSpeed);}
// Setters
/**
* Sets the top speed of the convertible
* @param topSpeedSet The top speed to set to the convertible
*/
public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}
/**
* Returns a short description of the convertible
* @return a short description of the convertible
*/
@Override
public String toString() {
String answer = "The car's color is "+super.getColor()
+", the number of doors is "+super.getNumberOfDoors()
+", and the top speed is "+this.topSpeed+" mph.";
return answer;
}
}
私は間違いなく誰かに私のために仕事をするように頼んでいませんが、誰かがインターフェイスがどのように機能するか、そしてこの宿題が本当に求めていることをもう少し明らかにすることができれば、それは素晴らしいことです.
すべての助けは非常に高く評価されています
ありがとう!