Chapter 6 - Java How to Program

Chapter 6 Methods: A Deeper Look

Objectives

  • Understand how static methods and fields are associated with classes rather than objects.

  • Learn about the method-call/return mechanism supported by the method-call stack.

  • Familiarize with argument promotion and casting.

  • Discover how packages group related classes.

  • Implement secure random-number generation for game-playing applications.

  • Comprehend the visibility limitations of declarations in programs.

  • Grasp the concept of method overloading and creation of overloaded methods.

Outline

6.1 Introduction
  • Developing and maintaining large programs is best achieved by constructing them from small, manageable modules.

  • Key topics:

    • Static methods

    • Method-call stack

    • Random-number generation simulation techniques

    • Constants in programs

    • Method overloading

6.2 Program Units in Java
  • Java programs combine user-defined and predefined methods/classes.

  • Predefined classes are part of the Java API and packaged for easy import and reuse.

  • Modularization: Helps in organizing related classes and makes programs easier to maintain.

  • Java 9 introduces modules as a new program unit.

  • The Java API provides extensive libraries for various operations including math, string manipulation, I/O, networking, etc.

6.2.1 Software Engineering Observation
  • Familiarize with Java API classes and methods to avoid reinventing the wheel, thus reducing development time and minimizing errors.

6.2.2 Program Modules and Classes
  • Modularity is achieved by separating code into classes and methods, each performing specific tasks.

  • Each method is self-contained, promoting reusability and easier debugging.

6.2.3 Error-Prevention Tip
  • Methods performing single tasks are easier to test and debug.

  • If a method does not have a clear, concise name, consider breaking it into smaller, more manageable methods.

6.3 Static Methods, Static Fields, and Class Math
  • Static Methods: Methods that apply to the class as a whole, not specific objects.

  • Math Class: Collection of static methods for common mathematical calculations.

    • Examples of Math methods (Table 6.2):

      • abs(x): absolute value of x

      • ceil(x): rounds x up to the nearest integer

      • floor(x): rounds x down to the nearest integer

      • pow(x, y): returns x raised to the power y

6.4 Declaring Methods with Multiple Parameters
  • Methods can take multiple parameters, committing to a comma-separated list.

  • Each method call must provide an argument for each parameter declared.

  • Consistency in argument types with parameter types is crucial.

6.4.1 Common Programming Errors
  • Specify the type for each parameter; omitting it leads to syntax errors.

6.5 Notes on Method Usage
  • Call methods using their name, via an object reference, or using the class name for static methods.

  • Control returns when the program flow reaches the end of the method, a return statement is executed, or an expression is returned.

6.6 Method-Call Stack and Activation Records
  • Stack Data Structure: Last-in, first-out (LIFO) mechanism to manage method calls.

  • Each method call creates a stack frame that contains return addresses and local variables.

  • Stack Overflow: Occurs when method calls exceed available memory space for stack frames.

6.7 Argument Promotion and Casting
  • Argument Promotion: Automatic conversion of argument values to compatible types as per method parameters.

  • Promotion rules dictate type conversions in multi-type expressions.

  • Casting is required when converting to a type lower in the hierarchy if potential data loss exists.

6.8 Java API Packages
  • Java has a rich assortment of predefined classes grouped into packages for various functionalities:

    • java.io: I/O operations

    • java.util: Utilities for data processing

    • java.awt: Abstract Window Toolkit for GUI

    • java.security: Security frameworks

6.9 Case Study: Secure Random-Number Generation
  • Utilize SecureRandom class to produce random values for simulations and games.

  • Understand how scaling and shifting work in random number generation (e.g. rolling a die).

6.10 Case Study: A Game of Chance; Introducing Enum Types
  • The Craps game involves rolling two dice, assessing outcomes to declare a win/loss status.

  • Enum: Represents fixed sets of constants, improving code readability and maintainability.

6.11 Scope of Declarations
  • The scope defines where in the program the declarations can be accessed.

  • Basic rules determine the accessibility of parameters and local variables within methods and classes.

6.12 Method Overloading
  • Allows multiple methods to have the same name but with different parameters.

  • Compiler selects the correct method based on the provided arguments.

  • Common Programming Error: Overloading methods must not have the same parameter list as it results in compilation errors.