Thursday, 15 January 2015

// The "TriangularPrism" class.
// Input 5 required values and then calculate and display
// the inputted values, surface area and volume of the
// triangular prism.  Calculate the base, then
// calculate the rectangles, then add these two together
// to get the grand total for the surface area.

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

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

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

        double height;
        double length;
        double sideA;
        double sideC;
        double base;
        double base2;
        double rectangles = 0;
        double grandTotal = 0;
        double volume = 0;

        c.print ("Enter the value of the base of the triangular prism: ");
        base = c.readDouble ();
        base2 = base;       // base2 will store the input base

        c.print ("Enter the value of the length of the triangular prism: ");
        length = c.readDouble ();

        c.print ("Enter the value of the height of the triangular prism: ");
        height = c.readDouble ();

        c.print ("Enter the value of side A of the triangular prism: ");
        sideA = c.readDouble ();

        c.print ("Enter the value of side C of the triangular prism: ");
        sideC = c.readDouble ();

        // display the inputted values
        c.println ("You just inputted a base of " + base2
                + " a length of " + length + " a height of ");
        c.println (height + " side A which is " + sideA
                + " and side C which is " + sideC + " (all in mm)");

        // first, calculate the surface area of the triangular prism
        // using 3 formulas:  base = (base * length) / 2  (recalculated base)
        // rectangles = sideA * height + base * height + sideC * height
        // grandTotal = rectangles + base
        base = (base2 * length) / 2;
        rectangles = sideA * height + base * height + sideC * height;
        grandTotal = rectangles + (2 * base);

        // final output statement # 1
        c.println ();       // one blank line
        c.println ("The surface area of the triangular prism with a "
                + "re-calculated base of " + base);
        c.println ("a length of " + length + " a height of " + height
                + " side A which is " + sideA);
        c.println ("and side C which is " + sideC
                + " is about: " + Math.round (grandTotal) + " mm sqrd");

        // next, calculate the volume of the triangular prism
        // using the formula:  volume = (base2* length * height) / 2
        volume = base2 * length * height;

        // final output statement # 2
        c.println ();
        c.println ("The volume of the triangular prism with an "
                + "input base of " + base2);
        c.println ("a length of " + length + " and a height of " + height);
        c.println ("is about: " + Math.round (grandTotal) + " mm cubed");

    } // main method
} // TriangularPrism class

// The "RectangularPrism" class.
// Input 3 required values and then calculate and display
// the inputted values, surface area and volume of the
// rectangular prism.

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

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

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

        double length, width, height;
        double total = 0;
        double volume = 0;

        c.print ("Enter the length of the rectangular prism (in mm): ");
        length = c.readDouble ();

        c.print ("Enter the height of the rectangular prism (in mm): ");
        height = c.readDouble ();

        c.print ("Enter the width of the rectangular prism (in mm): ");
        width = c.readDouble ();

        // display the inputted values
        c.println ("You just inputted the length " + length
                + " height " + height + " and width "
                + width + "(all in mm)");

        // first, calculate the surface area of the rectangular prism
        // using the formula:  total = 2 * (width * height + length * width
        //                             + length * height)
        total = 2 * (width * height + length * width + length * height);

        // final output statement # 1
        c.println ();       // one blank line
        c.println ("The surface area of the rectangular prism with a "
                + "length of " + length + " a width of " + width);
        c.println ("and a height of " + height + " is about: "
                + Math.round (total) + " mm squared");

        // next, calculate the volume of the rectangular prism
        // using the formula: volume = length * width * height
        volume = length * width * height;

        // final output statement # 2
        c.println ();
        c.println ("The volume of the rectangular prism with a "
                + "length of " + length + " a width of " + width);
        c.println ("and a height of " + height + " is about: "
                + Math.round (volume) + " mm cubed");


    } // main method
} // RectangularPrism class

