CodeQuiz 3
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:
- greet
- convertInchesToFeet
- andOperator
- isEmptyText
- formatName
- pair
- nthEvenNumber
- both
- totalCoffeeCups
- sumOfArray
› 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
| package challenges.warmup;
import stayaway.verify.easy.LevelTwoTests;
public class EasyLevel2 {
// Run main method to verify your
// challenge method implementations
public static void main(String[] args) {
LevelTwoTests levelTwo = new LevelTwoTests();
// Comment out to isolate the method run
levelTwo.greet();
levelTwo.convertInchesToFeet();
levelTwo.andOperator();
levelTwo.isEmptyText();
levelTwo.formatName();
levelTwo.pair();
levelTwo.nthEvenNumber();
levelTwo.both();
levelTwo.totalCoffeeCups();
levelTwo.sumOfArray();
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Given the user's full name, return the greeting string.
* <p>
* EXAMPLE:
* -------------
* greet("Tom Johnson") ➞ "Hello Tom Johnson!!"
* greet("Julie Williams") ➞ "Hello Julie Williams!!"
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public String greet(String name) {
String ret = "Hello " + name + "!!";
return ret;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Given the measurement value in inches, calculate and return the equivalent
* of the measurement value in feet.
* <p>
* EXAMPLE:
* -------------
* convertInchesToFeet(324) ➞ 27
* convertInchesToFeet(12) ➞ 1
* convertInchesToFeet(36) ➞ 3
* <p>
* RULE:
* -------------
* > Do not use sout
* > If inches are under 12, return 0
* > 12 inches = 1 foot
*/
public int convertInchesToFeet(int inches) {
if (inches < 12) return 0;
return inches / 12;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Implement the functionality of java AND ( && ) operator. The && operator
* takes in two boolean data value and only return [true] if both boolean data is true.
* Otherwise, it returns false.
* <p>
* EXAMPLE:
* -------------
* andOperator(true, false) ➞ false
* andOperator(true, true) ➞ true
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public boolean andOperator(boolean first, boolean second) {
return first && second;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Given the string input, determine if the input string is empty or not. In computer science,
* the "empty string" or "empty text" is a String data with zero character length.
* <p>
* EXAMPLE:
* -------------
* isEmpty("") ➞ true
* isEmpty("ab") ➞ false
* isEmpty(" ") ➞ false <-- !! hint
* <p>
* HINT:
* ------------
* Read: https://syntaxdb.com/ref/java/string-length
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public boolean isEmptyText(String text) {
int size = text.length();
if (size == 0) return true;
return false;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Given the user's first name and last name, return a single string
* in the lowercase form of "last name,first name". Refer to the EXAMPLE section for more details.
* <p>
* <p>
* EXAMPLE:
* -------------
* formatName("John", "Doe") ➞ "doe, john"
* formatName("Mary", "Jane") ➞ "jane, mary"
* <p>
* HINT:
* -------------
* String has toLowerCase() method. Please look it up.
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public String formatName(String first, String last) {
String output = last.toLowerCase() + ", " + first.toLowerCase();
return output;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Given two integer arguments a and b, return an integer array that contains
* these two argument as its first and second element.
* <p>
* <p>
* EXAMPLE:
* -------------
* pair(1, 2) ➞ [1, 2]
* pair(51, 21) ➞ [51, 21]
* pair(512124, 215) ➞ [512124, 215]
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public int[] pair(int a, int b) {
int[] arr = {a, b};
return arr;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Given an integer n, determine and return the n-th even number.
* <p>
* <p>
* EXAMPLE:
* -------------
* even(1) ➞ 0 // 1st even number is 0
* even(2) ➞ 2 // 2nd even number is 2
* even(3) ➞ 4 // 3rd even number is 4
* even(100) ➞ 198 // 100th even number is 198
* <p>
* RULE:
* -------------
* > Do not use sout
* > even number starts with 0 and positive
*/
public int nthEvenNumber(int n) {
int evenNumCounter = 0;
int number = 0;
while (true) {
if (number % 2 == 0) evenNumCounter++;
if (evenNumCounter == n) break;
number++;
}
return number;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Given two integer numbers first and second, return [true] if both of them are:
* 1) smaller than 0 or
* 2) greater than 0 or
* 3) exactly 0
* <p>
* Otherwise, please return [false]
* <p>
* EXAMPLE:
* -------------
* both(6, 2) ➞ true
* both(0, 0) ➞ true
* both(-1, 2)➞ false
* both(0, 2) ➞ false
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public boolean both(int first, int second) {
if (first == 0 && second == 0) return true;
if (first < 0 && second < 0) return true;
if (first > 0 && second > 0) return true;
return false;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* For each of the 6 coffee cups I buy from StarDucks Coffee,
* I get a 7th cup for free. In total, I get 7 cups. Modify this method
* that takes number of cups bought by me at StarDucks Coffee
* and return the total number of cups I would get.
* <p>
* <p>
* EXAMPLE:
* -------------
* totalCoffeeCups(6) ➞ 7
* totalCoffeeCups(12) ➞ 14
* totalCoffeeCups(213) ➞ 248
* <p>
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public int totalCoffeeCups(int cups) {
if (cups < 6) return cups;
return cups + cups / 6;
}
/**
* INSTRUCTIONS: [READY]
* -------------
* Implement this method that that takes an integer array
* and returns the sum of all numbers in the array.
* <p>
* <p>
* EXAMPLE:
* -------------
* sumOfArray( [2, 7, 4] ) ➞ 13
* sumOfArray( [45, 3, 0] ) ➞ 48
* sumOfArray( [-2, 84, 23] ) ➞ 105
* <p>
* <p>
* RULE:
* -------------
* > Do not use sout
*/
public int sumOfArray(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}//end::class
|
› greet
Given the user's full name, return the greeting string as seen in the example.
Example:
| greet("Tom Johnson") ➞ "Hello Tom Johnson!!"
greet("Julie Williams") ➞ "Hello Julie Williams!!"
|
Sample Answer:
| public String greet(String name) {
String ret = "Hello " + name + "!!";
return ret;
}
|
› convertInchesToFeet
Given the measurement value in inches, calculate and return the equivalent of the measurement value in feet. Keep in mind that if the inches are under 12, return 0 and also 1 foot is 12 inches.
Example:
| convertInchesToFeet(324) ➞ 27
convertInchesToFeet(12) ➞ 1
convertInchesToFeet(36) ➞ 3
|
Sample Answer:
| public int convertInchesToFeet(int inches) {
if (inches < 12) return 0;
return inches / 12;
}
|
› andOperator
Implement the functionality of java AND ( && ) operator. The && operator takes in two boolean data value and only return true if both boolean data is true. Otherwise, it returns false.
Example:
| andOperator(true, false) ➞ false
andOperator(true, true) ➞ true
|
Sample Answer:
| public boolean andOperator(boolean first, boolean second) {
return first && second;
}
|
› isEmptyText
Given the string input, determine if the input string is empty or not. In computer science, the "empty string" or "empty text" is a String data with zero character length.
Example:
| isEmpty("") ➞ true
isEmpty("ab") ➞ false
isEmpty(" ") ➞ false
|
Sample Answer:
| public boolean isEmptyText(String text) {
int size = text.length();
if (size == 0) return true;
return false;
}
|
Given the user's first name and last name, return a single string in the lowercase form of "last name,first name". Refer to the example section for more details.
Example:
| formatName("John", "Doe") ➞ "doe, john"
formatName("Mary", "Jane") ➞ "jane, mary"
|
Sample Answer:
| public String formatName(String first, String last) {
String output = last.toLowerCase() + ", " + first.toLowerCase();
return output;
}
|
› pair
Given two integer arguments a and b, return an integer array that contains these two argument as its first and second element.
Example:
| pair(1, 2) ➞ [1, 2]
pair(51, 21) ➞ [51, 21]
pair(512124, 215) ➞ [512124, 215]
|
Sample Answer:
| public int[] pair(int a, int b) {
int[] arr = {a, b};
return arr;
}
|
› nthEvenNumber
Given an integer n, determine and return the n-th even number. Note that for this problem even number starts with 0 and it's a positive number.
Example:
| even(1) ➞ 0 // 1st even number is 0
even(2) ➞ 2 // 2nd even number is 2
even(3) ➞ 4 // 3rd even number is 4
even(100) ➞ 198 // 100th even number is 198
|
Sample Answer:
| public int nthEvenNumber(int n) {
int evenNumCounter = 0;
int number = 0;
while (true) {
if (number % 2 == 0) evenNumCounter++;
if (evenNumCounter == n) break;
number++;
}
return number;
}
|
› both
Given two integer numbers first and second, return true if both of them are:
- smaller than 0 or
- greater than 0 or
- exactly 0
Otherwise, return false to the method user.
Example:
| both(6, 2) ➞ true
both(0, 0) ➞ true
both(-1, 2)➞ false
both(0, 2) ➞ false
|
Sample Answer:
| public boolean both(int first, int second) {
if (first == 0 && second == 0) return true;
if (first < 0 && second < 0) return true;
if (first > 0 && second > 0) return true;
return false;
}
|
› totalCoffeeCups
For each of the 6 coffee cups I buy from StarDucks Coffee, I get a 7th cup for free. In total, I get 7 cups. Modify this method that takes number of cups bought by me at StarDucks Coffee and return the total number of cups I would get.
Example:
| totalCoffeeCups(6) ➞ 7
totalCoffeeCups(12) ➞ 14
totalCoffeeCups(213) ➞ 248
|
Sample Answer:
| public int totalCoffeeCups(int cups) {
if (cups < 6) return cups;
return cups + cups / 6;
}
|
› sumOfArray
Implement this method that that takes an integer array and returns the sum of all numbers in the array.
Example:
| sumOfArray( [2, 7, 4] ) ➞ 13
sumOfArray( [45, 3, 0] ) ➞ 48
sumOfArray( [-2, 84, 23] ) ➞ 105
|
Sample Answer:
| public int sumOfArray(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
|