Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Submit Chapter4.java with four public static methods for the solutions of Exerci

ID: 3758506 • Letter: S

Question

Submit Chapter4.java with four public static methods for the solutions of Exercises starting on page 305 of building java programs:

#2 repl (name here is repl as in replicate, 2 points always just on spellling)

Write a method called rep1 that accepts a String and a number of repetitions as parameters and returns the String concatenated that many times. For example, the call rep1("hello", 3) should return "hellohellohello". If the number of repetitions is zero or less, the method should return a empty string.

#5 pow (Use of Math Class not allowed for this one)

Write a method called pow that accepts a base and an exponent as parameters and return the base raised to the given power. For example, the call pow(3, 4) should return 3 * 3 * 3 *3 *3, or 81. Assume that the base and exponent are nonnegative.

#15 getGrade (I require some calculations here, an if-else block, or switch, with no more than 5 cases, interpolate between points)

#21B A "perfect number" is a positive integer that is the sum of all its proper factors. The first two perfect numbers are 6 and 28, since 1+2+3=6 and 1+2+4+7+14=28. Write a public static method perfectNumber that takes one integer as a parameter, and returns a boolean, true if the passed parameter is a perfect number, false otherwise. A call to perfectNumber(6); should return true, while perfectNumber(999999); returns false

Explanation / Answer

public static String concatenate(String s,int times){
       String result = "";
       for(int i = 0 ; i < times; i++)
           result += s;
       return result;
   }
   public static int pow(int base,int exp){
       int result = 1;
       for(int i = 0 ; i < exp; i++)
           result *= base;
       return result;
   }
   public static boolean perfectNumber(int number){
       int sum = 0;
       //add to sum if i is the factor of number
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
return number == sum;

   }