Pa 6
Pa 6
To be discussed in Tutorials:
x
f (x, y) =
y
prime(N, 1) = true
For example,
prime(4) = prime(4,3)
prime(4,3) = prime(4,2)
prime(4,2) = false
Another example,
1
prime(7) = prime(7,6)
prime(7,6) = prime(7,5)
prime(7,5) = prime(7,4)
prime(7,4) = prime(7,3)
prime(7,3) = prime(7,2)
prime(7,1) = true
Translate the math-like definition of prime into two Java methods that return boolean. Use the % operator
to test divisibility. Put your method into a class, write a testing class, and test your program. (Look at
Triangle.java in this assignment.)
What is the output of the main method below? Justify your answer with a tracing table.
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
int n = 6 ;
mystery1 ( 0 , n ) ;
}
2
To be solved in Labs:
Write a recursive method to calculate the binomial coefficient. Make sure that n is less than k.
Write a main method that will allow the user to enter the actual parameters of the binomial coefficient
method. Use either Scanner class or the command line arguments as input mechanism.
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
Scanner s c = new Scanner ( System . i n ) ;
....
3
Return: java
Call: putAtFront("ALL", ’L’);
Return: LLA
Extra Exercises:
6
5
4
3
2
1
Blastoff!
1 1 1 1 1
e(n) = + + + + ... +
0! 1! 2! 3! n!
f (x, y) = x ∗ y
Your method will use only the addition and subtraction operators.
Write a main method to test your method.
Hint:
x ∗ y = x + x + ... + x
| {z }
y times
4
Exercise 6-14 Modulus
Write a recursive Java method ModulusRec that perform the modulus operation of two integers. If y is a
negative number, an error message has to be displayed.
f (x, y) = x%y
N 2 = (N − 1)2 + 2N − 1
Then use your implementation to implement cube() method using the following definition, where N is an
integer entered by the user.
cube(1) = 1
cube(N) = cube(N-1) + 3(square(N)) - 3N + 1
5
reverse("ABCDE") => "E" + ReverseRec("ABCD") => ...... => "EDCBA"
Hint: In addition to the methods charAt and length, use the predefined method substring(). For
example if we have a string String s = "CSEN202", then s.substring(1) returns "SEN202", i.e. the
string s without the first character.
Write a main method to test your method.
a) What is the value returned by the following invocation? Trace your program. mystery("", "CSEN", 3)
b) What does the above method do? Give an concise verbal description of how the value returned by
mystery("", s, k) is related to the values of the parameters s and k.
Hint:
6
1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, ...
To generate a member of the sequence from the previous member, read off the digits of the previous
member, counting the number of digits in groups of the same digit. For example:
• 111221 is read off as “three ones, then two twos, then one one” or 312211.
Write a recursive Java method lookAndSay and prints the first nth terms of this sequence. Hint: you
can create a helper method that takes a String as a parameter acting as the ith term of the sequence and
returns a String representing the ith +1 term..
mystery(6);