0% found this document useful (0 votes)
10 views7 pages

Pa 6

The document outlines a practice assignment for an Introduction to Computer Programming course, focusing on recursive methods in Java. It includes exercises on various topics such as division, prime number checking, palindromes, recursion tracing, and more, with specific instructions for implementation and testing. The assignment is designed to enhance students' understanding of recursion and its applications in programming.

Uploaded by

maryam.elnahas04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Pa 6

The document outlines a practice assignment for an Introduction to Computer Programming course, focusing on recursive methods in Java. It includes exercises on various topics such as division, prime number checking, palindromes, recursion tracing, and more, with specific instructions for implementation and testing. The assignment is designed to enhance students' understanding of recursion and its applications in programming.

Uploaded by

maryam.elnahas04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

German University in Cairo

Media Engineering and Technology


Prof. Dr. Slim Abdennadher
Dr. Milad Ghantous
Dr. Mohamed Hamed

Introduction to Computer Programming, Spring Term 2023


Practice Assignment 6

To be discussed in Tutorials:

Exercise 6-1 Division


Write a recursive Java method divideRec that perform the integer division operation of two numbers. If
y is a negative number, an error message has to be displayed.

x
f (x, y) =
y

DO NOT USE THE JAVA PREDEFINED OPERATOR /.


Hint:

x 0 : if x < y
= x−y
y 1+ y : Otherwise

Write a main method to test your method.

Exercise 6-2 Prime


A prime number is an integer that cannot be divided by any integer other than one and itself. For example,
7 is prime because its only divisors are 1 and 7. The integer 8 is not prime because its divisors are 1, 2,
4, and 8.
Another way to define prime is:

prime(N) = prime(N, N-1)

prime(N, 1) = true

prime(N, D) = false if D divides N


prime(N, D-1) otherwise

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.)

Exercise 6-3 Palindrome


Write a recursive method palindrome that takes a single string as a parameter from the user and returns
whether the string is palindrome or not.

Exercise 6-4 Recursion Tracing 1


Give the following program:
public s t a t i c void mystery1 ( int a , int b ) {
i f ( a <= b ) {
int m =(a + b ) / 2 ;
System . out . p r i n t (m + " ␣ " ) ;
mystery1 ( a , m − 1 ) ;
mystery1 (m+1, b ) ;
}
}

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 ) ;
}

Exercise 6-5 MergeRec


Write a recursive method mergeRec that given two strings displays the characters of the given strings in
an alternating way. Note that the two strings could be of different length. Note that you are not allowed
to use any additional strings.

Once you execute the following main method

public static void main(String[] args) {


String a = "hlo";
String b = "el";
mergeRec(a,b);
}

the following should be displayed:


hello

public static void mergeRec(String a, String b) {

2
To be solved in Labs:

Exercise 6-6 Eliminate


Write a recursive method eliminate that takes a String and a char and deletes each occurrence of this
character from the string.

Exercise 6-7 Binomial Coefficient


The binomial coefficient nk is the number of ways of picking k unordered outcomes from n possibilities,


also known as a combination or combinatorial number.


The binomial coefficient is defined recursively as follows:

   1 : if k = 0
n
= 1 : if n = k
k  n−1
+ n−1

: otherwise
k k−1

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 ) ;
....

Exercise 6-8 Replace


Write a recursive method replace that takes two arguments: String and char and replaces each occur-
rence of the character with a ’*’

Exercise 6-9 Put At Front - Final Spring 2013


Write a recursive method putAtFront that takes two parameters, a string s and a character c. The
method returns a string with all occurrences of c placed at the front of the string and all other characters
afterwards, in the same order they appear in the input string. If c does not exist, then the output string
is the same as s. If c is at the beginning of s, and it does not appear in s any more time, then the output
string is also the same as s.
The following list illustrates 4 different calls and the correct return value from calling the method each
time.

Call: putAtFront("sce", ’c’);


Return: cse
// note how c is at the front of the returned
// string and the remaining characters afterwards.
Call: putAtFront("static", ’t’);
Return: ttsaic
// here t appears twice in input string s.
// In the returned string, both are at front,
// and the remaining afterwards.
Call: putAtFront("banana", ’a’);
Return: aaabnn
Call: putAtFront("java", ’j’);

3
Return: java
Call: putAtFront("ALL", ’L’);
Return: LLA

Extra Exercises:

Exercise 6-10 Blast Off


