0
0
Javaprogramming~10 mins

Getter and setter methods in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AgetName
BsetName
CName
Dname
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'setName' instead of 'getName' for a getter method.
Using the field name directly as method name.
2fill in blank
medium

Complete 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'
AsetAge
BgetAge
Cage
DAge
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'getAge' instead of 'setAge' for a setter method.
Using the field name directly as method name.
3fill in blank
hard

Fix 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'
ASetSalary
BgetSalary
Csalary
DsetSalary
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'getSalary' instead of 'setSalary' for setter.
Capitalizing the 'S' in 'SetSalary' incorrectly.
4fill in blank
hard

Fill 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'
Adouble
BgetHeight
Cint
Dheight
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'int' as return type when field is double.
Using field name directly as method name.
5fill in blank
hard

Fill 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'
Avoid
BsetWeight
Cdouble
Dweight
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a return type other than void.
Incorrect method name or parameter type.