Use what you learned in problem 6 above to create a recursive function called in
ID: 3880663 • Letter: U
Question
Use what you learned in problem 6 above to create a recursive function called integerToString() that
returns a String representation of an integer n expressed in base b. For instance the function call
integerToString(100,8) would return the String “144”, which is what was printed in problem 6.
static String integerToString(int n, int b){
// your code starts here
// your code ends here
}
8. Recall the IntegerList ADT discussed in class whose states were the finite integer sequences, and whose
operations were isEmpty(), size(), get(), add(), remove(), and removeAll(). Write the
methods described below using only these six ADT operations. In other words you are writing methods
belonging to a client of IntegerList.
a. Write a static void method called swap(IntegerList L, int i, int j) that will interchange
the items currently at positions i and j of the List.
b. Write a static int method called search(IntegerList L, int x) that will perform a linear
search of L for the target x. search() will return the List index where x was found, or it will return
0 if no such index exists. (Recall List indices range from 1 to size().)
c. Write a static void method called reverse(IntegerList L) that reverses the order of the items
in L
Explanation / Answer
Please find my implementation of required method. Please report others in separate post.
public class Test {
static String integerToString(int n, int b){
// your code starts here
if(n < b)
return Integer.toString(n);
else {
int r = n%b;
return integerToString(n/b, b) + r;
}
// your code ends here
}
public static void main(String[] args) {
System.out.println(integerToString(100, 8));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.