5.13. Key Terms

 
[Page 159 ( continued )]

Review Questions

Sections 5.2 “5.3

5.1 What are the benefits of using a method? How do you declare a method? How do you invoke a method?
5.2 What is the return type of a main method?
5.3 Can you simplify the max method in Listing 5.1 using the conditional operator?
5.4 True of false? A call to a method with a void return type is always a statement itself, but a call to a method with a nonvoid return type is always a component of an expression.
5.5 What would be wrong with not writing a return statement in a nonvoid method? Can you have a return statement in a void method, such as the following?
   public static void   main(String[] args) {   int   i;   while   (   true   ) {  // Prompt the user to enter an integer  String intString = JOptionPane.showInputDialog(   "Enter an integer:"   );  // Convert a string into int    int   i = Integer.parseInt(intString); 

[Page 160]
   if   (i ==     )    return   ;  System.out.println(   "i = "   + i); } } 

Does the return statement in the following method cause syntax errors?

   public static void   xMethod(   double   x,   double   y) { System.out.println(x + y);    return   x + y;  } 

5.6 Define the terms parameter, argument, method signature.
5.7 Write method headers for the following methods :
  • Computing a sales commission, given the sales amount and the commission rate.

  • Printing the calendar for a month, given the month and year.

  • Computing a square root.

  • Testing whether a number is even, and returning true if it is.

  • Printing a message a specified number of times.

  • Computing the monthly payment, given the loan amount, number of years , and annual interest rate.

  • Finding the corresponding uppercase letter, given a lowercase letter.

5.8 Identify and correct the errors in the following program:
 1   public class   Test { 2   public static   method1(   int   n, m) { 3 n += m; 4 xMethod(   3.4   ); 5 } 6 7   public static int   xMethod(   int   n) { 8   if   (n >     )   return 1   ; 9   else if   (n ==     )   return 0   ; 10   else if   (n <     )   return -1   ; 11 } 12 } 

5.9 Reformat the following program according to the programming style and documentation guidelines proposed in §2.14, "Programming Style and Documentation." Use the next -line brace style.
   public class   Test {   public static double   xMethod(   double   i,   double   j) {   while   (i<j) { j ” ”; }   return   j; } } 

Section 5.5 Passing Parameters

5.10 How is an argument passed to a method? Can the argument have the same name as its parameter?
5.11 What is pass- by-value ? Show the result of the following programs:
[Page 161]
5.12 For (a) in the preceding question, show the contents of the stack just before the method max is invoked, just entering max , just before max is returned, and right after max is returned.

Section 5.6 Overloading Methods

5.13 What is method overloading? Is it possible to define two methods that have the same name but different parameter types? Is it possible to define two methods in a class that have identical method names and parameter lists but with different return value types or different modifiers?

[Page 162]
5.14 What is wrong in the following program?
   public class   Test {   public static void   method(   int   x) { }   public static int   method(   int   y) {   return   y; } } 

Section 5.8 The Scope of Local Variables

5.15 Identify and correct the errors in the following program:
 1   public class Test   { 2   public static void   main(String[] args) { 3 nPrintln(   "Welcome to Java!", 5   ); 4 } 5 6   public static void   nPrintln(String message,   int   n) { 7   int   n =   1   ; 8   for   (   int   i =     ; i < n; i++) 9 System.out.println(message); 10 } 11 } 

Section 5.9 The Math Class

5.16 True or false? The argument for trigonometric methods represents an angle in radians.
5.17 Write an expression that returns a random integer between 34 and 55 . Write an expression that returns a random integer between and 999 . Write an expression that returns a random number between 5.5 and 55.5 . Write an expression that returns a random lowercase letter.
5.18 Evaluate the following method calls:
 A.   Math.sqrt(4)   B.   Math.sin(2 * Math.PI)   C.   Math.cos(2 * Math.PI)   D.   Math.pow(2, 2)   E.   Math.log(Math.E)   F.   Math.exp(1)   G.   Math.max(2, Math.min(3, 4))   H.   Math.rint(-2.5)   I.   Math.ceil(-2.5)   J.   Math.floor(-2.5)   K.   Math.round(-2.5f)   L.   Math.round(-2.5)   M.   Math.rint(2.5)   N.   Math.ceil(2.5)   O.   Math.floor(2.5)   P.   Math.round(2.5f)   Q.   Math.round(2.5)   R.   Math.round(Math.abs(-2.5))   

Section 5.12 (Optional) Packages

5.19 What are the benefits of using packages?
5.20 If a class uses the package statement " package java.chapter5 ", where should the source code be stored, and where should the .class files be stored? How do you make the class available for use by other programs?
5.21 Why do you have to import JOptionPane but not the Math class?
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net