Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a class named Car.
Java
public class [1] { }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a name that does not represent the object, like Engine or Drive.
β Incorrect
The class name should be Car to model a car.
2fill in blank
mediumComplete the code to add a field for the car's color.
Java
public class Car { private [1] color; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using numeric types like int or double for color.
β Incorrect
The color of a car is best represented as a String because it is text.
3fill in blank
hardFix the error in the constructor to set the car's color.
Java
public class Car { private String color; public Car(String [1]) { this.color = color; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a different parameter name than the one assigned to the field.
β Incorrect
The constructor parameter must be named color to match the assignment this.color = color;.
4fill in blank
hardFill both blanks to add a method that returns the car's color.
Java
public class Car { private String color; public String [1]() { return [2]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Returning the parameter name or a wrong variable.
Using a method name that does not follow the getter naming convention.
β Incorrect
The method name to get the color is getColor, and it returns this.color to access the field.
5fill in blank
hardFill all three blanks to add a method that sets the car's color.
Java
public class Car { private String color; public void [1](String [2]) { this.[3] = [2]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using the same name for parameter and field without 'this.' prefix.
Using incorrect method or parameter names.
β Incorrect
The setter method is named setColor, it takes a parameter newColor, and assigns it to the field color.