Working with void methods
Classes perform actions using methods, and methods do not always have to return some form of data type. You will implement a method to turn on a car's engine inside the Car
class.
Cet exercice fait partie du cours
Introduction to Object-Oriented Programming in Java
Instructions
- Create a
void
method calledturnEngineOn
that takes no parameters inside theCar
class. - Print the message
"engine is on"
inside theturnEngineOn
method. - Call the
turnEngineOn
on themyCar
object instance already created for you.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
class Main {
static class Car {
String color;
String model;
int year;
Car(String color, String model, int year) {
this.color = color;
this.model = model;
this.year = year;
}
// Create the turnEngineOn method
____ ____() {
// Print out "engine is on"
____;
}
}
public static void main(String[] args) {
Car myCar = new Car("red", "camry", 2022);
// Call the turnEngineOn method on the myCar object instance
____.____;
}
}