Blakes 21 Days Chapter 4 Document


Day 4, Lists, Logic and Loops

Arrays

Declaring Array Variables

Creating Array Objects

Accessing Array Elements

Changing Array Elements


Listing 4.1

Listing 4.1 (screenshot from NetBeans) goes here

176

Figure 4.1 (the output pane) goes here

Multidimensional Arrays

Block Statements

If Conditionals

Switch Conditionals

Second Program

New Empty Java File window

183

Source Code

184

Files pane

185

Listing 4.2

(Running the Program) Command-line arguments

Figure 4.2 (screenshot from NetBeans Output pane) goes here

run:
1/2016 has 31 days.
BUILD SUCCESSFUL (total time: 0 seconds)

Setting Command-line arguments

190

191

192

Run, Run Project

193

Figure 4.3 (screenshot from NetBeans) goes here

186

Figure 4.4 goes here

187

Explanation of Program

The Ternary Operator

For Loops

Third Program

203

Listing 4.3

204

Figure 4.1 From the First Program, HalfDollars

200

Screenshot from NetBeans after successfully running the program

205

The explanation

While and Do Loops

While Loops

Fourth Program

Listing 4.4 - The Full Text of ArrayCopier.java

the Ouput pane

the Explanation

Do-While Loops

Breaking Out of Loops

Labeled Loops

Summary

Q & A

Quiz - Questions

  1. What kind of loop is used to execute the statements in the loop at least once before the conditional expression is evaluated?
    1. do-while
    2. for
    3. while
  2. Which of the following cannot be used as the test in a case statement ?
    1. characters
    2. strings
    3. objects
  3. Which instance variable of an array is used to find out how big it is ?
    1. size
    2. length
    3. MAX_VALUE

Answers

  1. A. In a do-while loop, the while conditional statement appears at the end of the loop. Even if it is initially false, the statement in the loop are executed once.
  2. C. It use to be true that strings could not be used as a test, but that is no longer the case.
  3. B. The length variable is an integer that returns the array's size. (how many elements are in the array)

Certification Practice

  1. What will the value of x when it is displayed ?
    1. 9.0
    2. 11.0
    3. 15.0
    4. The program will not compile.

Exercises



Reserve