I was presented with this problem Write a class TripleChecker with a static meth
ID: 3749408 • Letter: I
Question
I was presented with this problem
Write a class TripleChecker with a static method isTriple which
• Takes a single integer, and
• Returns true when the integer argument is exactly divisible by 3.
When I try to do this using this code, I get errors on lines 6 and 8. 6 says (cannot convert from int to boolean). What is my issue?
public class TripleChecker {
public static boolean isTriple(int num) {
// if number is divisible by three, "true" will be returned
int remainder = num % 3;
if (remainder = 0) { isTriple(num) == true; }
// if number is not divisible by three, "false" will be returned
else { isTriple(num) = false; }
return isTriple(num);
}
public static void main(String[] args) {
System.out.println("Begin testing");
System.out.println("Testing number: -300");
System.out.println("Expected output: true");
System.out.println("Actual output: " + isTriple(-300));
System.out.println("Testing number: -26");
System.out.println("Expected output: false");
System.out.println("Actual output: " + isTriple(-26));
System.out.println("Testing number: 0");
System.out.println("Expected output: false");
System.out.println("Actual output: " + isTriple(0));
System.out.println("Testing number: 3");
System.out.println("Expected output: true");
System.out.println("Actual output: " + isTriple(3));
System.out.println("Testing number: 25");
System.out.println("Expected output: false");
System.out.println("Actual output: " + isTriple(25));
System.out.println("Testing number: 333");
System.out.println("Expected output: true");
System.out.println("Actual output: " + isTriple(333));
System.out.println("End testing");
}
}
Explanation / Answer
public class TripleChecker { public static boolean isTriple(int num) { if(num == 0) return false; return num % 3 == 0; } public static void main(String[] args) { System.out.println("Begin testing"); System.out.println("Testing number: -300"); System.out.println("Expected output: true"); System.out.println("Actual output: " + isTriple(-300)); System.out.println("Testing number: -26"); System.out.println("Expected output: false"); System.out.println("Actual output: " + isTriple(-26)); System.out.println("Testing number: 0"); System.out.println("Expected output: false"); System.out.println("Actual output: " + isTriple(0)); System.out.println("Testing number: 3"); System.out.println("Expected output: true"); System.out.println("Actual output: " + isTriple(3)); System.out.println("Testing number: 25"); System.out.println("Expected output: false"); System.out.println("Actual output: " + isTriple(25)); System.out.println("Testing number: 333"); System.out.println("Expected output: true"); System.out.println("Actual output: " + isTriple(333)); System.out.println("End testing"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.