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 5
Discussion 25.03.2023 - 30.03.2023
To be discussed in Tutorials:
Exercise 5-1 Fibonacci The Fibonacci numbers are defined as follows. The zeroth Fibonacci number is
0. The first Fibonacci number is 1. The second Fibonacci number is 1 + 0 = 1. The third Fibonacci number
is 1 + 1 = 2. In other words, except for the first two numbers each Fibonacci number is the sum of the two
previous numbers. Thus, Fibonacci Numbers, or “How many rabbits will you get after a year?” is given by
Fib(0) = 0
Fib(1) = 1
Fib(n) = Fib(n − 1) + Fib(n − 2)
Write a Java program Fibonacci.Java that computes the nth Fibonacci number:Write a method fib
to calculate the nth Fibonacci number. Add a main method that asks the user for n and displays the
result, i.e. the nth Fibonacci number.
For more information on the use of Fibonacci Numbers please see Dr. Ron Knott’s excellent site at
2. http://www.ee.surrey.ac.uk/Personal/R.Knott/Fibonacci/fib.html
1.
Exercise 5-2 Maximum
Given the following skeleton for your program, use the concept of overloading to define three versions of
the method max, which should work as follows:
• The first method should find the maximum of two integers and return it.
• The second method should calculate the maximum of two floating-point numbers and return it.
• The third method should compare two strings and return the one that would appear later in the dictio-
nary (lexicographical order). Hint: The method x.compareTo(y), where x and y are strings, returns
an integer greater than zero if the string x appears later than y in the dictionary.
p u b l i c c l a s s Max
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
System . o u t . p r i n t l n ( max ( 1 , 5 ) ) ;
System . o u t . p r i n t l n ( max ( 1 . 5 , 5 . 5 ) ) ;
System . o u t . p r i n t l n ( max ( " H e l l o " , " World " ) ) ;
}
}
1
Exercise 5-3 Run Length Decompression
Given a String containing numbers and uppercase characters (A-Z), write Java methods that decompresses
the String.
Input: 12W1B12W3B24W1B14W
Output: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
To be solved in Labs:
Exercise 5-4 Power
Write a program that implements the definition of power,where m and n are integers entered by the user, such
that m is the base and n is the exponent:
power(m, n) = mn
Write a main to test your method.
Exercise 5-5 Euler
Write a method Euler to calculate the value of the mathematical constant e which is defined as:
1 1 1 1
e= + + +...+
1! 2! 3! n!
Implement first the method factorial ( f actorial(n) = n!) Write a main method to test your method.
Exercise 5-6 Perfect Number
A perfect number is a positive integer that is equal to the sum of its proper positive divisors. A proper divisor
of an integer n is an integer between 1 (inclusive) and n (exclusive) that divides n with no remainder. For
example, 6 is a perfect number because 6 = 1+2+3.
Write an algorithm that prints all perfect integers that are less than or equal to a given integer n. The program
should consist of four methods:
• A method that will calculate the sum of divisors of a given integer n
• A method that will check whether a number is a perfect number
• A method that will print all perfect numbers that are less than or equal a given integer n
• The main method
Extra Exercices:
Exercise 5-7 Palindrome
Write a java program that determines whether the text the user inputs is a palindrome or not. A palindrome
is a piece of text that can be read the same way in either direction (left to right and right to left). Examples
of palindromes include words such as racecar and noon.
2
Exercise 5-8 Prime
Write a method isPrime that determines whether a number is a prime number. A number is prime if it is
divisible only by one and itself. For example 11 is a prime number and 14 is not a prime number.
Write a main method to test your method.
A Sample output would be:
Enter a number: 3
3 is prime
Exercise 5-9 Character Count
Write a method named count that accepts two arguments: a String value, and a char value. Your method
is to return the total number of times the second argument (the single character value) appears inside of the
first argument. For example:
p u b l i c c l a s s Count {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
System . o u t . p r i n t l n ( c o u n t ( " H e l l o " , ’l ’) ); // displays 2
System . o u t . p r i n t l n ( c o u n t ( " H e l l o " , ’o ’ ) ); // displays 1
System . o u t . p r i n t l n ( c o u n t ( " H e l l o " , ’H ’ ) ); // displays 1
System . o u t . p r i n t l n ( c o u n t ( " H e l l o " , ’h ’ ) ); // displays 0
}
}
Exercise 5-10 Sum of Digits
Write a java method sumOfDigits that takes a string consisting of text and non-negative numbers as input
and returns the sum of the digits of all the numbers in the string. For example for the string: ” The year has
12 months, each month has 4 weeks and the week has 7 days.”, your program should return 14, i.e 1+2+4+7
Exercise 5-11 Z-Algorithm - Midterm Spring 2013
Given a string S of length n, the Z-Algorithm produces a string Z where Z.charAt(i) is the length of
the longest substring starting from S.charAt(i) which is also a prefix of S, i.e. the maximum k such that
S.charAt(j) == S.charAt(i+j) for all 0 < j < k. Note that Z.charAt(i)=0 means that S.charAt(0)
!= S.charAt(i).
The string x is a prefix of the string w if and only if w = xy.
Write a method that takes a string as argument and returns a string according to the Z-Algorithm presented
above.
Assume that Z.charAt(0) is always equal to 0.
For example:
zFunction("ababa") –> 00301
zFunction("axbyaxba) –> 00003001
zFunction("ababababx) –> 006040200
zFunction("CSEN") –> 0000
Explanation of the first sample run: zFunction("ababa") –> 00301
3
For the first character a, the z value is 0. For the second character b, the Z value is 0 since any prefix of
"ababa" starts with a. For the third character a, the longest substring starting from the third position is
"aba" that is also a prefix of "ababa". Thus, the Z-value of a is 3. The fourth character b is having a Z-value
0. The last character a is having a Z-value 1.
Exercise 5-12 Persistent numbers - Midterm Spring 2015
1. Write a method that takes a number and returns the multiplication of its digits.
2. Persistent numbers are numbers where the sequential product of its digits eventually produces a single
digit number. For example, take the number 764. 7 * 6 * 4 = 168; 1 * 6 * 8 = 48; 4 * 8 = 32;
and finally, 3 * 2 = 6. The number of multiplication steps required to reach the single digit number
from the given number is referred to as the persistence of the starting number. Thus the persistence of
764 is 4.
Write a method that takes an integer as input and returns its persistence.
Note: You have to use the method multiplyDigits described above with the most appropriate loop.
For example:
persistence(764) –> 4
persistence(2) –> 0
persistence(23) –> 1
Exercise 5-13 Letter Mapping - Midterm Spring 2018
The international standard letter/number mapping is given below.
1. Write a method that given an uppercase letter as parameter will return a number as indicated above.
public static int getNumber(char c)
For example, if the character c has a value ’K’, the method should return 5.
Note: You have to use a switch statement
2. Write a method translate that uses the getNumber method. The method takes as input a phone
number as a string and returns a string as output. The input string may contain letters. The method
should translate a letter (uppercase or lowercase) to a digit as described above and leaves all other
characters untouched.
For example, if the method takes as input the string 1-800-Flowers, then the method should return
1-800-3569377.
If the method takes as input 1800flowers, then the method should return 18003569377
3. Write a main method that reads a string and uses the translate method to translate the input string.