go to previous page   go to home page   go to next page

Answer:

public void setHeight()


Mutators

A method that changes the state of an object is called a mutator, or a setter. Often the name of these methods starts with "set".

Often a mutator method checks the suggested change and does not make it if there is a problem. For example, Cones should not have negative values for height and radius. Here is the code, with space for the setHeight() method. (More methods will be added later.)


 
// file: Cone.java
//
public class Cone
{
  private double radius;  // radius of the base
  private double height;  // height of the cone
  
  public Cone( double radius, double height )
  {
    this.radius = radius;
    this.height = height;
  }
  
  public double area()
  {
    return Math.PI*radius*(radius + Math.sqrt(height*height + radius*radius) );
  }
  
  public double volume()
  {
    return Math.PI*radius*radius*height/3.0;
  }
  
   void setHeight(   )
  {
    if (  )
      this.height =  ;
  }
  
}

If the parameter is negative leave the instance variable unchanged. In a small program, or for debugging, it might be OK to write an error message. But in large programs you should avoid too many error messages in too many places.

(Note from the field: ) A company I once worked for had a program that frequently wrote out "FATAL ERROR: parameter out of bounds". Users kept calling in upset about all these fatal events. We had to tone down the error messages and write out fewer of them.


QUESTION 21:

Fill in the blanks.


go to previous page   go to home page   go to next page