CodingBat Interlude
Practice for the fundamentals of coding in Java: logic and loops.
We are studying something easy for the two days at the end of the quarter.
-
Make an account on CodingBat.com. Do not use a valuable password.
-
Wednesday, November 1:
- Read about logical operations in Java.
- See below for a quick summary.
- Do eight problems from Logic-1 or higher.
-
Thursday, November 2:
- Read about for and while loops .
- Read about arrays which provide a context for using
for
. - See below for a quick summary.
- Do eight problems from Arrays-2.
Summary of Logical Operations
- if - else if - else
- and(
&&
), or (||
), not (!
)
public boolean someFunction (int n)
{
boolean answer = false;
if ( n > 20 ) {
answer = true;
} else {
answer = ( n < -10);
}
return answer;
}
Summary of Looping
arrayname.length
finds the length of the arrayarrayname[index]
gets the the value at the given index- for (set starting value; test to continue; what to do between steps) { … }
public int countNines (int[] nums)
{
int count = 0;
for (int k = 0; k < nums.length; k++)
{
if (nums[k] == 9)
{
count = count + 1;
}
}
return count;
}