Question: 9.1 Exercise (JDK Source Code): Extract the source code of the class Math from the JDK source code ($JAVA_HOME src.zip Math.java under folder

9.1 Exercise (JDK Source Code): Extract the source code of the class Math from the JDK source code ("$JAVA_HOME" ⇒ "src.zip" ⇒ "Math.java" under folder "java.lang"). Study how constants such as E and PI are defined. Also study how methods such as abs(), max(), min(), toDegree(), etc, are written.


 9.2 Exercise Matrix: Similar to Math class, write a Matrix library that supports matrix operations (such as addition, subtraction, multiplication) via 2D arrays. The operations shall support both doubles and ints. Also write a test class to exercise all the operations programmed.

Hints:

public class Matrix {

  public static void printMatrix(int[][] m) { ...... }

  public static void printMatrix(double[][] m) { ...... }

  public static boolean haveSameDimension(int[][] m1, int[][] m2) { ...... }

  public static boolean haveSameDimension(double[][] m1, double[][] m2) { ...... }

  public static int[][] add(int[][] m1, int[][] m2) { ...... }

  public static double[][] add(double[][] m1, double[][] m2) { ...... }

  ......

}


 9.3 Exercise PrintAnimalPattern (Special Characters and Escape Sequences): Write a program called PrintAnimalPattern, which uses println() to produce this pattern:

         '__'

         (©©)

 /========/

 / || %% ||

* ||----||

  ¥¥   ¥¥

  ""   ""

Hints:

  • Use escape sequence uhhhh where hhhh are four hex digits to display Unicode characters such as ¥ and ©. ¥ is 165 (00A5H) and © is 169 (00A9H) in both ISO-8859-1 (Latin-1) and Unicode character sets.
  • Double-quote (") and black-slash () require escape sign inside a String. Single quote (') does not require escape sign.

Try: Print the same pattern using printf(). (Hints: Need to use %% to print a % in printf() because % is the suffix for format specifier.)


 9.4 Exercise PrintPatterns: Write a method to print each of the followings patterns using nested loops in a class called PrintPatterns. The program shall prompt user for the sizde of the pattern. The signatures of the methods are:

public static void printPatternX(int size) // 'X' from 'A' to ..., size is a positive integer.

# # # # # # # # # # #               #                         #

 # # # # # # # # #               # # #                     # # #

   # # # # # # #               # # # # #                 # # # # #

     # # # # #               # # # # # # #             # # # # # # #

       # # #               # # # # # # # # #         # # # # # # # # #

         #               # # # # # # # # # # #     # # # # # # # # # # #

        (a)                       (b)                # # # # # # # # #

                                                         # # # # # # #

                                                           # # # # #

                                                             # # #

                                                               #

                                                              (c)

  

1                   1 2 3 4 5 6 7 8                   1     8 7 6 5 4 3 2 1

1 2                   1 2 3 4 5 6 7                 2 1     7 6 5 4 3 2 1

1 2 3                   1 2 3 4 5 6               3 2 1     6 5 4 3 2 1

1 2 3 4                   1 2 3 4 5             4 3 2 1     5 4 3 2 1

1 2 3 4 5                   1 2 3 4           5 4 3 2 1     4 3 2 1

1 2 3 4 5 6                    1 2 3         6 5 4 3 2 1     3 2 1

1 2 3 4 5 6 7                   1 2       7 6 5 4 3 2 1     2 1

1 2 3 4 5 6 7 8                   1     8 7 6 5 4 3 2 1     1

    (d)                 (e)                   (f)               (g)

  

             1                   1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

           1 2 1                   1 2 3 4 5 6 7 6 5 4 3 2 1

         1 2 3 2 1                   1 2 3 4 5 6 5 4 3 2 1

       1 2 3 4 3 2 1                   1 2 3 4 5 4 3 2 1

     1 2 3 4 5 4 3 2 1                   1 2 3 4 3 2 1

   1 2 3 4 5 6 5 4 3 2 1                   1 2 3 2 1

 1 2 3 4 5 6 7 6 5 4 3 2 1                   1 2 1

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1                   1

            (h)                               (i)

  

1                          1     1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

1 2                      2 1     1 2 3 4 5 6 7  7 6 5 4 3 2 1

1 2 3                  3 2 1     1 2 3 4 5 6      6 5 4 3 2 1

1 2 3 4              4 3 2 1     1 2 3 4 5          5 4 3 2 1

1 2 3 4 5          5 4 3 2 1     1 2 3 4              4 3 2 1

1 2 3 4 5 6      6 5 4 3 2 1     1 2 3                  3 2 1

1 2 3 4 5 6 7  7 6 5 4 3 2 1     1 2                      2 1

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1     1                          1

            (j)                               (k)

  

             1

           2 3 2

         3 4 5 4 3

       4 5 6 7 6 5 4

     5 6 7 8 9 8 7 6 5

   6 7 8 9 0 1 0 9 8 7 6

 7 8 9 0 1 2 3 2 1 0 9 8 7 

8 9 0 1 2 3 4 5 4 3 2 1 0 9 8

            (l)


 9.5 Exercise PrintTriangles: Write a method to print each of the following patterns using nested-loops in a class called PrintTriangles. The program shall prompt user for the numRows. The signatures of the methods are:

public static void printXxxTriangle(int numRows) // Xxx is the pattern's name

                           1

                       1  2  1

                   1  2  4  2  1

               1  2  4  8  4  2  1

           1  2  4  8 16  8  4  2  1

       1  2  4  8 16 32 16  8  4  2  1

   1  2  4  8 16 32 64 32 16  8  4  2  1

1  2  4  8 16 32 64 128 64 32 16  8  4  2  1

                 (a) PowerOf2Triangle

 

1                                     1

1 1                                1  1

1 2 1                           1  2  1

1 3 3 1                      1  3  3  1

1 4 6 4 1                 1  4  6  4  1

1 5 10 10 5 1            1  5 10 10  5  1

1 6 15 20 15 6 1       1  6 15 20 15  6  1

(b) PascalTriangle1          (c) PascalTriangle2


 9.6 Exercise TrigonometricSeries: Write a method to compute sin(x) and cos(x) using the following series expansion, in a class called TrigonometricSeries. The signatures of the methods are:

public static double sin(double x, int numTerms)  // x in radians

public static double cos(double x, int numTerms)


Compare the values computed using the series with the JDK methods Math.sin(), Math.cos() at x=0, π/6, π/4, π/3, π/2 using various numbers of terms.

Hints: Do not use int to compute the factorial; as factorial of 13 is outside the int range. Avoid generating large numerator and denominator. Use double to compute the terms as:



 9.7 Exercise Exponential Series: Write a method to compute e and exp(x) using the following series expansion, in a class called TrigonometricSeries. The signatures of the methods are:

public static double exp(int numTerms)  // x in radians

public static double exp(double x, int numTerms)



 9.8 Exercise SpecialSeries: Write a method to compute the sum of the series in a class called SpecialSeries. The signature of the method is:

public static double sumOfSeries(double x, int numTerms)



 9.9 Exercise FibonacciInt (Overflow) : Write a program called FibonacciInt to list all the Fibonacci numbers, which can be expressed as an int (i.e., 32-bit signed integer in the range of [-2147483648, 2147483647]). The output shall look like:

F(0) = 1

F(1) = 1

F(2) = 2

...

F(45) = 1836311903

F(46) is out of the range of int

Hints: The maximum and minimum values of a 32-bit int are kept in constants Integer.MAX_VALUE and Integer.MIN_VALUE, respectively. Try these statements:

System.out.println(Integer.MAX_VALUE);

System.out.println(Integer.MIN_VALUE);

System.out.println(Integer.MAX_VALUE + 1);

Take note that in the third statement, Java Runtime does not flag out an overflow error, but silently wraps the number around. Hence, you cannot use F(n-1) + F(n-2) > Integer.MAX_VALUE to check for overflow. Instead, overflow occurs for F(n) if (Integer.MAX_VALUE - F(n-1)) < F(n-2) (i.e., no room for the next Fibonacci number).

Write a similar program for Tribonacci numbers.


 9.10 Exercise FactorialInt (Overflow): Write a program called Factorial1to10, to compute the factorial of n, for 1≤n≤10. Your output shall look like:

The factorial of 1 is 1

The factorial of 2 is 2

...

The factorial of 10 is 3628800

9.11 Modify your program (called FactorialInt), to list all the factorials, that can be expressed as an int (i.e., 32-bit signed integer in the range of [-2147483648, 2147483647]). Your output shall look like:

The factorial of 1 is 1

The factorial of 2 is 2

...

The factorial of 12 is 479001600

The factorial of 13 is out of range

Hints: The maximum and minimum values of a 32-bit int are kept in constants Integer.MAX_VALUE and Integer.MIN_VALUE, respectively. Try these statements:

System.out.println(Integer.MAX_VALUE);

System.out.println(Integer.MIN_VALUE);

System.out.println(Integer.MAX_VALUE + 1);

Take note that in the third statement, Java Runtime does not flag out an overflow error, but silently wraps the number around.

Hence, you cannot use F(n) * (n+1) > Integer.MAX_VALUE to check for overflow. Instead, overflow occurs for F(n+1) if (Integer.MAX_VALUE / Factorial(n)) < (n+1), i.e., no room for the next number.

Try: Modify your program again (called FactorialLong) to list all the factorial that can be expressed as a long (64-bit signed integer). The maximum value for long is kept in a constant called Long.MAX_VALUE.


 9.12 Exercise Fibonacci (Overflow): Write a program called FibonacciInt to list all the Fibonacci numbers, which can be expressed as an int (i.e., 32-bit signed integer in the range of [-2147483648, 2147483647]). The output shall look like:

F(0) = 1

F(1) = 1

F(2) = 2

...

F(45) = 1836311903

F(46) is out of the range of int

Hints: The maximum 32-bit int is kept in constant Integer.MAX_VALUE. You cannot use F(n-1) + F(n-2) > Integer.MAX_VALUE to check for overflow. Instead, overflow occurs for F(n) if (Integer.MAX_VALUE - F(n-1)) < F(n-2), i.e., no room for the next number.

Try: Write a similar program for Tribonacci numbers.


 9.13 Exercise NumberConversion: Write a method call toRadix() which converts a positive integer from one radix into another. The method has the following header:

public static String toRadix(String in, int inRadix, int outRadix) // The input and output are treated as String.

9.14 Write a program called NumberConversion, which prompts the user for an input number, an input radix, and an output radix, and display the converted number. The output shall look like:

Enter a number and radix: A1B2

Enter the input radix: 16

Enter the output radix: 2

"A1B2" in radix 16 is "1010000110110010" in radix 2.


 9.15 Exercise NumberGuess: Write a program called NumberGuess to play the number guessing game. The program shall generate a random number between 0 and 99. The player inputs his/her guess, and the program shall response with "Try higher", "Try lower" or "You got it in n trials" accordingly. For example:

java NumberGuess

Key in your guess:

50

Try higher

70

Try lower

65

Try lower

61

You got it in 4 trials!

Hints: Use Math.random() to produce a random number in double between 0.0 and (less than) 1.0. To produce an int between 0 and 99, use:

int secretNumber = (int)(Math.random()*100);

Step by Step Solution

3.48 Rating (148 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

92 public class Matrix public static void printMatrixint m for int row m for int num row Systemoutprintnum Systemoutprintln public static void printMa... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!