Calling Methods Solutions
Main_Page > Back to Chapter Topics
IntroductionThese are the solutions for the review questions and exercises from Calling Methods |
||
Contents
|
OverviewMake sure you fully understand all the solutions to these problems before continuing to the next chapter |
Template loop detected: Template loop detected:
Solutions
Review Questions
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
The Icon, message, input value and option buttons.
Question 6
Use JOptionPane.showInputDialog().
Question 7
The 4 default buttons are Yes, No, Ok, and Cancel.
Question 8
The purpose of ConfirmDialog box is to allow confirmation of user action.
Question 9
Use JOptionPane.showMessageDialog().
Question 10
Integer.parseInt(string_value) will return the integer value of string_value. The characters in string_value must all be decimal digits, except the first character which can be a minus sign.
Programming Excercises
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 int low; // Lower bound for random number int high; // Upper bound for random number long result; // The random number // Get lower bound from user and put it into an int input = JOptionPane.showInputDialog("Please input lower bound"); low = Integer.parseInt(input); // Get upper bound from user and put it into an int 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); } }