User Defined Methods Solutions

From CompSciWiki
Jump to: navigation, search

Main_Page > User-Defined_Methods


Introduction

These are the solutions for the review questions for user-defined methods.





Overview

If you did not answer any of these questions successfully make sure to go back and re-read the material. If you have trouble understanding the material talk to your prof or a TA

Template loop detected: Template loop detected:

Review Questions

Question 1

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


Question 2

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.


Question 3

Any valid identifier name for variables may also be used as a method name.


Question 4

You can have as many parameters defined as you want. However, if there are too many, you should consider splitting it up into multiple methods.


Question 5

A local variable is a variable that is defined within a method. Method scope is the section of code where the local variable is visible.


Question 6

Each method has its own scope. The program treats each int separately.


Exercises

Exercise 1

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)
    {
        final String ODD_PATTERN = "+*";
        final String EVEN_PATTERN = "/_";

        String pattern; // Either "+*" or "/_"
        String result = ""; // The String to return

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

        // If numReps is odd, the pattern is "+*"
        else
        {
            pattern = ODD_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));

    }
}


Exercise 2

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)
    {
        double area = getTriangleArea(base, height);

        System.out.println("Base:   " + base);
        System.out.println("Height: " + height);
        System.out.println("Area:   " + area);
        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);
    }
}


Exercise 3

public static int findBiggest( int num1, int num2, int num3 )
{
    int biggest;

    if (( num1 > num2 ) && ( num1 > num3 ))
    {
        biggest = num1;
    }
    else if (( num2 > num1 ) && ( num2 > num3 ))
    {
        biggest = num2;
    }
    else
    {
        biggest = num3;
    }

    return biggest;
}


Exercise 4

public static double processNumbers( double dbl1, double dbl2 )
{
    double sum;            //This will store the sum of the two doubles
    double squareRoot;     //This will store the square root of sum

    sum = dbl1 + dbl2;     //Calculate the sum

    squareRoot = Math.sqrt( sum );    //Call the Math.sqrt method to calculate square root

    return squareRoot;
}


Exercise 5

public static boolean isPalindrome ( String myString )
{
    int i,j; //these will be used as letter count holders
    int myLength; // the length of myString
    
    myLength= myString.length();

    j = myLength -1;     

    // start at the begining and end of the string checking each character. 
    for (i=0; i< (myLength - 1); i++)
    {
        if (myString.charAt(i) != myString.charAt(j)) return false;
        
        j--;

    }

    return true;
}

Note: This solution does not check for spaces. While "STAR RATS" would return TRUE, "WAS IT A RAT I SAW" will return false. Re-write the program so that it accepts strings with spaces.


Exercise 6

This is x: 1
This is y: 2
This is y: 5
This is z: 3
End of program.


The difference in the y value is that the first print out of y relates to the initial condition of the main method y. The second y value is changed because of methodOne()

It is clear to see that methodTwo is VOID and doesn't have a return value. This is also obvious in the main method since there are no ints to 'catch' the return value of methodTwo.So, methodTwo can be called multiple times and it will never change the x, y, z values in the main method.


Template loop detected: Template loop detected: Template loop detected: