Pa 5
Pa 5
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.
• 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:
power(m, n) = mn
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:
Extra Exercices:
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
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
}
}
Write a method that takes a string as argument and returns a string according to the Z-Algorithm presented
above.
For example:
zFunction("ababa") –> 00301
zFunction("axbyaxba) –> 00003001
zFunction("ababababx) –> 006040200
zFunction("CSEN") –> 0000
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.
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
1. Write a method that given an uppercase letter as parameter will return a number as indicated above.
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.