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

From CompSciWiki
Jump to: navigation, search
m (Fixed hierarchy link)
Line 2: Line 2:
 
|Body=
 
|Body=
 
|Chapter_TOC=[[User-Defined_Methods|User-Defined Methods]]
 
|Chapter_TOC=[[User-Defined_Methods|User-Defined Methods]]
|Introduction=To familiarize yourself with creating user-defined methods, this page contains review questions and exercises. All the answers to the questions can be found on the chapter pages, and sample solutions are provided for each exercise.  Answers to these questions can be found [[User Defined Methods Solutions|here]]|Overview=This page contains some questions and exercises you can use to practice creating your own methods. Solutions are provided on the [[User Defined Methods Solutions|Solutions]] page.
+
|Introduction=To familiarize yourself with creating user-defined methods, this page contains review questions and exercises. All the answers to the questions can be found on the chapter pages, and sample solutions are provided for each exercise.  Answers to these questions can be found [[User Defined Methods Solutions|here]]
 +
|Overview=This page contains some questions and exercises you can use to practice creating your own methods. Solutions are provided on the [[User Defined Methods Solutions|Solutions]] page.
 
}}
 
}}
 
==Review Questions==
 
==Review Questions==

Revision as of 12:05, 1 December 2011

COMP 1010 Home > User-Defined Methods


Introduction

To familiarize yourself with creating user-defined methods, this page contains review questions and exercises. All the answers to the questions can be found on the chapter pages, and sample solutions are provided for each exercise. Answers to these questions can be found here

   

Review Questions

Question 1

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


Question 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:") );


Question 3

What are the valid names that a method name can have?


Question 4

How many parameters can a method have?


Question 5

What is a local variable, and how does it relate to method scope?


Question 6

If the main method and a user-defined method have an int by the same name, how does the program know the difference between them?


Question 7

What is the purpose of the main method in a Java program?


Exercises

Exercise 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.


Exercise 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.


Exercise 3

Identify and correct the problems found in this method:

public static void FindBiggest( num1, num2, num3 )
    if (( num1 > num2 ) && ( num1 > num3 ))
    {
        return num1;
    }
    else if (( num2 > num1 ) && ( num2 > num3 ))
    {
        return num2;
    }
    else
    {
        return num3;
    }


Exercise 4

Write a method called processNumbers which does the following:

  1. Takes two doubles, dbl1 and dbl2, as its parameters.
  2. Calculate the sum of the two doubles, and store it in a variable called sum.
  3. Calls the Math.sqrt function to get the square root of the sum, and return it.


Exercise 5

A palindrome is a string that reads the same, forwards and backwards. Examples of palindromes:

  1. STAR RATS
  2. WAS IT A RAT I SAW
  3. I PREFER PI
  4. STAR COMEDY BY DEMOCRATS

Write a method called isPalindrome which does the following:

  1. Takes a string as it's only parameter
  2. Uses a loop to check is the letters are the same at each ends
  3. uses the charAt() function to compare chars.


Exercise 6

Review the code below. What does it print out? Explain your answers.


public class myClass 
{
	public static void main(String[] args)
	{
		int x,y,z;
		
		x = 1;
		y = 2;
		z = 3;
		
		methodTwo();
		
		System.out.println("This is x: " + x);
		
		System.out.println("This is y:" + y);
		
		y = methodOne();
		
		System.out.println("This is y:" + y);
		
		methodTwo();
		
		System.out.println("This is z:" + z);
		
		System.out.println("End of program.");
	}
  
  
	public static int methodOne()
	{
		int x,y,z;
		
		x = 4;
		y = 5;
		z = 6;
		
		return y;
	}
	
	public static void methodTwo()
	{
		int x,y,z;
		
		x = 7;
		y = 8;
		z = 9;
		
	}
 
}


Exercise 7

Write a method which prints the contents of the array on a sigle line and calculates the average of the numbers in the array as given below. The array is as follows: int[] array = {3,12,18,37,169, 55,93,48}


public static void method()
{
	int index, sum = 0, avg;
		
	for(index=0; index < array.length; index++){
              System.out.println(index + ": " + array[index]);
        }//for
		
	for(index=0; index < array.length; index++){
              sum = sum + array[index]);
        }//for 
        
        avg = sum/array.length;
        
        System.out.println("The average of the numbers in the array is : " + avg);   
}//main method