Difference between revisions of "Calling Methods"
Line 7: | Line 7: | ||
=[[JOptionPane Methods]]= | =[[JOptionPane Methods]]= | ||
=[[Review Questions and Exercises]]= | =[[Review Questions and Exercises]]= | ||
+ | |||
+ | ==Review Questions and Exercises== | ||
+ | ===Review Questions=== | ||
+ | ====Question 1==== | ||
+ | How do you see if two strings are identical? | ||
+ | |||
+ | ====Question 2==== | ||
+ | Can you see if two Strings are identical, regardless of the case of each letter? If so, how? | ||
+ | |||
+ | ====Question 3==== | ||
+ | How would you use the Math class to square an integer? | ||
+ | |||
+ | ====Question 4==== | ||
+ | How would you use the Math class to take the square root of an integer? | ||
+ | |||
+ | ====Question 5==== | ||
+ | How do you take user input using JOptionPane? | ||
+ | |||
+ | ====Question 6==== | ||
+ | How do you display messages using JOptionPane? | ||
+ | |||
+ | ===Exercises=== | ||
+ | ====Exercise 1==== | ||
+ | Write a Java program which takes two integers as input (using JOptionPane.showInputDialog), and prints the smaller number. For example, if the numbers are 52 and 7, it should print the following: | ||
+ | |||
+ | <pre> | ||
+ | 7 | ||
+ | </pre> | ||
+ | |||
+ | ====Exercise 2==== | ||
+ | Write a Java program which takes one integer as input (using JOptionPane.showInputDialog), and prints out the value of 2 raised to the power of that number. For example, if you input 5, it should print the following: | ||
+ | |||
+ | <pre> | ||
+ | 32 | ||
+ | </pre> | ||
+ | |||
+ | ====Exercise 3==== | ||
+ | Write a Java program which takes two integers as input (assume that the first number is smaller than the second one). Then generate and print a random number which is between the two numbers you input. For example, if you give it the numbers 6 and 14, it should generate a random number between 6 and 14 inclusive. | ||
+ | |||
+ | ===Solutions=== | ||
+ | ====Question 1==== | ||
+ | Use the String class' .equals() method. | ||
+ | |||
+ | ====Question 2==== | ||
+ | Yes, using the String class' .equalsIgnoreCase() method. | ||
+ | |||
+ | ====Question 3==== | ||
+ | Use Math.pow(). The first argument is the number you are squaring, and the second parameter is 2. | ||
+ | |||
+ | ====Question 4==== | ||
+ | Use Math.sqrt(). | ||
+ | |||
+ | ====Question 5==== | ||
+ | Use JOptionPane.showInputDialog(). | ||
+ | |||
+ | ====Question 6==== | ||
+ | Use JOptionPane.showMessageDialog(). | ||
+ | |||
+ | ====Exercise 1==== | ||
+ | <pre> | ||
+ | import javax.swing.JOptionPane; | ||
+ | |||
+ | public class Test | ||
+ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | String input; // User's input | ||
+ | int first; // First input number | ||
+ | int second; // Second input number | ||
+ | int min; // Min of first and second | ||
+ | |||
+ | // Get user input and put it into an integer | ||
+ | input = JOptionPane.showInputDialog("Please input the first number"); | ||
+ | first = Integer.parseInt(input); | ||
+ | |||
+ | // Get user input and put it into another integer | ||
+ | input = JOptionPane.showInputDialog("Please input the second number"); | ||
+ | second = Integer.parseInt(input); | ||
+ | |||
+ | // Get the smaller of the two numbers | ||
+ | min = Math.min(first, second); | ||
+ | |||
+ | // Print out the result | ||
+ | System.out.println(min); | ||
+ | |||
+ | // End the program | ||
+ | System.exit(0); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | ====Exercise 2==== | ||
+ | <pre> | ||
+ | import javax.swing.JOptionPane; | ||
+ | |||
+ | public class Test | ||
+ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | String input; // User input | ||
+ | int exponent; // Exponent we will be using | ||
+ | double result; // 2 ^ exponent | ||
+ | |||
+ | // Get exponent from user, and put it into an integer | ||
+ | input = JOptionPane.showInputDialog("Please input the exponent"); | ||
+ | exponent = Integer.parseInt(input); | ||
+ | |||
+ | // Raise 2 to the power of the input exponent | ||
+ | result = Math.pow(2, exponent); | ||
+ | |||
+ | // Print out the result | ||
+ | System.out.println(result); | ||
+ | |||
+ | // End the program | ||
+ | System.exit(0); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ====Exercise 3==== | ||
+ | <pre> | ||
+ | import javax.swing.JOptionPane; | ||
+ | |||
+ | public class Test | ||
+ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | String input; // User input | ||
+ | double low; // Lower bound for random number | ||
+ | double high; // Upper bound for random number | ||
+ | long result; // The random number | ||
+ | |||
+ | // Get lower bound from user and put it into a double | ||
+ | input = JOptionPane.showInputDialog("Please input lower bound"); | ||
+ | low = Integer.parseInt(input); | ||
+ | |||
+ | // Get upper bound from user and put it into a double | ||
+ | input = JOptionPane.showInputDialog("Please input upper bound"); | ||
+ | high = Integer.parseInt(input); | ||
+ | |||
+ | // Compute a random number between low and high inclusive | ||
+ | result = (int)( (Math.random() * (high - low + 1)) + low); | ||
+ | |||
+ | // Print out the result | ||
+ | System.out.println(result); | ||
+ | |||
+ | // End the program | ||
+ | System.exit(0); | ||
+ | } | ||
+ | } | ||
+ | </pre> |
Revision as of 15:27, 20 March 2007
Chapter #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. This section defines some predefined methods that are useful. | ||
---|---|---|
Write a Program a Day | Case Studies |
Table of Contents |
---|
String Methods
Math Methods
JOptionPane Methods
Review Questions and Exercises
Review Questions and Exercises
Review Questions
Question 1
How do you see if two strings are identical?
Question 2
Can you see if two Strings are identical, regardless of the case of each letter? If so, how?
Question 3
How would you use the Math class to square an integer?
Question 4
How would you use the Math class to take the square root of an integer?
Question 5
How do you take user input using JOptionPane?
Question 6
How do you display messages using JOptionPane?
Exercises
Exercise 1
Write a Java program which takes two integers as input (using JOptionPane.showInputDialog), and prints the smaller number. For example, if the numbers are 52 and 7, it should print the following:
7
Exercise 2
Write a Java program which takes one integer as input (using JOptionPane.showInputDialog), and prints out the value of 2 raised to the power of that number. For example, if you input 5, it should print the following:
32
Exercise 3
Write a Java program which takes two integers as input (assume that the first number is smaller than the second one). Then generate and print a random number which is between the two numbers you input. For example, if you give it the numbers 6 and 14, it should generate a random number between 6 and 14 inclusive.
Solutions
Question 1
Use the String class' .equals() method.
Question 2
Yes, using the String class' .equalsIgnoreCase() method.
Question 3
Use Math.pow(). The first argument is the number you are squaring, and the second parameter is 2.
Question 4
Use Math.sqrt().
Question 5
Use JOptionPane.showInputDialog().
Question 6
Use JOptionPane.showMessageDialog().
Exercise 1
import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { String input; // User's input int first; // First input number int second; // Second input number int min; // Min of first and second // Get user input and put it into an integer input = JOptionPane.showInputDialog("Please input the first number"); first = Integer.parseInt(input); // Get user input and put it into another integer input = JOptionPane.showInputDialog("Please input the second number"); second = Integer.parseInt(input); // Get the smaller of the two numbers min = Math.min(first, second); // Print out the result System.out.println(min); // End the program System.exit(0); } }
Exercise 2
import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { String input; // User input int exponent; // Exponent we will be using double result; // 2 ^ exponent // Get exponent from user, and put it into an integer input = JOptionPane.showInputDialog("Please input the exponent"); exponent = Integer.parseInt(input); // Raise 2 to the power of the input exponent result = Math.pow(2, exponent); // Print out the result System.out.println(result); // End the program System.exit(0); } }
Exercise 3
import javax.swing.JOptionPane; public class Test { public static void main(String[] args) { String input; // User input double low; // Lower bound for random number double high; // Upper bound for random number long result; // The random number // Get lower bound from user and put it into a double input = JOptionPane.showInputDialog("Please input lower bound"); low = Integer.parseInt(input); // Get upper bound from user and put it into a double input = JOptionPane.showInputDialog("Please input upper bound"); high = Integer.parseInt(input); // Compute a random number between low and high inclusive result = (int)( (Math.random() * (high - low + 1)) + low); // Print out the result System.out.println(result); // End the program System.exit(0); } }