1. Netbeans

1.1. Introduction

  • Netbeans is an Integrated Development Environment (IDE).

  • Download from https://netbeans.org/downloads/ (please choose the Java EE bundle)

  • Have fun with psvm, sout_, soutv, for and other completion tricks.

    • More keyboard shortcuts can be found via: Help > Keyboard Shortcuts Card in Netbeans (opens a printable PDF file)

1.2. Netbeans naming conventions

The convention is that the Java source files land beneath a src folder. For instance, for exercise ch05 Exercise 5.1 the last part of the path will be wk04/ch05/src/Exercise5_1.java. Most of the directory related work will be taken care of by Netbeans, if you use it properly.

1.3. Project type

New projects in Netbeans must be created as Java Class Library, so netbeans makes no assumptions about class and file names. You will have to create all Java files using the new class wizard, and use keyboard short-cuts like psvm to create the public static void main(String[] args) method.

2. while

  • Do something as long as a condition is true.

  • loop continuation condition

  • termination: make sure it stops.

    • condition should change and become false, typically by the progress of the loop.

  • Simple: Writing penal labor, PenalLabor.java

  • The condition may never be true: loop body is NOT executed.

  • The condition can be part of the business, truthness.

  • example: RepeatAdditionQuiz.java

while_example_flow_chart
Figure 1. while loop example [1];
while_general_flow_chart
Figure 2. while loop in general [1];

3. do while

  • executes at least once.

  • condition is evaluated at end.

  • You can almost always write a while-do as do-while.

do_while_general_flow_chart
Figure 3. do while loop in general [1];

4. for

  • for-i is abbreviation for initialisation test advance

Example for loop
for (int i = 0; i < 100; i++) {
  System.out.println("Welcome to Java!");
}
for loop in general
for (initial-action; loop-continuation-condition; action-after-each-iteration) {
     // Loop body;
    Statement(s);
}
for_example_flow_chart
Figure 4. for loop example [1];
for_general_flow_chart
Figure 5. for loop in general [1];

5. while vs. for

while_for_equivalent
Figure 6. The for equivalent of a while loop [1];
for_while_equivalent
Figure 7. The while equivalent of a for loop [1];

6. Nested loops

  • Just as about any block, a while (or for) can be inside another while (or for).

  • The number of executions is the product of the inner and outer counter. Often \(n^2\).

  • Example multiplication table (improved) MultiplicationTable.java

7. Early stop or skip part


1. Liang, Y Daniel. Intro to Java Programming, Comprehensive Version, Global Edition, 10th Edition. Pearson (Intl), 11/2014. VitalBook file.