Write a recursive method countdown that takes a single integer as a parameter from the user and prints
the numbers from n until 1 and then prints ”Blastoff!”. If the parameter is zero, it prints only the word
”Blastoff!”
For example if the user will enter 6 then the program should output:

6
5
4
3
2
1
Blastoff!

Exercise 6-11 Power


Consider the evaluation of xn , where n is a non-negative integer. Write a recursive method powerRec to
calculate xn .
Write a main method to test your method.

Exercise 6-12 Natural Logarithm


Write a recursive method constantRec to calculate the value of the mathematical constant e which is
defined as:

1 1 1 1 1
e(n) = + + + + ... +
0! 1! 2! 3! n!

Implement first a recursive method factorial, where f actorial(n) = n!.


Write a main method to test your method.

Exercise 6-13 MultilplyRec


Write a recursive method multiplyRec that perform the multiplication of two numbers.

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

DO NOT USE THE JAVA PREDEFINED OPERATOR %.


Hint:

 0 : if x = 0
x%y = x : if x < y
(x − y)%y : Otherwise

Write a main method to test your method.

Exercise 6-15 Sum of Digits


To be solved in Labs
Write a recursive method to determine the sum of the digits of an integer. For example, the sum of digits
of 51624 is 5 + 1 + 6 + 2 + 4 = 18.

Exercise 6-16 Number of Digits


Write a recursive Java method numberDigitsRec, which given an integer, returns its number of digits.
For example the call numberDigitsRec(12312) will return 5.

Exercise 6-17 Cube numbers


Write a program that implements this definition of square() method using this definition

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

Exercise 6-18 CountRec


Write a recursive method named countRec that accepts two arguments: a String value, and a char
value. Your method is to return the total number of times the character appears inside of the string.

Exercise 6-19 Reverse


Consider reversing the characters in a string. Write a recursive Java method reverseRec that returns a
new string with the same characters of the original string, but in reversed order.
Think about the base case and recursive case:

• Base case: ReverseRec("") => ""


• Recursive case:

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.

Exercise 6-20 Recursion Tracing 2


Give the following program:
public s t a t i c void mystery ( S t r i n g p r e f i x , S t r i n g remaining , int k ) {
i f ( k == 0 ) {
System . out . p r i n t l n ( p r e f i x ) ;
return ;
}
i f ( r e m a i n i n g . l e n g t h ( ) == 0 ) return ;
mystery ( p r e f i x + r e m a i n i n g . charAt ( 0 ) , r e m a i n i n g . s u b s t r i n g ( 1 ) , k−1) ;
mystery ( p r e f i x , r e m a i n i n g . s u b s t r i n g ( 1 ) , k ) ;
}

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.

Exercise 6-21 Search


Write a recursive method search() to search for a char inside a String and returns its position inside
the String. The method should returns -1 if the char is not in the String.
Use the following main method to test your program:
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
System . out . p r i n t l n ( s e a r c h ( " example " , ’ a ’ ) ) ;
}

Hint:

• “Hello”.substring(1) returns “ello”.


• “Hello”.substring(1,4) returns “ell”.
• “Hello”.substring(0,s.length()-1) returns “Hell”.

Exercise 6-22 PerfectRec


A positive integer is said to be perfect if the sum of its factors (excluding the integer itself) is that integer.
For example, 6 is perfect, since the numbers that divide into it exactly are 1, 2, 3, and 6, and the sum of
1, 2, and 3 is itself 6.
Write a recursive method perfectRec to calculate the sum of divisors of n and use it to output whether
n is perfect or not.

Exercise 6-23 Look And Say


The look-and-say sequence is the sequence of integers beginning as follows:

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:

• 1 is read off as “one one” or 11.

• 11 is read off as “two ones” or 21.


• 21 is read off as “one two, then one one” or 1211.
• 1211 is read off as “one one, then one two, then two ones or 111221.

• 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..

Exercise 6-24 Recursion Tracing 3


Given the following method:
p u b l i c s t a t i c v o i d mystery ( i n t n ) {
i f ( n/2 == 0 ) {
System . out . p r i n t ( n ) ;
} e l s e i f ( n % 2 == 0 ) {
System . out . p r i n t ( " ( " + n + " + " ) ;
mystery ( n − 1 ) ;
System . out . p r i n t ( " ) " ) ;
} else {
System . out . p r i n t ( " ( " ) ;
mystery ( n − 1 ) ;
System . out . p r i n t ( " + " + n + " ) " ) ;
}
}

a) What does the method display for the following call:

mystery(6);

Trace your method using a stack.


b) What does the method displays for any integer?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy