Skip to content

CodeQuiz 2

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:

  • century
  • triangleArea
  • numberOfLegs
  • totalPoints
  • canDivideEvenly
  • returnFirstElement
  • returnLastElement



› 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
EasyLevel1.java
    package challenges.warmup;

    import stayaway.verify.easy.LevelOneTests;

    public class EasyLevel1 {

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

        // Comment out to isolate the method run
        levelOne.century();
        levelOne.triangleArea();
        levelOne.numberOfLegs();
        levelOne.totalPoints();
        levelOne.canDivideEvenly();
        levelOne.returnFirstElement();
        levelOne.returnLastElement();
    }

    /**
     * INSTRUCTIONS:
     * -------------
     * Given a year, return the century it is in.
     * The first century spans from the year 1 up to and including the year 100,
     * the second - from the year 101 up to and including the year 200, etc.
     * <p>
     * EXAMPLE:
     * -------------
     * century(1905) ➞ 20
     * century(1700) ➞ 17
     * <p>
     * RULE:
     * -------------
     * > Do not use sout
     */
    public int century(int year) {
        int result = year / 100;

        if (year % 100 == 0) return result;
        else return ++result;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify the following method so that given the base and height of a triangle,
     * it calculates the area and returns it the user.
     * <p>
     * EXAMPLE:
     * -------------
     * triangleArea(3, 2) ➞ 3
     * triangleArea(7, 4) ➞ 14
     * triangleArea(10, 10) ➞ 50
     * <p>
     * RULE:
     * -------------
     * > Do not use sout
     * > The area of a triangle is: (base * height) / 2
     */
    public int triangleArea(int base, int height) {
        int ans = (base * height) / 2;
        return ans;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify the following method so that given the number of chickens, cows, and
     * sheep at the farm, calculate and return total number of animal legs.
     * <p>
     * <p>
     * EXAMPLE:
     * -------------
     * numberOfLegs(2, 3, 5) ➞ 36
     * numberOfLegs(1, 2, 3) ➞ 22
     * numberOfLegs(5, 2, 8) ➞ 50
     * <p>
     * RULE:
     * -------------
     * > Do not use sout
     */
    public int numberOfLegs(int chickens, int cows, int sheep) {
        int ans = chickens*2 + cows*4 + sheep*4;
        return ans;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify the following method so that given the number of wins, draws and losses, calculate
     * the number of points a football team has obtained so far.
     * <p>
     * Constraints:
     * - every win  point gets 3 points
     * - every draw point get  1 point
     * - every loss point get  0 points
     * <p>
     * EXAMPLE:
     * -------------
     * totalPoints(3, 4, 2) ➞ 13
     * totalPoints(5, 0, 2) ➞ 15
     * totalPoints(0, 0, 1) ➞ 0
     * <p>
     * RULE:
     * -------------
     * > Do not use sout
     */
    public int totalPoints(int wins, int draws, int losses) {
        losses *= 0;
        return wins*3 + draws + losses;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify the following method so that given the integer a and b,
     * return [true] if integer a can be divided evenly by integer b. Return
     * [false] otherwise.
     * <p>
     * <p>
     * EXAMPLE:
     * -------------
     * canDivideEvenly(98, 7) ➞ true
     * canDivideEvenly(85, 4) ➞ false
     * <p>
     * RULE:
     * -------------
     * > Do not use sout
     */
    public boolean canDivideEvenly(int a, int b) {
        int ans = a % b;
        if(ans == 0) return true;
        else return false;
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify the following method so that given an integer array, return its
     * first element.
     * <p>
     * EXAMPLE:
     * -------------
     * getFirstValue( [1, 2, 3]     ) ➞ 1
     * getFirstValue( [80, 5, 100]  ) ➞ 80
     * getFirstValue( [-500, 0, 50] ) ➞ -500
     * <p>
     * RULE:
     * -------------
     * > Do not use sout
     */
    public int returnFirstElement(int[] arr) {
        return arr[0];
    }


    /**
     * INSTRUCTIONS:
     * -------------
     * Modify the following method so that given an integer array, return its
     * last element.
     * <p>
     * <p>
     * EXAMPLE:
     * -------------
     * returnLastElement( [1, 2, 3] ) ➞ 3
     * returnLastElement( [80, 5]   ) ➞ 5
     * returnLastElement( [-500, 0, 50, 7] ) ➞ 7
     * <p>
     * RULE:
     * -------------
     * > Do not use sout
     */
    public int returnLastElement(int[] arr) {
        int lastIdx = arr.length - 1;
        return arr[lastIdx];
    }
}//end::class



century

Given an integer user input year, return the century it is in.

Some helpful tips:

  • The 1st century spans from year 1 up to and including the year 100
  • The 2nd century spans from year 101 up to and including the year 200

Example:

century(1905)  20
century(1700)  17

Sample Answer:

1
2
3
4
5
6
public int century(int year) {
    int result = year / 100;

    if (year % 100 == 0) return result;
    else return ++result;
}



triangleArea

Given the base and height of a triangle, calculates the triangle area and return its result to the user.

Example:

1
2
3
triangleArea(3, 2)  3
triangleArea(7, 4)  14
triangleArea(10, 10)  50

Sample Answer:

1
2
3
4
public int triangleArea(int base, int height) {
    int ans = (base * height) / 2;
    return ans;
}



numberOfLegs

Given the number of chickens, cows, and sheep at the farm, calculate and return total number of animal legs.

Example:

1
2
3
numberOfLegs(2, 3, 5)  36
numberOfLegs(1, 2, 3)  22
numberOfLegs(5, 2, 8)  50

Sample Answer:

1
2
3
4
public int numberOfLegs(int chickens, int cows, int sheep) {
    int ans = chickens*2 + cows*4 + sheep*4;
    return ans;
}



totalPoints

Given the number of wins, draws and losses, calculate the number of points a football team has obtained so far.

Example:

1
2
3
totalPoints(3, 4, 2)  13
totalPoints(5, 0, 2)  15
totalPoints(0, 0, 1)  0

Sample Answer:

1
2
3
4
public int totalPoints(int wins, int draws, int losses) {
    losses *= 0;
    return wins*3 + draws + losses;
}



canDivideEvenly

Given the integer a and b, return true if integer a can be divided evenly by integer b. Return false otherwise.

Example:

canDivideEvenly(98, 7)  true
canDivideEvenly(85, 4)  false

Sample Answer:

1
2
3
4
5
public boolean canDivideEvenly(int a, int b) {
    int ans = a % b;
    if(ans == 0) return true;
    else return false;
}



returnFirstElement

Given an integer array, return its first element.

Example:

1
2
3
returnFirstElement( [1, 2, 3]     )  1
returnFirstElement( [80, 5, 100]  )  80
returnFirstElement( [-500, 0, 50] )  -500

Sample Answer:

1
2
3
public int returnFirstElement(int[] arr) {
    return arr[0];
}



returnLastElement

Given an integer array, return its last element.

Example:

1
2
3
returnLastElement( [1, 2, 3] )  3
returnLastElement( [80, 5]   )  5
returnLastElement( [-500, 0, 50, 7] )  7

Sample Answer:

1
2
3
4
public int returnLastElement(int[] arr) {
    int lastIdx = arr.length - 1;
    return arr[lastIdx];
}