Skip to content

CodeQuiz 1

In this code note, we will explore the sample answers to the seven methods implementation task from codquiz project. Make sure to watch and try out the code for each of the challenges.

List of challenge methods:

  • isEqualNumber
  • calculateAge
  • lessThanHundred
  • covertAgeToDays
  • isItTen
  • divisibleBy5
  • nextOf



› Solution

Please watch the following explainer video for each method's implementation detail:

Please expand to see instructor's implementation of above methods:

Expand to see the code
ReturnSomethingPart2
package challenges.warmup;

import stayaway.verify.WarmUpPart2Tests;
import stayaway.verify.WarmUpTests;

import java.io.FileOutputStream;
import java.io.PrintStream;

public class ReturnSomethingPart2 {


    // Run main method to verify your
    // challenge method implementations
    public static void main(String[] args) {
        WarmUpPart2Tests run = new WarmUpPart2Tests();

        run.isEqualNumber();
        run.calculateAge();
        run.lessThanHundred();
        run.covertAgeToDays();
        run.isItTen();
        run.divisibleBy5();
        run.nextOf();
    }

    /**
     * INSTRUCTIONS:
     * -------------
     * Modify this method so that given the two numbers, return [true] if they are equal,
     * return [false] boolean value if they are not equal.
     *
     * EXAMPLE:
     *  isEqualNumber(3, 4) -> false
     *  isEqualNumber(5, 5) -> true
     *  isEqualNumber(12, 22) -> false
     *
     * RULES:
     *  > Do not use sout inside this method
     */
    public boolean isEqualNumber(int first, int second) {
        if(first == second) return true;
        else return false;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify this method so that given the user's birth year,
     * return their current age.
     *
     * EXAMPLE:
     *  calculateAge(1990) -> 32
     *  calculateAge(2000) -> 22
     *  calculateAge(1967) -> 55
     *
     * RULES:
     *  > Do not use sout inside this method
     *  > birthYear cannot bigger than 2022
     */
    public int calculateAge(int birthYear) {
        int currentYear = 2022;
        int currentAge = currentYear - birthYear;
        return currentAge;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify this method so that given two numbers, return [true] if the sum of both
     * numbers is less than 100. Otherwise, return [false].
     *
     * EXAMPLE:
     *  lessThanHundred(22, 15) -> false
     *  lessThanHundred(83, 34) -> false
     *  lessThanHundred(3, 77)  -> true
     *
     * RULES:
     *  > Do not use sout inside this method
     */
    public boolean lessThanHundred(int first, int second) {
        int sum = first + second;

        if(sum < 100) return true;
        else return false;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify this method so that given the user's age in years, it
     * calculates and returns the age in days.
     *
     * EXAMPLE:
     *  covertAgeToDays(65) -> 23725
     *  covertAgeToDays(0)  -> 0
     *  covertAgeToDays(20) -> 7300
     *
     * RULES:
     *  > Do not use sout inside this method
     *  > parameter age is guaranteed to be positive
     */
    public int covertAgeToDays(int age) {
        int daysInYear = 365;
        int totalDays = age * daysInYear;
        return totalDays;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify this method so that given two integer, it returns if one
     * of them is 10 or if their sum is equal to 10.
     *
     * EXAMPLE:
     *  isItTen(9,10) -> true
     *  isItTen(8,8)  -> false
     *  isItTen(10,2) -> true
     *  isItTen(5,5)  -> true
     *
     * RULES:
     *  > Do not use sout inside this method
     */
    public boolean isItTen(int a, int b) {
        int sum = a + b;
        boolean weHaveTen = sum == 10 || a == 10 || b == 10 ;

        if(weHaveTen) return true;
        else return false;
    }

    /**
     * INSTRUCTIONS:
     * -------------
     * Modify this method so that given a number, it returns [true] if
     * the given number is divisible by 5, otherwise this method returns [false]
     *
     * EXAMPLE:
     * --------
     *  divisibleBy5(-55) -> true
     *  divisibleBy5( 15) -> true
     *  divisibleBy5( 37) -> false
     *
     * RULES:
     * ------
     *  > Do not use sout inside this method
     *
     */
    public boolean divisibleBy5(int number) {
        int result = number % 5;

        if(result == 0){
            return true;
        }else {
            return false;
        }
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify this method so that given a number, it returns the next number.
     *
     * EXAMPLE:
     * --------
     *  nextOf(0)  ->  1
     *  nextOf(9)  ->  10
     *  nextOf(-3) -> -2
     *
     * RULES:
     * ------
     *  > Do not use sout inside this method
     */
    public int nextOf(int number) {
        int nextNumber = ++number;
        return nextNumber;
    }

}//end::class



isEqualNumber

Given two integer numbers first and second, return true if they are equal. Return false otherwiser.

Usage Example:

1
2
3
isEqualNumber(3, 4)   // false
isEqualNumber(5, 5)   // true
sEqualNumber(12, 22)  // false

Sample Answer:

1
2
3
4
public boolean isEqualNumber(int first, int second) {
    if(first == second) return true;
    else return false;
}



calculateAge

Given one integer number birthYear that represents a user's year of birth, calculate and return their age.

Example:

1
2
3
calculateAge(1990)  // 32
calculateAge(2000)  // 22
calculateAge(1967)  // 55

Sample Answer:

1
2
3
4
5
public int calculateAge(int birthYear) {
    int currentYear = 2022;
    int currentAge = currentYear - birthYear;
    return currentAge;
}



lessThanHundred

Given two numbers first and second, return true if the sum of both number is less than 100. Otherwise, return false boolean value.

Example:

1
2
3
lessThanHundred(22, 15)  // false
lessThanHundred(83, 34)  // false
lessThanHundred(3, 77)   // true

Sample Answer:

1
2
3
4
5
6
public boolean lessThanHundred(int first, int second) {
    int sum = first + second;

    if(sum < 100) return true;
    else return false;
}



covertAgeToDays

Given a number age that represents user's current age, calculate and return the age in days. Note that there are 365 dats in a year.

Example:

1
2
3
covertAgeToDays(65)  // 23725
covertAgeToDays(0)   // 0
covertAgeToDays(20)  // 7300

Sample Answer:

1
2
3
4
5
public int covertAgeToDays(int age) {
    int daysInYear = 365;
    int totalDays = age * daysInYear;
    return totalDays;
}



isItTen

Given two integer numbers a and b, return true if one of them is equal to 10 or their sum is equal to 10.

Example:

1
2
3
4
isItTen(9,10)  // true
isItTen(8,8)   // false
isItTen(10,2)  // true
isItTen(5,5)   // true

Sample Answer:

1
2
3
4
5
6
7
public boolean isItTen(int a, int b) {
    int sum = a + b;
    boolean weHaveTen = sum == 10 || a == 10 || b == 10 ;

    if(weHaveTen) return true;
    else return false;
}



divisibleBy5

Given an integer number number, return true if it is divisible by 5, otherwise return false to the user. Note that if number is divisible by another number, their modulus will return zero.

Example:

1
2
3
divisibleBy5(-55)  // true
divisibleBy5( 15)  // true
divisibleBy5( 37)  // false

Sample Answer:

1
2
3
4
5
6
7
8
9
public boolean divisibleBy5(int number) {
    int result = number % 5;

    if(result == 0){
        return true;
    }else {
        return false;
    }
}



nextOf

Given an integer number, calculate and return the next number.

Example:

1
2
3
nextOf(0)  // 1
nextOf(9)  // 10
nextOf(-3) // -2

Sample Answer:

1
2
3
4
public int nextOf(int number) {
    int nextNumber = ++number;
    return nextNumber;
}