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

NOTE: you cannot use the Math.max() method to answer this question. Write the Ja

ID: 3758434 • Letter: N

Question

NOTE: you cannot use the Math.max() method to answer this question.

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.

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
*/

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.

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
*/

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 maxOfThree(int first, int second, int third)
{
if(first > second) //If first number is greater than second, now either first or third could be greatest.
{
if(first > third) //If first is also greater than third.
return first;
else               //If it is not greater than third.
return third;
}
else               //If second number is greater than first, now either second or third could be greaters.
{
if(second > third) //If second is also greater than third.
return second;
else               //If it is not greater than third.
return third;
}
}

public static boolean isDivisible(int number)
{
number = Math.abs(number);       //If negative number, convert it to positive.
for(int i = 2; i < number; i++) //For all numbers between 2 and number-1
if(number % i == 0)           //If any value divides number exactly.
return true;                   //Conclude that number is divisible.
return false;                     //If all values exhaust, conclude that number is not divisbile.
}

public static boolean isPrime(int number)
{
number = Math.abs(number);       //If negative number, convert it to positive.
if(isDivisible(number))       //If the number has factors.
return false;                   //Conclude the number is not prime.
else                           //If not.
return true;                    //Conclude the number is prime.
}
  
public static boolean isLeapYear(int year)
{
if(year % 100 == 0)
if(year % 400 == 0)
return true;
else
return false;
else
if(year % 4 == 0)
return true;
else
return false;
}
  
public static int daysPerMonth(int month, int year)
{
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;
else if(month == 4 || month == 6 || month == 8 || month == 10 || month == 12)
return 30;
else
if(isLeapYear(year))
return 29;
else
return 28;   
}