User Defined Methods Review Questions and Exercises

From CompSciWiki
Revision as of 22:25, 13 March 2007 by Ummisur0 (Talk | contribs)

Jump to: navigation, search

Review Questions

1. Explain the relationship between an argument and its corresponding parameter. Do not define argument or parameter.

Answer: During a method call, the calling method passes one argument to each parameter of the called method.


2. Which of the following methods completes execution first? What does this tell you about the order in which nested methods are executed?

System.out.println( JOptionPane.showInputDialog("Please enter a message:") );

Answer: JOptionPane.showInputDialog executes first, then passes its return value as an argument to System.out.println. When method calls are nested, the inner-most methods are executed first, because the outer methods need the inner methods' return values.

Exercises

1. Write a user-defined method which has an int parameter called numReps and returns a String. The String will contain a sequence.

  • If numReps is odd, the sequence is "+-".
  • If numReps is even, the sequence is "/_".

The parameter numReps determines how many times the sequence is repeated in the String. For example:

  • When numReps is 2, the pattern is "/_/_".
  • When numReps is 5, the pattern is "+-+-+-+-+-".

When you are finished, write a main method which tests your new user-defined method. Try the inputs 3, 4, and 0.

Solution:

public class PatternSequence
{
    /**
     * This method creates a String consisting of a repeated series of characters.
     *
     * @param numReps - The number of times to repeat the sequence.
     *
     * @return String result - The built String.
     */
    public static String buildSequence(int numReps)
    {
        String pattern; // Either "+-" or "/_"
        String result = ""; // The String to return

        // If numReps is even, the pattern is "/_"
        if(numReps % 2 == 0)
        {
            pattern = "/_";
        }

        // If numReps is odd, the pattern is "+-"
        else
        {
            pattern = "+-";
        }

        // numReps determines how many times the pattern is repeated
        for(int i=0; i<numReps; i++)
        {
            result += pattern;
        }

        return result;
    }

    // Main method
    public static void main(String[] args)
    {
        // Let's test this out!
        System.out.println(buildSequence(3));
        System.out.println(buildSequence(4));
        System.out.println(buildSequence(0));

    }
}


2. Write a user-defined method which calculates the area of a triangle, called getTriangleArea. The formula is:

       base x height
area = _____________

            2

All values are of the double type.

Next, write a method called printArea which accepts a base parameter and a height parameter. The method prints the base, height, and area. Within this method you will call getTriangleArea.

Finally, write a main method which tests printArea. Try the following inputs: base = 2, height = 4; base = 5, height = 3; base = 0, height = 17.

Solution:

public class TriangleArea
{
    /**
     * This method calculates the area of a triangle.
     *
     * @param base - The size of the triangle base.
     * @param height - The height of the triangle.
     *
     * @return double result - The area of the triangle.
     */
    public static double getTriangleArea(double base, double height)
    {
        double result = (base * height) / 2;

        return result;
    }


    /**
     * This method prints the base, height, and area of a triangle.
     *
     * @param base - The size of the triangle base.
     * @param height - The height of the triangle.
     *
     * @return void
     */
    public static void printArea(double base, double height)
    {
        System.out.println("Base:   " + base);
        System.out.println("Height: " + height);
        System.out.println("Area:   " + getTriangleArea(base, height));
        System.out.println();
    }

    // Main method
    public static void main(String[] args)
    {
        // Let's test this out!
        printArea(2, 4);
        printArea(5, 3);
        printArea(0, 17);
    }
}