Methods Continued Exercises

  < Previous  Next >
  1. Define and provide an example for each of the following terms to someone else:
    1. Mutator method
    2. Accessor method
    3. Private helper method
    4. Constructor
    5. No argument constructor
  2. Think of three real world objects. Identify what the instance data would be and what would the accessor and mutator methods would be for each one.
  3. Write a simple Java program with a simple class that has the following:
    • 1+ accessor methods
    • 1+ mutator methods
    • 1+ instance data
    Have the program create at least 2 instances of the class.
  4. Locate the following terms in your code and tell someone else what they do:
    • Reference variable
    • Object
    • Member access operator
  5. Write down the line numbers in the order that they're executed in the following code:
    public class ArgumentParameterExample{                    // Line  1
    							  // Line  2 
        public static void paramTest( int num, String msg ){  // Line  3
    	num = 42;					  // Line  4 
    	msg = "All your base are belong to us";		  // Line  5
        }							  // Line  6 
        							  // Line  7 
        public static void main(String[] args){		  // Line  8 
    	int value = 1;					  // Line  9 
    	String str = "hello, world";			  // Line 10
    							  // Line 11
    	paramTest( value, str );			  // Line 12
    							  // Line 13
    	System.out.println( value );			  // Line 14
    	System.out.println( str );			  // Line 15
        }							  // Line 16
    }                                                         // Line 17
    
  6. Discuss with someone else the difference between an argument and a parameter.
  7. The volume of a cube is defined as its width * height * length. Write the SimpleCube class to match up with the methods called in main(). Have toString() display the following:
    A WIDTH by HEIGHT by LENGTH cube has a volume of VOLUME
    where WIDTH, HEIGHT, and LENGTH are replaced by the values of instance data members and VOLUME is calculated as WIDTH * HEIGHT * LENGTH.
    /** Simple class to practice writing methods with multiple parameters
     * @author Hyrum D. Carroll
     * @version 0.3 (August 29, 2020)
     */
    
    public class CubeDriver{
        public static void main( String[] args ){
    	SimpleCube box = new SimpleCube();
    
    	int x = 3;
    	int y = 4;
    
    	box.setWidth( x );
    	box.setHeight( y );
    	box.setLength( 5 );
    
    	int volume = box.getVolume();
    	System.out.println( volume );
    
    	System.out.println( box.toString() );
        }
    }
    
  8. For each of the following pairs of methods, determine which ones are valid and why or why not:
    1. public void printDetails( int length ){
         ...
      }
      
      public void printDetails( int width ){
         ...
      }
    2. public double incrementByValue( int increase ){
         ...
      }
      
      public double incrementByValue( double increase ){
         ...
      }
    3. public int getCost( int fee ){
         ...
      }
      
      public double getCost( int fee ){
         ...
      }
    4. public double updateInvoice( double fees, double taxes, double shipping ){
         ...
      }
      
      public double updateInvoice( double shipping, double fees, double taxes ){
         ...
      }
  9. Given the following method declaration, write an overloaded method:
    public boolean withdraw( double amount, double fee ){
       ...
    }