Data Structures Chapter 2 Quiz

Card Set Information

Data Structures Chapter 2 Quiz
2012-09-11 21:51:02
data structures

Pages 39-55 ch 2
Show Answers:

  1. Reference Variable
    Refer to objects
  2. Null Reference
    does not refer to anything
  3. Null Pointer Exception
    error because variable no longer refers to anything
  4. Describe the three kinds of class members we have used.  Which kinds of members were public and which were private?
    • Instance Variables - private - variable declaration
    • Constructors - public - method responsible for initializing instance variables
    • Methods - public - does computations that access the class's instance variables. Two types include accessor methods and modification methods
  5. Which kind of method often has a void return type? Why?
    Accessor methods have a return type because they are used to get information on an object. Modification methods have a void return type because they make some change to the variables but do not return information.
  6. Write a Throttle constructor with no arguments.  Should set the top position to 1 and the current position to off.
    • public Throttle()
    • {
    •      top = 1;
    •      position = 0;
    • }
  7. Write a Throttle constructor with two arguments: the total number of positions for the throttle and its initial position.
    • public Throttle( int size, int initial)
    • {
    •      if ( size <= 0 )
    •           throw new IllegalArgumentException("size<=0: " +size);
    •      if ( initial <= 0 )
    •           throw new IllegalArgumentException("initial<=0: " +initial);
    •      if ( initial > size )
    •            throw new IllegalArgumentException("initial too large: " +initial);
    •      top = size;
    •      position = initial;
    • }
  8. What is the difference between a modification method and an accessor method?
    An accessor method does not alter an object but provides information on it.  A modification method changes an objects instance variables.
  9. Add a new throttle method that will return true when the current flow is more than half of the maximum flow. Use the getFlow method.
    • public boolean isAboveHalf()
    • {
    •      return(getFlow() > 0.5);
    • }
  10. What kind of constructor does Java usually provide automatically?
    Java automatically provides a no argument constructor that sets each instance variablet o its initialization value.