// The "SquareBasedPyramid" class.
// Input 3 required values and then calculate and display
// the inputted values, surface area and volume of a
// square-based pyramid.  To calculate the square-based pyramid's
// surface area, calculate that of a triangle, then that of a square
// and then add these two calculated values together.

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

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

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

        double base;
        double height;
        double side;
        double triangle = 0;
        double baseSqrd = 0;
        double grandTotal = 0;
        double volume = 0;

        c.print ("Enter the value of the side of the square-based pyramid (in mm): ");
        side = c.readDouble ();

        c.print ("Enter the value of the base of the square-based pyramid (in mm): ");
        base = c.readDouble ();

        c.print ("Enter the value of the height of the square-based pyramid (in mm): ");
        height = c.readDouble ();

        // display the inputted values
        c.println ("You just inputted one of two equal sides " + side
                + " the base " + base + " and height " + height
                + "(all in mm)");

        // first, calculate the surface area of the square-based pyramid
        // using 3 formulas  triangle = (base * side) / 2
        // baseSqrd = (base * base) and grandTotal = (4 * triangle) + baseSqrd
        triangle = (base * side) / 2;
        baseSqrd = (base * base);
        grandTotal = (4 * triangle) + baseSqrd;

        // final output statement # 1
        c.println ();       // one blank line
        c.println ("The surface area of the square-based pyramid with a base "
                + "of " + base);
        c.println ("and a side length of " + side
                + " is about: " + Math.round (grandTotal) + " mm squared");

        // next, calculate the volume of the square-based pyramid
        // using 2 formulas:  baseSqrd = (base * base) and
        // volume = (baseSqrd * height) / 3
        // (however, no need to recalculate baseSqrd here)
        volume = (baseSqrd * height) / 3;

        // final output statement # 2
        c.println ();
        c.println ("The volume of the square-based pyramid with a base "
                + "of " + base);
        c.println ("and a height of " + height
                + " is about: " + Math.round (volume) + " mm cubed");


    } // main method
} // SquareBasedPyramid class

// The "Cone" class.
// Input 3 required values and then calculate and display the
// inputted values, surface area and volume of the cone.

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

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

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

        double radius;
        double height;
        double side;
        final double PI = 3.14159;
        double total;
        double volume;

        c.print ("Enter the value for the radius of the cone (in mm): ");
        radius = c.readDouble ();

        c.print ("Enter the value for the height of the cone (in mm): ");
        height = c.readDouble ();

        c.print ("Enter the value for a side length of the cone (in mm): ");
        side = c.readDouble ();

        // display the inputted values
        c.print ("You just inputted a radius of " + radius + " mm");
        c.println (" a height of " + height + " mm ");
        c.println ("and a side length of " + side + " mm");

        // first, calculate the surface area of the cone
        // using the formula:  total = PI * radius * side
        total = PI * radius * side;

        // final output statement # 1
        c.println ();       // one blank line
        c.println ("The surface area of the cone with a radius of ");
        c.println (radius + " mm and a side length of "
                + side + " mm is about:  " + Math.round (total) + " mm squared");

        // next, calculate the volume of the cone using the
        // formula:  volume = (PI * (radius * radius) * height) / 3  OR
        // (PI * Math.pow (radius,2) ( height) / 3
        volume = (PI * Math.pow (radius, 2) * height) / 3;

        // final output statement # 2
        c.println ();
        c.println ("The volume of the cone with a radius of " + radius
                + " mm and a height of " + height + " mm ");
        c.println ("is about: " + Math.round (volume) + " mm cubed");

    } // main method
} // Cone class

// The "Sphere" class.
// Input 1 required value and then calculate and display
// the inputted value, surface area and volume of a sphere.

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

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

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

        double radius;
        double surfaceArea = 0;
        double volume = 0;
        final double PI = 3.14159;

        c.print ("Enter the value of the radius of the sphere (in mm): ");
        radius = c.readDouble ();

        // display the input value (radius)
        c.println ("You just inputted a radius of " + radius + " mm");

        // first, calculate the surface area of the sphere
        // using the formula:  surface area = 4 * PI * radius
        surfaceArea = 4 * PI * radius;

        // final output statement # 1
        c.println ();       // one blank line
        c.println ("The surface area of the sphere with radius " + radius
                + " mm is about: " + Math.round (surfaceArea) + " mm squared");

        // next, calculate the volume of the sphere using the formula:
        // volume = (4 * PI * (radius * radius * radius)) / 3  OR
        // (4 * PI * Math.pow (radius,3)) / 3
        volume = (4 * PI * Math.pow (radius, 3)) / 3;

        // final output statement # 2
        c.println ();
        c.println ("The volume of the sphere with radius " + radius
                + " mm is about: " + Math.round (volume) + " mm cubed");

    } // main method
} // Sphere class

