Preview

05 - Mutator Methods

 1. Corresponding to each get method, programmers also provide a public ____ method to change the value of a private instance variable in a class.

  met

  set

  get

  let

 2. Mutator methods are _____ methods meaning that they do not return a value.

 3. Mutator methods do take a ___________, the new value for the ______________.

  parameter / instance variable

  variable name / method itself

  variable / class object

  class definition / parameter

 4. Here is an example of the Student class with a mutator method called ___________.
class Student {

   //Instance variable name
   private String name;

   /** setName sets name to newName
    *  @param newName                */
   public void setName(String newName)
   {
      name = newName;
   }

   public static void main(String[] args)
   {
      // To call a set method, use objectName.setVar(newValue)
      Student s = new Student();
      s.setName("Ayanna");
   }
  }

  static void main

  setName():

  String name;

  newName

 5. Setters have a void return type and take a new value as a parameter but the value of the instance variable must remain unchanged.

  FALSE

  TRUE

 6. Consider a class Event that keeps track of people. Which of the following method signatures could replace the missing header for the set method so it works?
public class Event
{
    //number of people at the event
    private int numOfPeople;

    /* Missing header of set method */
    {
        numOfPeople = people;
    }
}

  public void setNum(int people)

  public int setNum()

  public int setNumOfPeople(int p)

  public int getNum(int people)

 7. Mutator methods do not have to have a name with ?set? in it, although most do.

  FALSE

  TRUE

 8. Mutator methods can be any methods that change the value of an instance variable or a static variable in the class.

  FALSE

  TRUE

 9. Consider the following class definition. Which of the following is the correct reason for why the class does not compile?
public class Water
{
    private int currentTemp;

    public Water(int temp)
    {
        currentTemp = temp;
    }

    public void resetTemp()
    {
        currentTemp = newTemp;
    }
}

  The resetTemp method should have a parameter.

  The resetTemp method is missing a return type.

  The instance variable currentTemp should be public instead of private.

  The constructor should not have a parameter.

 10. Which of the following needs to be done in order for the method to work as intended. Read the context and consider the code below.
//In the Event class below, the addPeople
 method is intended to increase the value
 of the instance variable numOfPeople by 
the value of the parameter additionalPeople. 
The method does not work as intended.
What needs to be done to correct the error?
public class Event
{
    private int numOfPeople;

    public Event(int n)
    {
        numOfPeople = n;
    }

    public int addPeople(int additionalPeople) // Line 10
    {
        numOfPeople += additionalPeople; // Line 12
    }
}

  Replace line 10 with public void addPeople(int additionalPeople)

  Replace line 12 with return additionalPeople

  Replace line 12 with numOfPeople = additionalPeople;

  Replace line 10 with public addPeople (int additionalPeople)