Cornelius Eanes's Programming Portal
Assignments (For Loops Pt. 1)
[78] Counting with a For Loop

Goal: Learn what a For Loop is and what it's different parts do.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Counting with a For Loop
/// File Name: ForLoopCounting.java
/// Date Finished: Feb 09, 2016

import java.util.Scanner;

public class ForLoopCounting {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String message;
        int iterations = 10;

        System.out.println("Type in a message and I'll print it " + iterations + " times.");
        System.out.print("> ");
        message = input.nextLine();

        for (int i = 2; i <= iterations; i = i + 2) {
            System.out.println(i + ". " + message);
        }

        // The first part of the for-loop is ran only once, at the start.
        // The second part is ran each time the loop begins. If it evaluates to true, the loop begins.
        // The third part is ran at the end of each loop.

    }

}

Output

[79] Ten Times

Goal: Print something exactly 10 times.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Ten Times
/// File Name: TenTimes.java
/// Date Finished: Feb 09, 2016

public class TenTimes {

    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(i + ". Mr Davis is cool.");
        }
    }

}

Output

[80] Counting Machine

Goal: Integrate user input into a For Loop.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Counting Machine
/// File Name: CountingMachine.java
/// Date Finished: Feb 10, 2016

import java.util.Scanner;

public class CountingMachine {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int iterations;

        System.out.println("Count to how many numbers?");
        System.out.print("> ");
        iterations = scanner.nextByte();

        for (int i = 0; i < iterations; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

    }

}

Output

[81] Counting Machine Revisited

Goal: Have the user control the For Loop.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Counting Machine Revisited
/// File Name: CountingMachine2.java
/// Date Finished: Feb 10, 2016

import java.util.Scanner;

public class CountingMachine2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int start, end, incr;

        System.out.print("Count from : ");
        start = input.nextByte();

        System.out.print("Count until: ");
        end = input.nextInt();

        System.out.print("Count by   : ");
        incr = input.nextInt();

        // some super hacker-proof capabilities
        long t1 = System.currentTimeMillis();
        for (int i = start; i < end; i += incr) {
            if (System.currentTimeMillis() - t1 > 1000) {
                System.out.println();
                System.out.println("Infinite loop detected!");
                break;
            }
            System.out.print(i + " ");
        }
        System.out.println();

    }

}

Output

[82] Counting by Halves

Goal: Learn how to use more than integers in For Loops.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Counting by Halves
/// File Name: CountingByHalves.java
/// Date Finished: Feb 10, 2016

public class CountingByHalves {

    public static void main(String[] args) {

        System.out.println(" x\n----------");
        for (double x = -10; x < 10; x += 0.5) {
            System.out.println(x);
        }

    }

}

Output

[83] Xs and Ys

Goal: Visualize a table of values with a For Loop.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Xs and Ys
/// File Name: XsAndYs.java
/// Date Finished: Feb 10, 2016

public class XsAndYs {

    public static void main(String[] args) {

        System.out.println(" x      y\n--------------------");
        for (double x = -10; x < 10; x += 0.5) {
            double y = x * x;
            System.out.println(x + " \t " + y);
        }

    }

}

Output

[84] Noticing Even Numbers

Goal: Loop through some values and indicate which ones are even.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Noticing Even Numbers
/// File Name: EvenNumbers.java
/// Date Finished: Feb 10, 2016

public class EvenNumbers {

    public static void main(String[] args) {

        for (int i = 1; i <= 20; i++) {
            System.out.print(i);
            if (i % 2 == 0) {
                System.out.println(" <");
            } else {
                System.out.println();
            }
        }

    }

}

Output

[85] Fizz Buzz

Goal: Do the Fizz Buzz Challenge: Loop through 1 to 100 (inclusive) and print "Fizz" if the number is divisible by 3, "Buzz" if it's divisible by 5, "FizzBuzz" if it's divisible by both, or is none of those conditions meet, just print the number.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Fizz Buzz
/// File Name: FizzBuzz.java
/// Date Finished: Feb 10, 2016

public class FizzBuzz {

    public static void main(String[] args) {

        for (int i = 1; i <= 100; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0) {
                System.out.println("Fizz");
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }

    }

}

Output

[86] Letter at a Time

Goal: Learn the difference between character length and position.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: PROGRAM_NAME
/// File Name: LetterAtATime.java
/// Date Finished: Feb 10, 2016

import java.util.Scanner;

public class LetterAtATime
{
    public static void main( String[] args )
    {
        Scanner kb = new Scanner(System.in);

        System.out.print("What is your message? ");
        String message = kb.nextLine();

        System.out.println("\nYour message is " + message.length() + " characters long.");
        System.out.println("The first character is at position 0 and is '" + message.charAt(0) + "'.");
        int lastpos = message.length() - 1;
        System.out.println("The last character is at position " + lastpos + " and is '" + message.charAt(lastpos) + "'.");
        System.out.println("\nHere are all the characters, one at a time:\n");

        for ( int i=0; i<message.length(); i++ )
        {
            System.out.println("\t" + i + " - '" + message.charAt(i) + "'");
        }

        int a_count = 0;

        for ( int i=0; i<message.length(); i++ )
        {
            char letter = Character.toLowerCase(message.charAt(i));
            if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u')
            {
                a_count++;
            }
        }

        System.out.println("\nYour message contains " + a_count + " vowel(s). Isn't that interesting?");

        // If the less than condition is changed to a less than or equal to condition, an exception is thrown.
        // Something like this:
        // java.lang.StringIndexOutOfBoundsException: String index out of range: 3

        // The length of "box" is 3, and the position of the 'x' is 2.

        // The length starts counting at 1, while the position starts counting at 0, so if we try to loop until
        // the position equals the length, an error happens because there's no char there.

    }
}

Output

[88] Adding Values with a For Loop

Goal: Given a number, start from 1 and add all natural numbers until that number.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Adding Values with a For Loop
/// File Name: AddingValues.java
/// Date Finished: Feb 10, 2016

import java.util.Scanner;

public class AddingValues {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int iterations, total = 0;

        System.out.print("Number: ");
        iterations = input.nextInt();

        for (int i = 1; i <= iterations; i++) {
            System.out.print(i + " ");
            total += i;
        }
        System.out.println();
        System.out.println("The sum is " + total + ".");

    }

}

Output