Write a class Car with a constructor that accepts a yearModel, a make, and a speed (in MPH) as its argument

huangapple 未分类评论45阅读模式
英文:

Write a class Car with a constructor that accepts a yearModel, a make, and a speed (in MPH) as its argument

问题

以下是翻译好的内容:

我们需要编写一个名为Car的类,它接受型号(model)、制造商(make)和速度(speed)作为参数。我们已经获得了运行主方法的代码,并且只需要编写包括加速(将速度增加5 mph)和刹车(将速度减少5 mph,但不低于0)功能的类。

主方法提供了以下代码行来设置参数:

Car a = new Car(2008, "Toyota", 0);
Car b = new Car(2017, "Ford", 20);
Car c = new Car(2012, "Volvo", 6);
Car[] test_field = {a, b, c};

主方法中的调用如下:

for(int i = 0; i < test_field.length; i++){
    test_field[i].accelerate();
}

或者:

for(int i = 0; i < test_field.length; i++){
    test_field[i].brake();
}

在编写方法时,当我尝试运行时,我一直收到“Car类中的方法无法适用于给定类型”的错误,分别是在加速(accelerate)和刹车(brake)方法中。我似乎无法找出问题所在。

以下是你的代码:

public class Car {
    
    private int yearModel;
    private String make;
    private int speed;
    
    public Car() {
       
    }
    
    public Car(int ym, String m, int sp) {
       
        yearModel = ym;
        make = m;
        speed = sp;
    } 
    
    public void setModel(int ym) {
        yearModel = ym;
    }
    
    public void setMake(String m) {
        make = m;
    }
    
    public void setSpeed(int sp) {
        speed = sp;
    }
       
    public int accelerate(Car c) {
        speed = speed + 5;
        return speed;
    }
    
    public int brake(Car c) {
        speed = speed - 5;
      
        if (speed < 0) {
            speed = 0;
            return speed;
        }
        else {
            return speed;
        }
    }
}

对于一个初学者的编程者,如果能提供任何建议或见解,我会非常感激。

英文:

So we are expected to write a class Car that accepts model, make and speed as an argument. We are given the main method to run and are just expected to write the class that includes and Accelerate (add 5 mph to speed) and brake ( decrease 5 mph but never below 0).

The main method gives us these lines of code to set the argument:

Car a = new Car(2008, &quot;Toyota&quot;, 0);
Car b = new Car(2017, &quot;Ford&quot;, 20);
Car c = new Car(2012, &quot;Volvo&quot;, 6);
Car [] test_field = {a, b, c}; 

the calls in the main are shown as

for(int i = 0; i &lt; test_field.length; i++){
    test_field[i].accelerate();
   }
}
else{
     for(int i = 0; i&lt;test_field.length; i++){
        test_field[i].brake();
}

when writing the method, i keep getting a method in class Car cannot be applied to given types for both my accelerate and brake methods when i try to run and I cant seem to figure out my issue.

my code:

public class Car {

private int yearModel;
private String make;
private int speed;

   public Car() {
   
   }
   public Car(int ym, String m, int sp) {
   
   yearModel = ym;
   make = m;
   speed = sp;
 } 
   public void setModel(int ym) {
   yearModel = ym;
   }
   public void setMake(String m) {
   make = m;
   }
   public void setSpeed(int sp) {
   speed = sp;
   
   }
   
 
  public int accelerate(Car c) {
  
  speed = speed + 5;
  
  return speed;
  
  }
  public int brake(Car c) {
 speed = speed - 5;
  
  if (speed &lt; 0) {
  speed = 0;
  return speed;
  }
  
  else return speed;
  
  }
}

any input or insight for a novice coder appreciated.

答案1

得分: 0

为什么你的加速(accelerate)和刹车(brake)方法在方法签名中需要一个Car对象?

public int accelerate(Car c)

你在调用方法时也没有传递Car对象。

test_field[i].accelerate();

由于你在方法中并没有使用c,你可以移除这个参数。

应该像这样修改:

public int accelerate() {
    speed = speed + 5;
    return speed;
}

public int brake() {
    speed = speed - 5;
    if (speed < 0) {
        speed = 0;
        return speed;
    }
    else {
        return speed;
    }
}
英文:

Why does your accelerate and brake methods require a Car object in the method signature?

public int accelerate(Car c)

You also don't pass a Car object when you call the method.

test_field[i].accelerate();

Since you don't do anything with c in the method, you can remove that argument.

Should look like this:

public int accelerate() {
    speed = speed + 5;
	return speed;
}

public int brake() {
    speed = speed - 5;
    if (speed &lt; 0) {
        speed = 0;
        return speed;
	}
	else {
	    return speed;
	}
}

huangapple
  • 本文由 发表于 2020年4月8日 01:33:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61085952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定