Submit Chapter4.java with four public static methods for the solutions of Exerci
ID: 3758600 • 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.
Below is my solution but i keep getting a error message saying there is no main method. Can anybody figure out what is wrong with it.
public class Chapter4
{
public static String repl (String baseString,int num)
{
String finalString="";
for (int i=1; i<=num; i++)
finalString += baseString;
return finalString;
}
public static int pow (int base, int power)
{
int result = 1;
for(int i = 1; i <= power; i++)
result*=base;
return result;
}
public static double getGrade (int score)
{
if (score < 60)
return 0.0;
if(score < 60)
return 0.0;
else if(score >= 60 && score <= 62)
return 0.7;
else if(score > 62 && score < 95)
return 0.7 + ((score-62)*0.1);
else
return 4.0;
}
public static boolean perfectNumber(int num)
{
int sum = 0;
for(int j = 2; j<= (int)Math.sqrt(num); j++)
if(num % j == 0)
{
sum += j;
sum += num / j;
}
sum++;
if(sum == num)
return true;
else
return false; }
}
Explanation / Answer
a)
2)
public class Pow_Base {
public static void main (String args[]) {
System.out.println(pow(5,3));
}
public static int pow (int base, int times) {
int tmp = 1;
// int tmp = 0 --> wrong
for (int i = 0 ; i < times ; i++ ) {
tmp = tmp * base;
//result*=base; ---->wrong
}
return tmp;
}
}
3)
public static double getGrade(int score)
{
double grade = 0.0;
if(score < 60 && score > 0)
{
return grade = 0.0;
}
else if (score >= 60 && score <= 62)
{
return grade = 0.7;
}
else if(score >= 63 && score <= 94)
{
grade = 0.7;
for (int i = 63, i <= score; i++)
{
grade += 0.1;
}
return grade;
}
else if(score >= 95 && score <= 100)
{
return grade = 4.0;
}
else
{
throw new IllegalArgumentException("Invalid values. Scores " +
"must be between 0 and 100");
}
}
}
4)
public static void perfectNumbers(int n) {
System.out.print("Perfect numbers up to " + n + ": ");
for (int i=2; i<=n; i++) {
if (isPerfectNumber(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}
public static boolean isPerfectNumber(int n) {
int x = (int) Math.sqrt(n);
int sum = 1;
for (int i=2; i<=x; i++) {
if (n % i == 0) {
sum += i + n/i;
}
}
if (sum == n) {
return true;
}
return false;
}