Create a class called \"Methods\" with 8 methods (including the main method). Th
ID: 3873901 • Letter: C
Question
Create a class called "Methods" with 8 methods (including the main method). The class "Methods" must be public. All methods must be public and static.
Method name: average
Input: 3 floats
Output: 1 float
Purpose: returns the average of 3 numbers
Method name: max
Input: 4 floats
Output: 1 float
Purpose: returns the largest of 4 numbers
Method name: evenlyDivides
Input: 2 ints
Output: 1 boolean
Purpose: returns true if second parameter evenly divides the first; returns false otherwise
Method name: isEven
Input: 1 int
Output: 1 boolean
Purpose: returns true if parameter is even; returns false otherwise
Method name: isPrime
Input: 1 int
Output: 1 boolean
Purpose: returns true if parameter is a prime number; returns false otherwise
Method name: sumPriors
Input: 1 int
Output: 1 int
Purpose: returns the sum of all numbers between 1 and the parameter (include the parameter in the sum)
Method name: message
Input: nothing
Output: String
Purpose: returns "Done!"
Method name: main
Input: String[] args
Output: void
Purpose: test your code by printing the return of the following method calls in order. Each call should be printed on a newline. Your "print" method will be very useful here. Note: the following is NOT valid java code. It just gives you an idea of what to print.
average(1,2,3)
max(1,2,3,4)
evenlyDivides(18,2)
isEven(5)
isPrime(14)
sumPriors(6)
message()
Output (newline characters are shown as ):
Explanation / Answer
Methods.java
public class Methods {
public static void main(String[] args) {
System.out.println(average(1, 2, 3));
System.out.println(max(1, 2, 3, 4));
System.out.println(evenlyDivides(18, 2));
System.out.println(isEven(5));
System.out.println(isPrime(14));
System.out.println(sumPriors(6));
System.out.println(message());
}
public static float average(float a1, float a2, float a3) {
return (a1+a2+a3)/3;
}
public static float max(float a1, float a2, float a3, float a4) {
if(a1>a2 && a1>a3&&a1>a4) {
return a1;
} else if(a2 > a3 && a2 > a4){
return a1;
} else if(a3 > a4){
return a3;
} else {
return a4;
}
}
public static boolean evenlyDivides(int m, int n) {
return m % n == 0;
}
public static boolean isEven(int n) {
return n % 2 == 0;
}
public static boolean isPrime(int n) {
for (int f = 2; f <= n / 2; f++) {
if (n % f == 0) {
return false;
}
}
return true;
}
public static int sumPriors(int n) {
int sum = 0;
for(int i=1;i<=n;i++) {
sum += i;
}
return sum;
}
public static String message() {
return "Done!";
}
}
Output:
2.0
4.0
true
false
false
21
Done!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.