Difference between revisions of "User Defined Methods Review Questions and Exercises"

From CompSciWiki
Jump to: navigation, search
(Exercises)
m (Added missed closing tag.)
 
(34 intermediate revisions by 9 users not shown)
Line 1: Line 1:
=Review Questions=
+
{{Template:1010Topic
 +
|Chapter_TOC=[[User Defined Methods]]
 +
|Previous=[[Passing Arguments using Methods]]
 +
|Next=[[Arrays]]
 +
|Body=
  
1. Explain the relationship between an argument and its corresponding parameter. Do not define '''argument''' or '''parameter'''.
+
==Introduction==
 +
To familiarize yourself with creating user-defined methods, this page contains review questions and exercises found in the Program A Day section.
  
'''Answer:''' During a method call, the calling method passes one argument to each parameter of the called method.
+
=Try These Program A Day Questions=
  
 +
==User Defined Methods==
 +
# [[Print a Calendar]]
 +
# [[Print Numbers]]
 +
# [[Add Area Code]]
 +
# [[Fix Code Sample]]
 +
# [[Concatenate Strings]]
  
2. Which of the following methods completes execution first? What does this tell you about the order in which nested methods are executed?
+
==User Defined Methods Involving Arrays==
 +
# [[Passing Arrays]]
 +
# [[PrintArray Method]]
 +
# [[FillArray Method]]
 +
# [[ReverseArray Method]]
 +
# [[commissionCalculator]]
 +
# [[Cellphone Texting]]
  
<pre>
+
==Tougher Examples==
System.out.println( JOptionPane.showInputDialog("Please enter a message:") );
+
# [[Rock Paper Scissors]]
</pre>
+
# [[Student Record List With Parallel Arrays]]
 
+
# [[Top Secret]]
Answer: '''JOptionPane.showInputDialog''' executes first, then passes its return value as an argument to '''System.out.println'''.
+
# [[String Binary Search]]
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:'''
+
 
+
<pre>
+
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));
+
 
+
    }
+
}
+
 
+
</pre>
+
 
+
 
+
2. Write a user-defined method which calculates the area of a triangle, called ''getTriangleArea''. The formula is:
+
<pre>
+
      base x height
+
area = _____________
+
 
+
            2
+
</pre>
+
 
+
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:'''
+
 
+
<pre>
+
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);
+
    }
+
}
+
 
+
</pre>
+

Latest revision as of 13:40, 8 December 2011

COMP 1010 Home > User Defined Methods


Introduction

To familiarize yourself with creating user-defined methods, this page contains review questions and exercises found in the Program A Day section.

Try These Program A Day Questions

User Defined Methods

  1. Print a Calendar
  2. Print Numbers
  3. Add Area Code
  4. Fix Code Sample
  5. Concatenate Strings

User Defined Methods Involving Arrays

  1. Passing Arrays
  2. PrintArray Method
  3. FillArray Method
  4. ReverseArray Method
  5. commissionCalculator
  6. Cellphone Texting

Tougher Examples

  1. Rock Paper Scissors
  2. Student Record List With Parallel Arrays
  3. Top Secret
  4. String Binary Search
Previous Page: Passing Arguments using Methods Next Page: Arrays