Thursday, 18 December 2014

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

// Exercise 4.5 - Question 4 (a,b,c,d & e):

// State, with reasons, which loop structure you think would be most
// appropriate for solving each of the following problems.  Do NOT
// actually solve the problem in each part.

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

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

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

        // Question 4 (a):

        // Use a FOR loop that repeats 100 times, starting at 1 and
        // ending at 100, declare 'sum' as an int type, cube each index
        // value of the FOR loop and add it to the 'sum' --> use Math.pow(i,3)
        // with 'i' being the index.  Output the results ('sum') after the
        // FOR loop is done.

        // Question 4 (b):

        // Use a DO loop to first prompt the user for a user ID and password.
        // Compare the input password to a constant stored in the program.
        // If they match, exit the DO loop and tell the user he/she is now
        // logged in.  However, if they do NOT match, keep the user in the
        // DO loop until he/she enters a password that matches the one
        // stored in the program.

        // Question 4 (c):

        // Use a WHILE loop to keep entering only positive integers (> 0).
        // The user should first be prompted for a positive integer.  The
        // WHILE loop condition, now being true, allows entry into the loop.
        // As long as the WHILE loop condition is TRUE, keep entering positive
        // integers in the loop.  Only exit the WHILE loop when a sentinel value
        // (-1) is entered.

        // Question 4 (d):

        // Use a FOR loop to count the number of times a positive integer is
        // evenly divisible by 2 (using modulus), starting at 100. If the
        // division of the integer by 2 is even, there is no remainder, and
        // 1 should be added to a counter.  However, if the integer cannot be
        // evenly divided by 2, there will be a remainder, so the counter should
        // not be incremented by 1.  At the end of the repetition (to 400),
        // output the number of times a positive integer was evenly divisible
        // by 2.

        // Question 4 (e):

        // Use a FOR loop to accumulate the simple interest earned at 8%
        // on $1.00 for 10 years.  The FOR loop will repeat 10 times, once
        // for each year, and the interest for 1 year (1.00 * 0.08) will be
        // added to the total interest. After the FOR loop is done, output
        // what $1.00 is worth after 10 years at 8% interest per year.

    } // main method
} // Exercise45D_Chapter4 class

No comments:

Post a Comment