Java Reference
In-Depth Information
Self-Test Exercises
12. What is the output of the following program?
public class Exercise12
{
public static void main(String[] args)
{
System.out.println(mystery(3));
}
public static int mystery( int n)
{
if (n <= 1)
return 1;
else
return ( mystery(n
1) + n );
}
}
13. What is the output of the following program? What well-known mathematical
method is rose ?
public class Exercise13
{
public static void main(String[] args)
{
System.out.println(rose(4));
}
public static int rose( int n)
{
if (n <= 0)
return 1;
else
return ( rose(n 1) * n );
}
}
14. Redefine the method power (Display 11.3) so that it also works for negative
exponents. To do this, you also have to change the type of the value returned
to double . The method heading for the redefined version of power is as follows:
/**
Precondition: If n < 0, then x is not 0.
Returns x to the power n.
*/
public static double power( int x, int n)
Hint: x n is equal to 1/( x n ).
Search WWH ::




Custom Search