// The "Cylinder" class.
// Input 2 required values and then calculate and display
// the inputted values, surface area and the volume of the cylinder.

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

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

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

        double radius;
        double height;
        double lateralSurface;
        double total = 0;
        double volume = 0;
        final double PI = 3.14159;

        c.print ("Enter the value of the radius of the cylinder (in mm): ");
        radius = c.readDouble ();

        c.print ("Enter the value of the height of the cylinder (in mm): ");
        height = c.readDouble ();

        // display the inputted values
        c.println ("You just inputted a radius of " + radius
                + " and a height of " + height + "(all in mm)");

        // first, calculate the surface area of the cylinder
        // using the formula: total =  2 * PI * (radius * radius) +
        //                             2 * PI * radius * height
        total = (2 * PI * Math.pow (radius, 2)) + (2 * PI * radius * height);

        // final output statement # 1
        c.println ();    // one blank line
        c.println ("The surface area of the cylinder with radius " + radius);
        c.println ("mm and height " + height + " mm is about : "
                + Math.round (total) + " mm squared");

        // next, calculate the volume of the cylinder
        // using the formula: volume = PI * (radius * radius) * height
        volume = PI * Math.pow (radius, 2) * height;

        // final output statement # 2
        c.println ();
        c.print ("The volume of the cylinder with height " + height + " mm");
        c.println (" is about: " + Math.round (volume) + " mm cubed");


    } // main method
} // Cylinder class

// The "Circle" class.
// Input 1 required value and then calculate and display
// the inputted value, perimeter and area of a circle.

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

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

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

        double radius;
        double diameter;
        final double PI = 3.14159;
        double perimeter;
        double area;

        c.print ("Enter the value of the radius of the circle: ");
        radius = c.readDouble ();

        diameter = 2 * radius;

        // display the inputted value (radius) and the calculated value
        // of diameter (2 * radius)
        c.println ("You just inputted the value of a radius of " + radius
                + " cm");
        c.println ("The diameter is calculated as twice the radius = "
                + diameter + " cm");

        // first, calculate the perimeter of the circle
        // using the formula:  perimeter = 2 * PI * radius OR
        //                                 PI * diameter
        perimeter = 2 * PI * radius;

        // final output statement # 1
        c.println ();       // one blank line
        c.println ("The perimeter of the circle with a radius of " + radius
                + " is about: " + Math.round (perimeter) + " cm");


        // next, calculate the area of the circle
        // using the formula:  area = PI * (radius * radius)  OR
        //                            PI * Math.pow (radius,2)
        area = PI * Math.pow (radius, 2);

        // final output statement # 2
        c.println ();
        c.println ("The area of the circle with a radius of " + radius
                + " is about: " + Math.round (area) + " cm squared");

    } // main method
} // Circle class

// The "Trapezoid" class.
// Input 5 required values and then calculate the perimeter and area
// and output the inputted and calculated values.


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

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

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

        int sideA, sideC, sideD, base, height;
        int perimeter = 0;
        int area = 0;

        // prompt for and input the values required to calculate
        // both the perimeter and area of a trapezoid

        c.print ("Enter the value of side A of the trapezoid (in cm): ");
        sideA = c.readInt ();

        c.print ("Enter the value of side C of the trapezoid (in cm): ");
        sideC = c.readInt ();

        c.print ("Enter the value of side D of the trapezoid (in cm): ");
        sideD = c.readInt ();

        c.print ("Enter the value of the height of the trapezoid (in cm): ");
        height = c.readInt ();

        c.print ("Enter the value of the base of the trapezoid (in cm): ");
        base = c.readInt ();

        c.println ();       // one blank line
        c.println ("The 3 required values you've inputted are: ");
        c.println ("side A: " + sideA + " side C: " + sideC
                + " side D: " + sideD + " height: " + height
                + " and base: " + base);

        // first, calculate the perimeter of the trapezoid
        // according to the formula:  perimeter = sideA + sideC + sideD + base
        perimeter = sideA + sideC + sideD + base;

        // final output statement # 1
        c.println ();
        c.println ("The perimeter of the trapezoid is: " + perimeter + " cm");

        // next, calculate the area of the trapezoid
        // acocrding to the formula:  area = ((sideA + sideB) * height) / 2
        area = ((sideA + base) * height) / 2;

        // final output statement # 2
        c.println ();
        c.println ("The area of the trapezoid is: " + area + " cm");

    } // main method
} // Trapezoid class

