NOTE: you cannot use the Math.max() method to answer this question. 1- Write the
ID: 3758521 • Letter: N
Question
NOTE: you cannot use the Math.max() method to answer this question.
1- Write the Java function maxOfThree:
/*
maxOfThree( int, int, int ) int
function produces (returns) the larger of the three given integers
ex1: maxOfThree( 2, 5, 1 ) 5
ex2: maxOfThree( 1, 5, 2 ) 5
ex3: maxOfThree( 2, 1, 5 ) 5
ex4: maxOfThree( 3, 3, 1 ) 3
ex5: maxOfThree( 1, 3, 1 ) 3
ex6: maxOfThree( 1, 1, 4 ) 4
ex7: maxOfThree( 10, 0, 3 ) 10
ex8: maxOfThree( 3, -5, 0 ) 3
ex9: maxOfThree( 0, -10, -2 ) 0
*/
NOTE: you cannot use the Math.max() method to answer this question.
2- Write the Java function isDivisible:
/*
isDivisible( int ) boolean
function determines whether the given integer n is divisible by any integer m,
such that 1 < m < n
ex1: isDivisible( 13 ) false
ex2: isDivisible( 10 ) true
ex3: isDivisible( 1 ) false
ex4: isDivisible( 2 ) false
ex5: isDivisible( 0 ) false
ex6: isDivisible( -1 ) false
ex7: isDivisible( -2 ) false
ex8: isDivisible( -10 ) false
ex9: isDivisible( -13 ) true
*/
3- Write the Java function isPrime:
/*
isPrime( int ) boolean
function determines whether the given integer n is prime
ex1: isPrime( 13 ) true
ex2: isPrime( 10 ) false
ex3: isPrime( 1 ) true
ex4: isPrime( 2 ) true
ex5: isPrime( 0 ) true
ex6: isPrime( -1 ) true
ex7: isPrime( -2 ) true
ex8: isPrime( -10 ) false
ex9: isPrime( -13 ) true
*/
HINT: You can use a call to the isDivisible function from question 6 to answer this question.
4- Write the Java function isLeapYear:
/*
isLeapYear( int ) -> boolean
function determines whether the given year is a leap year
ex1: isLeapYear( 2000 ) -> true
ex2: isLeapYear( 1000 ) -> false
*/
5- Write the Java function daysPerMonth:
/*
daysPerMonth( int, int ) -> int
function determines the number of days for the given month and year
ex1: daysPerMonth( 12, 2015 ) -> 31 // December 2015
ex2: daysPerMonth( 9, 2014 ) -> 30 // September 2014
ex3: daysPerMonth( 2, 2015 ) -> 28 // February 2015
ex4: daysPerMonth( 2, 2016 ) -> 29 // February 2016
*/
Explanation / Answer
public static int max(int a,int b){
return a >= b ? a : b;
}
public static int max(OfThreeint a, int b,int c){
return max(max(a,b),c);
}
public static boolean isDivisible(int n){
for (int i=2; i< n; i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static boolean isPrime(int n){
return isDivisible(n);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.