Blakes 21 Days Chapter 05 Document


Day 5, Creating Classes and Methods

Defining Classes

Creating Instance and Class Variables

Defining Instance Variables

Class Variables

Creating Methods

Defining Methods

Listing 5.1

Run the program

Figure 5.1 goes here - output pane

The this keyword

Variable Scope and Method Definitions

Passing Arguments to Methods

Listing 5.2 The Full Text of Passer.java

Figure 5.2 goes here

Class Methods

Creating Java Applications

Helper Classes

Java Applications and Arguments

Passing Arguments to Java Applications

Handling Arguments in Your Java Application

Listing 5.3

Figure 5.3 goes here

Creating Methods with the Same Name

Listing 5.4

Figure 5.4 goes here - Output pane from NetBeans

Constructors

Basic Constructors

Calling Another Constructor

Overloading Constructors

Listing 5.5

Overriding Methods

Creating Methods That Override Existing Methods

Listing 5.6

Run SubPrinter

Figure 5.5 goes here

Calling the Original Method

Overriding Constructors

Listing 5.7

Figure 5.6 goes here



Summary

Q & A

Quiz - Questions

  1. If a local variable has the same name as an instant variable, how can you refer to the instance variable in the scope of the local variable ?
    1. You can't, you should rename one of the variables.
    2. Use the keyword this before the instance variable name.
    3. Use the keyword super before the name.
  2. Where are instance variables declared in a class?
    1. Antwhere in the class
    2. Outside all methods in the class
    3. After the class declaration and above the first method
  3. How can you send to a program an argument that includes a space or spaces ?
    1. Surround the argument with double quotes.
    2. Separate the arguments with commas.
    3. Separate the arguments with periods.

Answers

  1. B. Answer A is a good idea, but variable name conflicts can be a source of subtle errors in your Java program.
  2. B. Customarily, instance variables are declared right after the class declaration and before any methods. It's necessary only that they be outside all methods.
  3. A. The quotation marks are not included in the argument when it is passed to the program.

Certification Practice

  1. What statement should replace the // answer goes here so that the result variable equals 312.0 ?
    1. float calculateResult(int c) {
    2. float calculateResult(int a, int b) {
    3. float calculateResult(int a, int b, int c) {
    4. float calculateResult() {

Exercise