Java Daily A

Programming: if, for

Quick recall / teaching episode.

if statement

if (n < 10) {
    small = true;
} else {
    small = false;
}

for statement

for (int k=0; k<10; k++) {
    // body
}

The loop variable cannot be used after the loop is over.

Exerises

  1. How many of the inputs are true?

     public static int truec (bool a, bool b, bool c)
    
  2. How many numbers are there in the set {a, a+1, ..., b}?

     public static int hmn (int a, int b)
    

    2a. Use math. 2b. Use a for loop, no math.

  3. Create a pattern with the numbers 1..n followed by the separator.

     public static String patc (int n, String sep)
    

    Examples:

     patc(3, "...") => "1...2...3..."
     patc(4, "x") => "1x2x3x4x"
     patc(-2,"oops") =>  ""
    
  4. How many multiples of m are in the range [start..end]. Instead of a formula that just gives the answer, practice Java coding.

     public static int mc (int start, int end, int m)
    
  5. Give x+5 if b is true, otherwise x-5. Except if y is more than 100, reverse the meaning of b.

     public static int ch (int x, bol b, int y)