To help with the JavaBat coding problems, here are a few complete code examples for similar problems for Logic, Strings, Arrays, Recursion, and the must return type X error.
This code uses an if/else structure to return either true or false.
aIsBigger() should return true if A is larger than B by 2 or more.
Another technique is to have a build up the answer in a local "result" variable which is set in various if-statements and returned on the last line:
See Java help on if boolean logic for more information.
Enclose strings in double quotes
"like this", and use + to combine them to make bigger strings. For example,
this withNo() method returns a new String with "No:" added to the front.
With a string, str.substring(i, j) returns the String that starts at index i and goes up to but not including j. The first char in the String is at index 0, so str.substring(0, 2) returns a string of the first two chars. The method str.length() returns the length of a string.
For example this twoE() method returns true if the string contains exactly two 'e' chars. The code "for (int i=0; i<str.length(); i++) { ..." is the standard way to look at each char in a String. This code uses str.charAt(i) to pull out each char, and == to compare two chars (a char is written in single quotes, like 'e').
Alternately, here is a solution which uses substring() to pull out parts of the string. Use .equals() to compare two Strings (do not use ==).
See the Java help on Java Strings and Java Loops for more information.
This pair13() method returns true if the int array contains a pair of 13's next to each other.
This new6() method makes and returns a new int array of size N that is filled with the value 6.
See the Java help on Java Arrays and Java Loops for more information.
Recursive code begins with a base case if-statement which checks for one or more cases that are so simple, that the answer can be returned immediately. That is followed by a recursive case which calls the same method with slightly smaller inputs, and then fixes up what it returns. "Smaller" inputs means at least one step towards the base case.
Here is a simple recursive method that counts the number of "A" in the given string.
This method must return a result of type XXX -- this compile error results if it appears that the method can exit without calling return. For example, this code will not compile:
In this case the compiler gets confused: what happens if both if statements are false? The correct form has a last catch-all else, so some value is always returned:
Logic
public boolean aIsBigger(int a, int b) {
if (a > b && (a - b) >= 2) {
return true;
} else {
return false;
}
}
public boolean aIsBigger(int a, int b) {
boolean result = false;
if (a > b && a-b >= 2) {
result = true;
}
// could have more if statements to set result
return result;
}
String
public String withNo(String str) {
return "No:" + str;
}
public boolean twoE(String str) {
int count = 0;
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == 'e') count++;
}
if (count == 2) return true;
else return false;
// this last if/else can be written simply as "return (count == 2);"
}
public boolean twoE(String str) {
int count = 0;
for (int i=0; i<str.length(); i++) {
String sub = str.substring(i, i+1);
if (sub.equals("e")) count++;
}
if (count == 2) return true;
else return false;
// this last if/else can be written simply as "return (count == 2);"
}
Array
public boolean pair13(int[] nums) {
int count = 0;
for (int i=0; i<(nums.length-1); i++) {
if (nums[i]==13 && nums[i+1]==13) return true;
}
return false; // if we get here, there was not a pair of 13's
// note: the i loop stops one short of the full length,
// so the code can refer to nums[i+1] in the loop.
}
public int[] new6(int n) {
int[] result = new int[n];
for (int i=0; i<result.length; i++) {
result[i] = 6;
}
return result;
}
Recursion
public int countA(String str) {
// Base case -- return simple answer
if (str.length() == 0) {
return 0;
}
// Count A right at the front
int count = 0;
if (str.substring(0, 1).equals("A")) {
count = 1;
}
// Make recursive call with the rest of string and add count.
// Note that str.substring(1) starts with char 1 and goes to the
// end of the string.
return count + countA(str.substring(1));
}
Must Return Type X
// This does not work
public boolean foo() {
if (something) {
return true;
} else if (something else) {
return false;
}
}
// This works
public boolean foo() {
if (something) {
return true;
} else {
return false;
}
}