LoslegenKostenlos loslegen

Working with void methods

Classes perform actions using methods, and methods do not always have to return a specific data type. You will implement a method to turn on a car's engine inside the Car class.

Diese Übung ist Teil des Kurses

Introduction to Object-Oriented Programming in Java

Kurs anzeigen

Anleitung zur Übung

  • Create a void method called turnEngineOn that takes no parameters inside the Car class.
  • Print the message "engine is on" inside the turnEngineOn method.
  • Call the turnEngineOn on the myCar object instance already created for you.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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"
          System.out.println("____");            
      }

  }
  public static void main(String[] args) {
    Car myCar = new Car("red", "camry", 2022);
    // Call the turnEngineOn method on the myCar object instance
    ____.____();
  }
}
Code bearbeiten und ausführen