Do not use any break statements to exit loops Do not use global variables There
ID: 3527613 • Letter: D
Question
Do not use any break statements to exit loops Do not use global variables There should be no strings, reals, etc you cannot use codes like math.log()... class Utilities { // Reverses an integer. For example, 34 returns 43, 478 returns 874, 22 returns 22, and // -63 returns -36. static int reverse( int num ) { // Returns the sum of all of the divisors. For example, 7 returns 8 and 28 returns 56. static int divisorSum( int num ) { // Decide if the integer is some power of some other integer. For example, 8, 49, and 81 // return true while 10, 28, and 2 return false. (What should 0 and 1 return?) static isPower( int num ) { // Calculate the integer log base of. Do this by repeatedly dividing by 2 and counting. // For example, 2 and 3 return 1. 4, 5, 6, and 7 return 2. 80 and 100 return 6. static intLog( int num ) { }Explanation / Answer
/*Please Rate*/
public class Utilities {
static int reverse(int num) {
int newInt = 0;
int remainder;
int flag = 0;
if (num < 0) {
num = -num;
flag = 1;
}
while (num > 0) {
remainder = num % 10;
newInt = (newInt * 10) + (remainder);
num = num / 10;
}
if (flag == 1)
newInt = -newInt;
return newInt;
}
static int divisorSum(int num) {
int divisor = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0)
divisor = divisor + i;
}
return divisor;
}
static boolean isPower(int num) {
boolean status = false;
for (int i = 2; i < num; i++) {
for (int j = 1; Math.pow(i, j) <= num; j++) {
if (Math.pow(i, j) == num)
status = true;
}
}
return status;
}
static int intLog(int num) {
int count = 0;
while (num > 1) {
num = num / 2;
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(reverse(-123));
System.out.println(divisorSum(28));
System.out.println(isPower(49));
System.out.println(intLog(100));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.