CIS163AA - Java Prog I - Sect 5811 - Quiz #1 - by Pat Moss

Due Monday, September 26, 2005

Name: Pat Moss

I.  Definitions:  Please explain the following terms:

 1. increment     -- Adding a value to a counter. Often, adding 1 to a
    (p. 81)          loop control counter, such as count++ or ++count.
                     Also consider decrement, which is subtracting a
                     value from a counter, such as count-- or --count.
 2. AWT           -- Abstract Windowing Toolkit. The package in the 
    (p. 639,660)     Java API (java.awt) that contains classes related
                     to graphics and graphical user interfaces.
                     Also consider the newer Swing package (javax.swing)
                     that contains classes related to graphical user
                     interfaces. Swing provides alternative components
                     to the AWT, but does not replace it.
 3. instance      -- An object created from a class. Multiple objects
    (p. 650)         can be instantiated from a single class.
 4. package       -- A Java reserved word that is used to specify a group
    (p. 655)         of related classes. Examples: java.lang, java.awt.
 5. constructor   -- A special method in a class that is invoked when
    (p. 645,115,169) an object is instantiated from the class. Used to
                     initialize the object. A constructor is a special
                     method that has the same name as the class.  
                     Constructors do not have a return type (not even
                     void) and therefore cannot have a return statement.
 6. Math method: pow(x,y) -- The Math class has a large number of basic
    (p. 127,849)     mathematical methods, including Math.pow(x,y).
                     All methods in the Math class are static methods.
                     The Math class is defined in the java.lang package
                     and therefore, does not require an import statement.
                     The method Math.pow(x,y) returns the value of x
                     raised to the power y. The formal declaration has 
                     the form: static double pow (double num, double power).
 7. variable      -- An identifier in a program that represents a memory
    (p. 663)         location in which a data value is stored (for a
                     primitive data type), or in which a memory address
                     reference value is stored (for an object).
 8. wrapper class -- A class designed to store a primitive data type in
    (p. 663,139)     an object. Usually used when a object reference is
                     needed and a primitive type will not suffice.
                     All wrapper classes are included in java.lang.
 9. container     -- A Java graphical user interface component that can
    (p. 644)         hold other components.
10. NumberFormat class -- The NumberFormat class and the DecimalFormat 
    (p. 130,854)     class are used to format numerical information so
                     that it looks more appropriate when printed or displayed.
                     These are in java.text. NumberFormat methods include:
                     getCurrencyInstance() and getPercentInstance().
                     An example: (p. 132)
                     double subtotal = 19.35;
                     NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
                     System.out.println("Subtotal: " + fmt1.format(subtotal);
                     The output is: Subtotal: $19.35 

II. Question or short essay

 1. What is the reason for using a semicolon (;) in a Java program?
    The semicolon is used as a delimiter to end each Java statement.
    Java is a "free form" language, so that:
    a. One statement can be placed on each line, and
    b. One statement can be continued over several lines, and
    c. Multiple statements can be contained on one line, etc.
    Some examples:
    a. z = x + y;
    b. z = x +
               y;
    c. z = x + y; d = a * b - c; sqroot2 = Math.sqrt (2.0D);
    Note: Numeric integer literals default to int. Numeric floats default
          to double. To force the primitive data type, use F, D, and L:
          xfloat = 2.0F; xdouble = 2.0D; xlong = 2L; (p. 75) 
 2. In graphics programming what purpose does the setColor method have?
    Sets this graphic context's current color to the specified color.
    Ref: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html
    For example: page.setColor (Color.yellow); (p. 178)
 3. How is a method different from a class?
    A class is the blueprint for an object -- the model that defines the
    variables and methods that an object will contain when instantiated.

    A method is a named group of declarations and programming statements
    that can be invoked (executed) when needed. A method is part of a
    class. (p. 643, 653)
 4. How could you get a random integer somewhere from 1 to 6?
    Use Math.random() as follows:

    int random1 = (int) (Math.random() * 6) + 1;

    Here: Math.random() returns a value with floor = 0.0 and ceiling
    less than 1.0. Math.random() * 6 returns a value with floor = 0.0
    and ceiling less than 6.0. The (int) cast returns an integer value
    with floor = 0 and ceiling = 5. Adding 1 returns an integer value
    with floor = 1 and ceiling = 6.

    Notes: a. The Math class is part of the java.lang package, and thus,
              does not require an explicit import declaration. (p. 127)
           b. Another possibility: Use the Random class (p. 124-127)
              The Random class is part of the java.util package.
 5. What is the difference between a frame and a panel?
    A frame is a container that is used to display GUI-based Java
    applications. A frame is displayed as a separate window with its
    own title bar. A frame is defined by the JFrame class. (p. 142)

    A panel is also a container. However, unlike a frame, it cannot be
    displayed on its own. A panel must be added to another container
    for it to be displayed. Its primary role is to help organize other
    components in a GUI. A panel is defined by the JPanel class. (p. 142)
 
III. Programming Exercises  
     
  1. Write a program to read in the weight (in pounds) of an object and
     compute and print the weight in kilograms with two decimal places.
     (Hint: 1 pound is equal to 0.453592 kg).
  2. Write an applet that draws a smiling face. Give the face eyes with
     pupils, ears, a nose, and a mouth.
 
     Hand in a printed copy of your source code.