// The "Triangle" class.
// Input 4 required values and then calculate the perimeter and area
// and output the inputted and calculated values.

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

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

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

        int sideA, sideC, height, base;
        int perimeter = 0;
        int area = 0;

        c.print ("Enter the value of side A of the triangle (in cm): ");
        sideA = c.readInt ();

        c.print ("Enter the value of side B of the triangle (in cm): ");
        sideC = c.readInt ();

        c.print ("Enter the value of the height of the triangle (in cm): ");
        height = c.readInt ();

        c.print ("Enter the value of the base of the triangle (in cm): ");
        base = c.readInt ();

        c.println ();       // one blank line
        c.println ("The 4 required values you've inputted are: ");
        c.println ("side A: " + sideA + " side C: " + sideC
                + " height: " + height + " and base: " + base);


        // first, calculate the perimeter of the triangle
        // according to the formula:  perimeter = side A + side C + base
        perimeter = sideA + sideC + base;

        // final output statement # 1
        c.println ();
        c.println ("The perimeter of the triangle is: " + perimeter + " cm");

        // next, calculate the area of the parallelogram
        // acocrding to the formula:  area = base * height
        area = (base * height) / 2;

        // final output statement # 2
        c.println ();
        c.println ("The area of the triangle is: " + area + " cm");

    } // main method
} // Triangle class

// The "Parallelogram" class.
// Input 3 required values and then calculate the perimeter and area
// and output the inputted and calculated values.

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

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

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

        int base;
        int height;
        int side;
        int perimeter = 0;
        int area = 0;

        // prompt for and input the values required to calculate
        // both the perimeter and area of a parallelogram
        c.print ("Enter the value of the base of the parallelogram (in cm): ");
        base = c.readInt ();

        c.print ("Enter the value of the height of the parallelogram (in cm): ");
        height = c.readInt ();

        c.print ("Enter the value of one side of the parallelogram (in cm): ");
        side = c.readInt ();

        c.println ();       // one blank line
        c.println ("The 3 required values you've inputted are: ");
        c.println ("base: " + base + " height: " + height + " and "
                + "side: " + side);

        // first, calculate the perimeter of the parallelogram
        // according to the formula:  perimeter = 2 (base + side)
        perimeter = 2 * (base + side);

        // final output statement # 1
        c.println ();
        c.println ("The perimeter of the parallelogram is: " + perimeter + " cm");

        // next, calculate the area of the parallelogram
        // acocrding to the formula:  area = base * height
        area = base * height;

        // final output statement # 2
        c.println ();
        c.println ("The area of the parallelogram is: " + area + " cm");


    } // main method
} // Parallelogram class

// The "Rectangle" class.
// Input 2 required values and then calculate and display
// the inputted values, perimeter and area of the rectangle.

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

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

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

        int length;
        int width;
        int perimeter = 0;
        int area = 0;

        c.print ("Enter the length of the rectangle: ");
        length = c.readInt ();

        c.print ("Enter the width of the rectangle: ");
        width = c.readInt ();

        // display the inputted values
        c.println ("You just inputted a length of " + length
                + " and a width of " + width + " (both in cm)");

        // first, calculate the perimeter of the rectangle
        // using the formula:  perimeter = 2 * (length + width)
        perimeter = 2 * (length + width);

        // final output statement # 1
        c.println ();       // one blank line
        c.println ("The perimeter of the rectangle with a length of "
                + length + " and a width of " + width + " is: "
                + perimeter + " cm");

        // next, calculate the area of the rectangle
        // using the formula:  area = length * width
        area = length * width;

        // final output statement # 2
        c.println ();
        c.println ("The area of the rectangle with a length of "
                + length + " and a width of " + width + " is: "
                + area + " cm sqrd");

    } // main method
} // Rectangle class

Monday, 12 January 2015

// The "Evens_Revised" class.
// (from the book:  A Guide to Programming in Java)

// This program accepts a positive or negative integer.
// Then, a ''modulus 2'' operation is performed,
// An appropriate message is displayed depending
// on whether the input integer was odd or even.

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

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

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

        int number;
        int answer;
        char response = 'y';

        do
        {

            c.println (" Enter a value that is + or -");
            number = c.readInt ();
            answer = number % 2;

            if (answer == 0)
            {

                c.println ("This is: " + number);
                c.println ("Congratulations! You've finally printed out an even number!");


            }
            else
            {
                c.println ("The result of " + number + " modulus 2 is "
                        + Math.abs (answer));
            }

            c.println ("Do you want to continue? (Y/N)");
            response = c.readChar ();
            if (response == 'N' || response == 'n')
            {
                break;
            }
        }
        while (response == 'y' || response == 'Y');
        // end of do-while loop
        c.println ();
        c.println ("Thanks for running the 'Evens_Revised' Java program!");

    } // main method
} // Evens_Revised class

