前提・実現したいこと
下記のエラーメッセージを解決したいのですが、ポリ−フォーフィズムを用いてオーバーロードしたメソッドを使いたい。
発生している問題・エラーメッセージ
TestPackage.java:12: エラー: クラス SimpleCarのメソッド inputDataは指定された型に適用できません。 cars[0].inputData(7f, 9f, 1f, 1f, 10); ^ 期待値: float,float,float,float 検出値: float,float,float,float,int 理由: 実引数リストと仮引数リストの長さが異なります TestPackage.java:13: エラー: クラス SimpleCarのメソッド inputDataは指定された型に適用できません。 cars[1].inputData(7f, 9f, 1f, 1f, 10); ^ 期待値: float,float,float,float 検出値: float,float,float,float,int 理由: 実引数リストと仮引数リストの長さが異なります エラー2個
該当のソースコード
java
1 2public class SimpleCar{ 3 protected String maker; 4 protected float fuelEconomy; 5 protected float startTime; 6 protected float endTime; 7 protected float mileage;//Total of driving kilometer 8 protected float cliantTime; 9 10 public SimpleCar(String maker, float fuelEconomy){//no airconditioner 11 this.maker = maker; 12 this.fuelEconomy = fuelEconomy; 13 } 14 public void inputData(float startTime, float endTime, float mileage, float cliantTime){ 15 this.startTime = startTime; 16 this.endTime = endTime; 17 this.mileage = mileage; 18 this.cliantTime = cliantTime; 19 } 20 public float calculateFuelConsumptionOldRegulations(){ 21 return mileage/fuelEconomy; 22 } 23 public float calculateFuelConsumptionNewRegulations(){ 24 return mileage/fuelEconomy; 25 } 26} 27--------------------------------------------------------------------------- 28public class AdvancedCar extends SimpleCar{ 29 protected float airConditionerFuelEconomy; 30 31 public AdvancedCar(String maker, float fuelEconomy, float airConditionerFuelEconomy){ 32 super(maker, fuelEconomy); 33 this.airConditionerFuelEconomy = airConditionerFuelEconomy; 34 } 35 public float calculateFuelConsumptionOldRegulations(){ 36 return mileage/fuelEconomy + (endTime-startTime)/airConditionerFuelEconomy; 37 } 38 public float calculateFuelConsumptionNewRegulations(){ 39 return mileage/fuelEconomy + (endTime-startTime)/airConditionerFuelEconomy; 40 } 41} 42------------------------------------------------------------------------------- 43public class DeluxeCar extends AdvancedCar{ 44 45 public DeluxeCar(String maker, float fuelEconomy, float airConditionerFuelEconomy){ 46 super(maker, fuelEconomy, airConditionerFuelEconomy); 47 } 48 public float calculateFuelConsumptionOldRegulations(){ 49 return mileage/fuelEconomy + (endTime-startTime)/airConditionerFuelEconomy; 50 } 51 public float calculateFuelConsumptionNewRegulations(){ 52 return mileage/fuelEconomy + cliantTime/airConditionerFuelEconomy; 53 } 54} 55------------------------------------------------------------------------------- 56public class BusTaxi extends DeluxeCar{ 57 protected int minNumPassengers; 58 59 public BusTaxi(String maker, float fuelEconomy, float airConditionerFuelEconomy){ 60 super(maker, fuelEconomy, airConditionerFuelEconomy); 61 } 62 public float calculateFuelConsumptionOldRegulations(){ 63 return mileage/fuelEconomy + (endTime-startTime)/(airConditionerFuelEconomy/minNumPassengers); 64 } 65 public float calculateFuelConsumptionNewRegulations(){ 66 return mileage/fuelEconomy + cliantTime/(airConditionerFuelEconomy/minNumPassengers); 67 } 68 public void inputData(float startTime, float endTime, float mileage, float cliantTime, int minNumPassengers){ 69 super.inputData(startTime, endTime, mileage, cliantTime); 70 this.minNumPassengers = minNumPassengers; 71 } 72} 73--------------------------------------------------------------------- 74public class FuelConsumptionCalculation { 75 private float fuelConsumptionOldRegulation = 0; 76 private float fuelConsumptionNewRegulation = 0; 77 private SimpleCar[] cars; 78 79 public FuelConsumptionCalculation(SimpleCar[] Cars) { 80 cars = Cars; 81 calculateFuelConsumption(); 82 } 83 private void calculateFuelConsumption() { 84 for(int i=0; i<cars.length; i++){ 85 fuelConsumptionOldRegulation += cars[i].calculateFuelConsumptionOldRegulations(); 86 } 87 for(int i=0; i<cars.length; i++){ 88 fuelConsumptionNewRegulation += cars[i].calculateFuelConsumptionNewRegulations(); 89 } 90 } 91 public float getFuelConsumptionOldRegulations() { 92 return fuelConsumptionOldRegulation; 93 } 94 public float getFuelConsumptionNewRegulations() { 95 return fuelConsumptionNewRegulation; 96 } 97} 98------------------------------------------------- 99public class TestPackage{ 100 public static void main(String[] args){ 101 SimpleCar[] cars = { 102 new BusTaxi("Mercedes", 6.6f, 11.5f), 103 new BusTaxi("Mercedes", 6.6f, 11.5f), 104 new DeluxeCar("Honda", 14f, 10.5f), 105 new AdvancedCar("Toyota", 15.5f, 9.5f), 106 new SimpleCar("Nissan", 13f), 107 }; 108 cars[0].inputData(7f, 9f, 1f, 1f, 10); 109 cars[1].inputData(7f, 9f, 1f, 1f, 10); 110 cars[2].inputData(7f, 9f, 1f, 1f); 111 cars[3].inputData(7f, 9f, 1f, 1f); 112 cars[4].inputData(7f, 9f, 1f, 1f); 113 114 FuelConsumptionCalculation fcc = new FuelConsumptionCalculation( cars ); 115 116 System.out.println("The amount of saving fuel on airconditioner usage is "+ (fcc.getFuelConsumptionOldRegulations()-fcc.getFuelConsumptionNewRegulations()) +"."); 117 } 118} 119
回答1件
あなたの回答
tips
プレビュー