Java Reference
In-Depth Information
the corresponding week day (”Sunday”, ..., ”Saturday”) to the output
console using a
switch
conditional statement.
Exercise 3.6 (Calculating
sin
nx and
cos
nx)
Write
recursive
functions
sin(int n, double x)
and
cos(int n, double
x)
that respectively compute sin
nx
and cos
nx
using the following
trigonometric formula:
sin(
nx
)=sin
x
cos(
n
−
1)
x
+cos
x
sin(
n
−
1)
x,
cos(
nx
) = cos
x
cos(
n
−
1)
x
−
sin
x
sin(
n
−
1)
x.
(
Hint:
Consider sin 0 = 0 and cos 0 = 1 as the terminal cases.)
Exercise 3.7 (Palindrome **)
A palindrome is a word or phrase that can be read in
either
direction
like “radar” or “Was it a rat I saw.” Write an iterative function
CheckingPalindrome(String str)
that checks whether a word/phrase
stored in
str
is a palindrome or not. One shall use the
charAt(int
pos)
method of the
String
class that reports the character of type
char
located at position
pos
in the string. Checking whether a string
is a palindrome or not can also be done
recursively
as follows: A
word
s
is a palindrome if there exists a
word
w
and a
character
c
such that
s
=
cwc
and
w
is a palindrome (of smaller length). Design
a recursive function
CheckingPalindromeRec(String str, int left,
int right)
that checks whether the portion of the string
str
delimited
by the index range
left
and
right
is a palindrome or not. Test
your program by calling the function
CheckingPalindromeRec(str, 0,
str.length()-1);
Search WWH ::
Custom Search