// The "Chapter3_Example4Bii" class.  Chapter 3, Example 4Bii.
// (from the Carter book)

// If we want to have the ELSE associated with the first IF, the preferred
// solution is to use brace brackets to "close off" the second IF, as shown
// below.  This works if 'p' and 'q' are both set to true, but not if 'q'
// is set to false (while 'p' is true).  If 'p' is false, it works, as well.

// An empty ELSE statement provides an alternative solution to the problem.
// In this solution, no brace brackets are required.

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

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

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

        boolean p = true, q = true;
        if (p)
            if (q)
                c.println ("Take Path 1A");
            else
                ;  // to "close off" the second IF
        else
            c.println ("Take Path 2A");

        p = false;
        q = false;

        if (p)
            if (q)
                c.println ("Take Path 1B");
            else
                ;  // to "close off" the second IF
        else
            c.println ("Take Path 2B");


      } // main method
} // Chapter3_Example4Bii class

// The "Chapter3_Example4Bi" class.  Chapter 3, Example 4Bi.
// (from the Carter book)

// If we want to have the ELSE associated with the first IF, the preferred
// solution is to use brace brackets to "close off" the second IF, as shown
// below.  This works if 'p' and 'q' are both set to true, but not if 'q'
// is set to false (while 'p' is still true).  If 'p' is also false, it works too.

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

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

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

        boolean p = true, q = true;

        if (p)
        {
            if (q)
                c.println ("Take Path 1A");
        }
        else
            c.println ("Take Path 2A");

        p = false;
        q = false;

        if (p)
        {
            if (q)
                c.println ("Take Path 1B");
        }
        else
            c.println ("Take Path 2B");

    } // main method
} // Chapter3_Example4Bi class

// The "Chapter3_Example4" class.  Chapter 3, Example 4.
// (from the Carter book)

// This multi-way IF statement prints a comment that depends on the value
// of the char variable 'grade'.  Try this with 'grade' values A, B, C, D,
// E, F (remember that E is considered invalid).

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

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

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

        char grade = 'F';

        if (grade == 'A')
            c.println ("Excellent");
        else if (grade == 'B')
            c.println ("Good");
        else if (grade == 'C')
            c.println ("Average");
        else if (grade == 'D')
            c.println ("Pass - Fair");
        else if (grade == 'F')
            c.println ("Fail - Poor");
        else
            c.println ("Invalid grade - must be A, B, C, D or F only");

    } // main method
} // Chapter3_Example4 class

// The "Chapter3_Example3" class.  Chapter 3, Example 3.
// (from the Carter book)

// The IF statement shown below efficiently determines the largest
// value among 3 values, x, y and z, and assigns this value to 'largest'.
// There are many other ways to determine the largest of the 3 values,
// but this one (below) involves the least work.  Test this using different
// values for x, y and z.

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

public class Chapter3_Example3
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        
        int x = 4, y = 5, z = 3, largest;
        
        if (x >= y)
            // y is eliminated - largest must be x or z
            if (x >= z)
                largest = x;
            else
                largest = z;
        else
            // x is eliminated - largest must be y or z
            if (y >= z)
                largest = y;
            else
                largest = z;
        c.println ("largest = " + largest);      
       
    } // main method
} // Chapter3_Example3 class

// The "Chapter3_Example2" class.  Chapter 3, Example 2.
// (from the Carter book)

// Nested IF statements.  (new and improved version of Example 1).
// That is, if x is negative, only 1 test is performed, while if x is
// positive or zero, only 2 tests are performed.  In no case is it
// necessary to perform 3 tests.

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

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

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

        int x = 10;

        if (x < 0)
            c.println ("x value is negative");
        else
            if (x > 0)
                c.println ("x value is positive");
            else
                c.println ("x value is zero");

    } // main method
} // Chapter3_Example2 class

// The "Chapter3_Example1" class.  Chapter 3, Example 1.
// Nested IF statements.  (from the Carter book)

// For any value of x, this solution involves 3 tests.  But, we can
// increase the efficiency of the code by nesting these statements
// (see Example 2, which involves placing one IF statement within
// another IF statement).

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

