Difference between revisions of "Calling Methods"

From CompSciWiki
Jump to: navigation, search
(sqrt())
(sqrt())
Line 114: Line 114:
  
 
   00  double squareRoot;
 
   00  double squareRoot;
   01  squareRoot = Math.sqrt(25);
+
   01  squareRoot = Math.sqrt(25); // math.sqrt(25)
  
 
The result of '''squareRoot''' is '''5'''.
 
The result of '''squareRoot''' is '''5'''.

Revision as of 13:48, 8 March 2007

Calling Methods

Introduction

Calling a method is like getting a screwdriver out of a toolbox labeled "screwdrivers" and then using it to solve your problem. Whenever JOptionPane.showInputDialog is called in Java, you are really telling Java to run the "tool" named showInputDialog which is located in the JOptionPane "toolbox".

You can write your own methods as well, this will be discussed later. For a full list of all the predefined methods, visit The Java API. Here are some predefined methods that are useful:

String Methods

equals()

This method compares two strings. The best way to think about it, is to think of it as a method that is applied to a string to see if it equals another. Example:

String testString;
testString = "I am test";

if testString.equals("I am test")
{
     //testString equals "I am test"
}
else
{
     //testString does not equal "I am test"
}

The above example would result in the if statement being true. It is important to note that equals() must be applied to a string variable.

equalsIgnoreCase()

This method is the same as the one above but the result will be true even if the cases are different. "I AM TEST" would be the same as "i am test".

For a full list of all the String methods you can visit: http://java.sun.com/javase/6/docs/api/java/lang/String.html (and then scroll down to the Method Summary table)

Math Methods

Math methods are very interesting. You can use them to help you in many applications that deal with numbers (e.g. banking system, lottery, cashier application, etc.). This section is introduced to help you understand how built-in math methods (in API) can be used within your code. These methods are very simple to use so there is no need to panic. You only need to call the method using the following general statement:

 Type variableName = Math.methodName(argumentList);
 
 Type         = double, float, or int.
 variableName = any name that suites your purpose.
 methodName   = round, min, sqrt, etc.
 argumentList = could be zero or more parameter(s).

Practice after each method introduced to improve your skills. You just need to know that the more you practice the method, the easier they will be.

Most used math methods (at least in COMP 1010):

round()

This method returns the argument passed (number) to the closest int.

 00  int n;
 01  n = Math.round(2.365);

The result of n is 2.

random()

This method returns a random double number between 0 and 1. It could be used to specify a range of numbers that you want your program to work with.

if you want to get a random number between 0.0 and 1.0:

00  double rand;
01  rand = Math.random();

between 1.0 and 11.0:

00  double rand;
01  rand = Math.random() * 10; // the number 10 increases the range to 10 numbers instead (0,1) range.

(Math.random() * n): n depends on the amount of numbers you require for the program. if you use n to be 15, then your range will be between 1.0 and 16.0:

00  double rand;
01  rand = Math.random() * 15;

For example, you want the numbers from 22 to 32:

00  int rand;
01  rand = (int)(Math.random() * 10) + 22; // 

The line 01 uses (int) to cast the double to int (be able to get an integer result).

you can use this method to pick a lucky number for a lottory application.

min()

This method returns the smaller of the two numbers passed to the min() method.

 00  int min1;
 01  double min2;
 02
 03  min1 = Math.min(3, 2);      // you can compare integers
 04  min2 = Math.min(1.2, 3.5);  // or you can compare doubles

The result of min1 is 2 and min2 is 1.2.

max()

This method returns the maximum number of the two numbers passed to the min() method.

 00  int max1;
 01  double max2;
 02
 03  max1 = Math.max(3, 2);      // you can compare integers
 04  max2 = Math.max(1.2, 3.5);  // or you can compare doubles

The result of min1 is 3 and min2 is 3.5.

pow()

This method (math.pow(x,n)) returns the result of x raised to the power of n.

 00  double power;
 01  power = Math.pow(2, 3);   // 2 to the power 3

The result of power is 8.

sqrt()

This method returns the square root of a positive number.

 00  double squareRoot;
 01  squareRoot = Math.sqrt(25);  // math.sqrt(25)

The result of squareRoot is 5.

More Math methods and examples

For a full list of all the math methods you can visit: API Math Methods(and then scroll down to the Method Summary table)

To see an example about some Math methods you can visit: http://www.cafeaulait.org/course/week4/40.html

JOptionPane Methods

showInputDialog()

showMessageDialog()

More JOptionPane Methods

-http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html

Review Questions and Exercises

1. Write a Java program which takes two integers as input (using JOptionPane.showInputDialog), and prints the the smaller number. For example, if the numbers are 52 and 7, it should print the following:

7