Difference between revisions of "Calling Methods"

From CompSciWiki
Jump to: navigation, search
(random())
(I deleted Solutions Page)
 
(47 intermediate revisions by 6 users not shown)
Line 1: Line 1:
=Calling Methods=
+
{{1010Chapter|ChapterNum=3
==Introduction==
+
|Picture=Wiki_method01.jpg
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".
+
|Introduction=Calling a method is like getting a screwdriver out of a toolbox labeled "screwdrivers" and using it to solve your problem. For instance, 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 [http://java.sun.com/javase/6/docs/api/ The Java API]. Here are some predefined methods that are useful:
+
You can write your own methods as well, although this will be discussed later. For a full list of all the predefined methods, visit [http://java.sun.com/javase/6/docs/api/ The Java API]. This section demonstrates some predefined methods that are useful.
  
==String Methods==
+
|Body=
===equals()===
+
==[[String Methods|String Methods]]==
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:
+
==[[Math Methods]]==
 +
==[[JOptionPane Methods]]==
 +
==[[Calling Methods Questions|Review Questions and Exercises]]==
  
<pre>
 
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"
 
}
 
</pre>
 
  
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()===
+
|PChapterNum=2
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".
+
|PChapterLink=[[Java Fundamentals]]
 
+
|NChapterNum=4
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)
+
|NChapterLink=[[Control Structures]]
 
+
}}
==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; //
+
 
+
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);
+
 
+
The result of '''squareRoot''' is '''5'''.
+
 
+
===More Math methods and examples===
+
For a full list of all the math methods you can visit: [http://java.sun.com/javase/6/docs/api/java/lang/Math.html 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==
+
You have already seen JOptionPane being used for getting input from the user [[Input using JOptionPane|here]], but you can do more with JOptionPane.
+
===showInputDialog()===
+
There are two different basic ways of calling showInputDialog. You've already seen the first one, which is simply by calling the method and sending the message as an argument:
+
 
+
<pre>
+
String input;
+
input = JOptionPane.showInputDialog("Please enter input");
+
</pre>
+
 
+
There is also a way of sending a default value for the input. After the message argument, add a comma and then enter the default value in between quotation marks:
+
 
+
<pre>
+
String input;
+
input = JOptionPane.showInputDialog("Please enter a name", "Default Name");
+
</pre>
+
 
+
===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
+

Latest revision as of 20:31, 6 December 2011


Wiki 1010 Table of Contents

Wiki method01.jpg

Chapter 3

Calling a method is like getting a screwdriver out of a toolbox labeled "screwdrivers" and using it to solve your problem. For instance, 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, although this will be discussed later. For a full list of all the predefined methods, visit The Java API. This section demonstrates some predefined methods that are useful.

  Write a Program a Day Case Studies

String Methods

Math Methods

JOptionPane Methods

Review Questions and Exercises




Chapter 2: Java Fundamentals Table of Contents Chapter 4: Control Structures