Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a getter method for the field name.
Java
public String [1]() { return name; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'setName' instead of 'getName' for a getter method.
Using the field name directly as method name.
β Incorrect
Getter methods in Java start with 'get' followed by the field name with the first letter capitalized.
2fill in blank
mediumComplete the code to declare a setter method for the field age.
Java
public void [1](int age) {
this.age = age;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'getAge' instead of 'setAge' for a setter method.
Using the field name directly as method name.
β Incorrect
Setter methods in Java start with 'set' followed by the field name with the first letter capitalized.
3fill in blank
hardFix the error in the setter method name for the field salary.
Java
public void [1](double salary) {
this.salary = salary;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'getSalary' instead of 'setSalary' for setter.
Capitalizing the 'S' in 'SetSalary' incorrectly.
β Incorrect
Setter methods must start with 'set' followed by the field name with the first letter capitalized, exactly as 'setSalary'.
4fill in blank
hardFill both blanks to complete the getter method for the field height.
Java
public [1] [2]() { return height; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'int' as return type when field is double.
Using field name directly as method name.
β Incorrect
Getter methods return the field type and have the name 'get' + capitalized field name.
5fill in blank
hardFill all three blanks to complete the setter method for the field weight.
Java
public [1] [2]([3] weight) { this.weight = weight; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using a return type other than void.
Incorrect method name or parameter type.
β Incorrect
Setter methods have void return type, method name 'set' + capitalized field name, and parameter type matching the field type.