Thursday, 18 December 2014

// The "Exercise43_Exercise1" class.
// (from the Carter book)

// Exercise 4.3, Question 1 (a,b,c & d):

// Using a FOR loop in Java.
// Exercise 4.3, Question 1 --> a,b,c and d.
// What does each statement print?

import java.awt.*;
import hsa.Console;

public class Exercise43_Exercise1
{
    static Console c;           // The output console

    public static void main (String[] args)
    {
        c = new Console ();

        // Exercise # 1 (a):

        for (int i = -3 ; i <= 3 ; i++)
        {
            c.println ("*");
        }
        // produces 7 stars - one per line
        c.println ();

        // Exercise # 1 (b):

        for (int countDown = 5 ; countDown > 0 ; countDown--)
        {
            c.println (countDown + " seconds (till blast off)!");
        }
        // countDown starts at 5 and counts down to 1 (0 does not show)
        c.println ();

        // Exercise # 1 (c):

        for (char letter = 'P' ; letter <= 'S' ; letter++)
        {
            c.println ("Give me a " + letter);
        }
        // produces letters starting with 'P' and ending at 'S', inclusive
        c.println ();

        // Exercise # 1 (d):

        for (int i = 2 ; i < 100 ; i *= i)
        {
            c.println ("i = " + i);
        }
        // produces multiples of 'i' where 'i' starts at 2 and
        // ends at 100
        // ends at 'i' = 16 because the last multiplication
        // would result in 16 * 16 = 256
        c.println ();

    } // main method
} // Exercise43_Exercise1 class

No comments:

Post a Comment