public class Chapter3_Example1
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        
        int x = -10;
        
        if (x < 0)
        {
            c.println ("x value is negative");
        }
        else if (x > 0)
        {
            c.println ("x value is positive");
        }
        else
        {
            c.println ("x value is zero");
        }
     
    } // main method
} // Chapter3_Example1 class

// The "Search" class.
//(see page 244) (from the Carter book)

// This class contains only one method: linear().
// The main() method is in the controlling class,
// called 'FindNum', which calls the linear()
// method here to find an element in the 'array'.

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

public class Search
{

    // returns the index of the element 'numToFind'.
    // -1 is returned if the element is not found.
    // pre:  none
    // post:  index of numToFind has been returned.  -1 has been
    //        returned if element is not found.
    public static int linear (int[] array, int numToFind)
    {
        int index = 0;

        while ((array [index] != numToFind) &&
                (index < array.length - 1))
        {
            index += 1;
        } // end of while loop

        if (array [index] == numToFind)
        {
            return (index);
        }
        else
        {
            return (-1);
        } // end of if-else statement

    } // end of linear method
} // end of Search class

// The "FindNum" class.
// (see page 245)  (from Carter book)

// This is the controlling class (containing main() method)
// and it uses the Search class method called linear()
// to find a number in the array called 'numArray'.

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

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

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

        final int MAX = 20;
        int[] numArray = new int [MAX];
        // declare an int array with room for 21 elements
        // (starting at 0 through 20)
        int num, location;

        // fill the array with 21 random numbers
        for (int i = 0 ; i < numArray.length ; i++)
        {
            numArray [i] = (int) ((MAX + 1) * Math.random ());
        } // end of for loop # 1

        // prompt the user for a number to search for
        c.print ("Enter a number between 0 and " + MAX + " ");
        num = c.readInt ();

        // now, search for that number and notify the user
        // of num's location in the array, numArray.
        location = Search.linear (numArray, num);
        // calls the linear method in the Search class.
        // the if-else statement checks to see if the
        // chosen number exists - if so, it shows its location.
        if (location == -1)
        {
            c.print ("Sorry, your number was not found ");
            c.println ("in the array");
        }
        else
        {
            c.print ("First occurrence of your number is ");
            c.println ("element # " + location);
        } // end of if-else statement

    } // end of main method
} // end of FindNum class

// The "DiceRolls" class.

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

public class DiceRolls
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        
        int [] outcomes = new int [13];  // array for storing outcomes
        int numRolls;  // number of rolls
        int outcome;  // counter (and element value)
        
        // prompt user for number of rolls
        c.print ("How many rolls? ");
        numRolls = c.readInt();
        
        // roll dice and add to outcomes
        for (int roll = 0; roll < numRolls; roll++)
        {
            outcome = (int)(6 * Math.random() + 1) +
                      (int)(6 * Math.random() + 1);
            outcomes [outcome] += 1; // add 1 to counter at index 'outcome'
        } // end of for loop # 1
        
        // show counts of outcomes
        for (int i = 2; i <= 12; i++)
        {
            System.out.println (i + ": " + outcomes [i]);
        }  // end of for loop # 2

    } // main method
} // DiceRolls class

// The "CountLetters" class.  (see page 243)
// (from Carter book)

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

public class CountLetters
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        
        final int LOW = 'A';   // smallest possible value
        final int HIGH = 'Z';  // highest possible value
        int [] letterCounts = new int [HIGH - LOW + 1];
        // an array of counters
        String word;
        char [] wordLetters;       // an array of char (letters)
        int offset;
        
        // prompt user for a word
        c.print ("Enter a word: ");
        word = c.readLine();
        
        // convert the input word to a char array and count
        // letter occurrences
        word = word.toUpperCase();  // convert 'word' to uppercase
        wordLetters = word.toCharArray();  // convert 'word' to a char array
        for (int letter = 0; letter < wordLetters.length; letter++)
        {
            offset = wordLetters [letter] - LOW;
            letterCounts [offset] += 1;  // add 1 to appropriate counter
        } // end of for loopn # 1
        
        // show letter occurrences
        for (int i = LOW; i <= HIGH; i++)
        {
            c.println ((char) i + ": " + letterCounts [i - LOW]);
        } // end of for loop # 2
        
       
    } // end of main method
} // end of CountLetters class