Giu 2478 61 14620 2024-01-07T22 10 06
Giu 2478 61 14620 2024-01-07T22 10 06
6
5
4
3
2
1
Blastoff!
Solution:
import j a v a . u t i l . ∗ ;
public c l a s s Countdown {
public s t a t i c void countdown ( int n ) {
i f ( n == 0 ) {
System . out . p r i n t l n ( " B l a s t o f f ! " ) ;
} else {
System . out . p r i n t l n ( n ) ;
countdown ( n−1) ;
}
}
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 ) ;
System . out . p r i n t l n ( " P l e a s e ␣ e n t e r ␣ a ␣number : ␣ " ) ;
int i n p u t = s c . n e x t I n t ( ) ;
countdown ( i n p u t ) ;
}
}
Solution:
public s t a t i c int PowerRec ( int x , int n ) {
i f ( n == 0 )
1
return 1 ;
else {
i f ( n%2 == 0 )
return ( PowerRec ( x , n / 2 ) ∗ PowerRec ( x , n / 2 ) ) ;
else
return ( x ∗ PowerRec ( x , n−1) ) ;
}
}
1 1 1 1 1
e(n) = + + + + ... +
0! 1! 2! 3! n!
Solution:
public c l a s s ConsR {
public s t a t i c double c o n s t a n t R e c ( int n ) {
i f ( n == 0 )
return 1 . 0 ;
else
return ( 1 . 0 / f a c t ( n ) ) + c o n s t a n t R e c ( n−1) ;
}
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
2
Solution:
import j a v a . u t i l . ∗ ;
public c l a s s M u lt i p ly R e c {
public s t a t i c int M u lt i p ly R e c ( int num1 , int num2) {
i f (num2 == 0 )
return 0 ;
else
return (num1 + M ul t i pl y R ec (num1 , num2−1) ) ;
}
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 ) ;
System . out . p r i n t ( " Enter ␣ t h e ␣ f i r s t ␣num : ␣ " ) ;
int numA = s c . n e x t I n t ( ) ;
System . out . p r i n t ( " Enter ␣ t h e ␣ s e c o n d ␣num : " ) ;
int numB = s c . n e x t I n t ( ) ;
System . out . p r i n t l n ( "The␣ p r o d u c t ␣ i s ␣ " + Mu l t i pl y R ec (numA,
numB) ) ;
}
}
x
f (x, y) =
y
: if x < y
x 0
= x−y
y 1+ y : Otherwise
Solution:
public s t a t i c int d i v i d e R e c ( int x , int y ) {
i f ( y <= 0 )
return −1;
else i f (x < y)
return 0 ;
else
return 1 + d i v i d e R e c ( x−y , y ) ;
}
f (x, y) = x%y
3
Hint:
: if x = 0
0
x%y = x : if x < y
(x − y)%y : Otherwise
Solution:
public c l a s s ModulusRec {
public s t a t i c int modRec ( int x , int y ) {
i f ( x == 0 )
return 0 ;
else i f (x < y)
return x ;
else
return modRec ( x−y , y ) ;
}
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 ( modRec ( 7 , 2 ) ) ; // where 7%2=1
System . out . p r i n t l n ( modRec ( 0 , 2 ) ) ; // where 0%2=0
System . out . p r i n t l n ( modRec ( 3 , 5 ) ) ; // where 3%5=3
System . out . p r i n t l n ( modRec ( 6 , 2 ) ) ; // where 6%2=0
}
}
Solution:
import j a v a . u t i l . ∗ ;
public c l a s s SumOfDigits {
public s t a t i c int add ( int num)
{
i f (num < 1 0 )
return num ;
else
return ( ( num % 1 0 ) + add (num / 1 0 ) ) ;
}
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 ) ;
System . out . p r i n t l n ( " P l e a s e ␣ e n t e r ␣ a ␣number : ␣ " ) ;
int i n p u t = s c . n e x t I n t ( ) ;
System . out . p r i n t l n ( "The␣sum␣ o f ␣ t h e ␣ d i g i t s ␣ o f ␣ " + i n p u t + " ␣
i s ␣"
+ add ( i n p u t ) ) ;
}
}
4
Solution:
public s t a t i c int numberDigitsRec ( int x ) {
i f ( x%10 == x )
return 1 ;
else
return numberDigitsRec ( x / 1 0 ) + 1 ;
}
prime(N, 1) = true
For example,
prime(4) = prime(4,3)
prime(4,3) = prime(4,2)
prime(4,2) = false
Another example,
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.)
Solution:
import j a v a . u t i l . ∗ ;
public c l a s s Prime {
public s t a t i c boolean Prime ( int n ) {
i f ( n == 1 )
return true ;
else
return prime ( n , n−1) ;
}
5
e l s e i f ( n%d == 0 )
return f a l s e ;
e l s e return prime ( n , d−1) ;
}
cube(1) = 1
cube(N) = cube(N-1) + 3(square(N)) - 3N + 1
(N − 1)2 = N 2 − 2N + 1
Solution:
import j a v a . u t i l . ∗ ;
public c l a s s Cube {
public s t a t i c int cube ( int n ) {
i f ( n == 1 )
return n ;
else
return cube ( n−1) + ( 3 ∗ s q u a r e ( n ) ) − ( 3 ∗ n ) + 1 ;
}
public s t a t i c int s q u a r e ( int n ) {
i f ( n == 1 )
return n ;
else
return s q u a r e ( n − 1 ) + ( 2 ∗ n ) − 1 ;
}
6
Exercise 10-11 Binomial Coefficient
The binomial coefficient nk is the number of ways of picking k unordered outcomes from n possibilities,
if k = 0
1 :
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 ) ;
....
Solution:
public c l a s s Binom {
public s t a t i c long c h o o s e ( int n , int k )
{
long answer ; // Hold t h e answer
i f ( k == 0 | | n == k )
answer = 1 ;
else
answer = c h o o s e ( n−1, k ) + c h o o s e ( n−1, k−1) ;
return answer ;
}
Solution:
public c l a s s Count {
public s t a t i c int count ( S t r i n g x , char c ) {
i f ( x . l e n g t h ( ) == 0 )
return 0 ;
e l s e i f ( x . charAt ( 0 ) == c )
return count ( x . s u b s t r i n g ( 1 ) , c ) + 1 ;
7
else
return count ( x . s u b s t r i n g ( 1 ) , c) ;
}
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 ( count ( " H e l l o " , ’l ’) ); // displays 2
System . out . p r i n t l n ( count ( " H e l l o " , ’o ’ ) ); // displays 1
System . out . p r i n t l n ( count ( " H e l l o " , ’H ’ ) ); // displays 1
System . out . p r i n t l n ( count ( " H e l l o " , ’h ’ ) ); // displays 0
}
}
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.
Solution:
public c l a s s R e v e r s e {
public s t a t i c S t r i n g r e v e r s e R e c ( S t r i n g s ) {
i f ( s . l e n g t h ( ) <= 1 )
return s ; // 1 or 0 c h a r a c t e r s t r i n g i s
i t s own r e v e r s e
else
return r e v e r s e R e c ( s . s u b s t r i n g ( 1 ) ) + s . charAt ( 0 ) ;
}
public s t a t i c void main ( S t r i n g [ ] a r g s ) {
String x = (" abcdefghij ") ; // Sample s t r i n g
System . out . p r i n t l n ( r e v e r s e R e c ( x ) ) ;
}
}
Solution:
import j a v a . u t i l . Scanner ;
public c l a s s P a l i R e c {
8
Scanner s c = new Scanner ( System . i n ) ;
System . out . p r i n t l n ( " P l e a s e ␣ e n t e r ␣ a ␣word␣ t o ␣ be ␣ checked ␣ f o r ␣
palindrome " ) ;
String x = sc . nextLine ( ) ;
System . out . p r i n t l n ( p a l i n d r o m e ( x ) ) ;
}
public s t a t i c boolean p a l i n d r o m e ( S t r i n g x )
{
i f ( x . l e n g t h ( )==0 | | x . l e n g t h ( )==1 )
return true ;
i f ( x . charAt ( 0 ) == x . charAt ( x . l e n g t h ( ) −1) )
return p a l i n d r o m e ( x . s u b s t r i n g ( 1 , x . l e n g t h ( ) −1) ) ;
return f a l s e ;
}
Solution:
public c l a s s R ep l ac e {
public s t a t i c S t r i n g r e p l a c e ( S t r i n g s , char c ) {
i f ( s . l e n g t h ( ) == 0 ) {
return " " ;
} e l s e i f ( s . charAt ( 0 ) != c ) {
return s . charAt ( 0 ) + r e p l a c e ( s . s u b s t r i n g ( 1 ) , c ) ;
} else {
return "∗ " + r e p l a c e ( s . s u b s t r i n g ( 1 ) , c ) ;
}
}
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 ( r e p l a c e ( "Computer␣ S c i e n c e " , ’ e ’ ) ) ;
}
}
Solution:
public s t a t i c S t r i n g e l i m ( S t r i n g s , char c )
{
i f ( s . l e n g t h ( ) == 0 )
return " " ;
e l s e i f ( s . charAt ( 0 ) != c )
return s . charAt ( 0 ) + e l i m ( s . s u b s t r i n g ( 1 ) , c ) ;
else
return e l i m ( s . s u b s t r i n g ( 1 ) , c ) ;
9
}
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 ) ;
}
Solution:
3 1 0 2 5 4 6
a) What is the value returned by the following invocation? Trace your program. mystery("", "CSEN", 3)
Solution:
CSE
CSN
CEN
SEN
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.
Solution:
The method prints out all subsequences of s of length k.
10
Exercise 10-19 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:
Solution:
public s t a t i c int s e a r c h ( S t r i n g s , char c )
{
i f ( s . l e n g t h ( ) ==0)
return −1;
e l s e i f ( s . charAt ( s . l e n g t h ( ) −1) == c )
return s . l e n g t h ( ) − 1 ;
else
return s e a r c h ( s . s u b s t r i n g ( 0 , s . l e n g t h ( ) −1) , c ) ;
}
Solution:
11
import j a v a . u t i l . Scanner ;
public c l a s s PutAtFront {
else {
i f ( s . charAt ( s . l e n g t h ( ) − 1 ) == c ) {
return c + putAtFront ( s . s u b s t r i n g ( 0 ,
s . length ( ) − 1) , c ) ;
}
else {
return putAtFront ( s . s u b s t r i n g ( 0 ,
s . l e n g t h ( ) − 1 ) , c ) + s . charAt ( s . l e n g t h ( ) −
1) ;
}
}
}
}
Solution:
import j a v a . u t i l . Scanner ;
public c l a s s P e r f e c t R e c 2 {
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 ) ;
System . out . p r i n t l n ( " P l e a s e ␣ e n t e r ␣ ␣ a ␣number : ␣ " ) ;
int query = s c . n e x t I n t ( ) ;
i f ( sum == query ) {
System . out . p r i n t l n ( query + " ␣ i s ␣ p e r f e c t " ) ;
} else {
System . out . p r i n t l n ( query + " ␣ i s n ’ t ␣ p e r f e c t : ␣The␣sum␣ i s ␣ " +
12
sum ) ;
}
}
/∗ Another s o l u t i o n
p u b l i c s t a t i c i n t sumFactorsTo ( i n t num , i n t i n d e x ) {
i f ( i n d e x == num) {
return 0;
} else {
i n t sumAfter = sumFactorsTo (num , i n d e x +1) ;
i f ( ! ( num % i n d e x == 0) ) {
r e t u r n sumAfter ;
}
r e t u r n i n d e x+ sumAfter ;
}
}
∗/
• 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..
Solution:
13
import j a v a . u t i l . Scanner ;
public c l a s s LookAndSay {
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 ) ;
System . out . p r i n t l n ( " P l e a s e ␣ e n t e r ␣ a ␣number : " ) ;
int query = s c . n e x t I n t ( ) ;
lookAndSay ( query , query , " " ) ;
}
public s t a t i c S t r i n g h e l p e r ( S t r i n g x )
{
i f ( x . l e n g t h ( ) ==0)
return " " ;
char c = x . charAt ( 0 ) ;
int i ;
int c o u n t e r =1;
f o r ( i =1; i <x . l e n g t h ( ) ; i ++)
{
i f ( c==x . charAt ( i ) )
c o u n t e r ++;
else
break ;
}
return " " + c o u n t e r+ c+ h e l p e r ( x . s u b s t r i n g ( i , x . l e n g t h ( ) ) ) ;
14
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 + " ) " ) ;
}
}
mystery(6);
Solution:
mystery(6); -> (6 + ((4 + ((2 + 1) + 3)) + 5))
b) What does the method displays for any integer?
Solution:
The recursive method mystery that accepts an int n as a parameter and prints out the numbers 1
through n inclusive in a particular pattern that looks like a set of mathematical additions wrapped
in parentheses. The order of the numbers should begin with all of the evens in downward order,
followed by all of the odds upward from 1. Each time a number is added to the pattern, a new set
of parentheses and a + sign are added too.
Solution:
15
public static void mergeRec(String a, String b)
{
helper(a,b,0,0);
}
public static void helper (String a, String b, int i, int j)
{
if(a.length()==i && b.length()==j)
{
return;
}
if(b.length()==j)
{
System.out.print(a.charAt(i)+" ");
helper (a,b,++i,j);
}
else{
if(a.length()==i)
{
System.out.print(b.charAt(j) + " ");
helper (a,b,i,++j);
}
else
{
System.out.print(a.charAt(i) + " " + b.charAt(j) +" ");
helper(a,b,++i,++j);
}
}
}
1 5 8 2 3 4
Solution:
16
{
if(a.length==i && b.length==j)
{
return;
}
if(b.length==j)
{
System.out.print(a[i]+" ");
helper (a,b,++i,j);
}
else{
if(a.length==i)
{
System.out.print(b[j] + " ");
helper (a,b,i,++j);
}
else
{
System.out.print(a[i] + " " + b[j] +" ");
helper(a,b,++i,++j);
}
}
}
17