

The good news is: you have learned all of them now !
However, you still need to learn how to use them effectively


We will now learn how to use the power of a programming language (Java) by combining (nesting) different statements







We will use this notation to expression some of our algorithms.
input: n = some integer number 
Note:

The algorithm contains some abstract steps that need to be fleshed out (work out the details)

Apply this programming technique to obtain a refined algorithm:
Now the algorithm contain only (concrete) steps that can be easily translated into a Java program !!!
import java.util.Scanner; public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int x; System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number x = 1; while ( x <= n ) // Run x = 1, 2, ..., n { if ( n % x == 0 ) { // x is a divisor of n System.out.println(x); // Print x (because it's a divisor) } x++; // Make sure we more to the next number !! // or else: infinite loop !!! } } } 
How to run the program:

for every possible value x do { if ( x is a solution ) print x; } 




input x, y; min = min(x, y); // this is the range of the brute force search for (a = 1, 2, ...., min) do { if (x and y are divisible by a) { print a; } } 
import java.util.Scanner; public class Divisors02 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x, y, a, min = 0; x = in.nextInt(); // Read in x y = in.nextInt(); // Read in y if ( x < y ) // Find min(x,y) min = x; else min = y; a = 1; while ( a <= min ) // Run a = 1, 2, ..., min(x,y) { if ( x % a == 0 && y % a == 0 ) { // a is a divisor of x and y System.out.println(a); // Print a (because it's a common divisor) } a++; // Make sure we move to the next number !! // or else: infinite loop !!! } } } 
How to run the program:



for ( every possible position i ) { if ( character at position i in string x == 'a ) { position i is a correct solution } } 
Ending position to search: x.length() − 1 !!!
import java.util.Scanner; public class FindChar01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String x; /* =========================================== Read in the word and store it in variable x ============================================ */ System.out.print("Enter a word x = "); x = in.next(); // Read in a word int i; i = 0; while ( i <= x.length()  1 ) // Run i = 0, 1, ..., x.length()1 { if ( x.charAt(i) == 'a' ) { // found 'a' at position i System.out.println(i); // Print position i } i++; // Make sure we more to the next position !! // or else: infinite loop !!! } } } 
How to run the program:




input x; a = 2; < Try use a = 2 as factor for x as long as x is not equal to 1 do { if (x is divisible by a) { // a is a factor of x !!! print a; (Because we have found another factor) Remove factor a from the number x (and continue) } else { Try use a+1 as factor } } 

import java.util.Scanner; public class Factor01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x; int a; System.out.print("Enter a number x: "); x = in.nextInt(); // Read in number a = 2; while ( x > 1 ) { if ( x % a == 0 ) { // a is a factor of x System.out.println(a); // Print a (because it's a divisor) x = x / a; // Remove factor a from x } else { a++; // Use next number as factor } } } } 
How to run the program:


Divide: 37 by 7 Initially: Quotient = 0 Remainder = 37 Is Remainder >= 7 ? Yes: Quotient = 1, Remainder = 30 Is Remainder >= 7 ? Yes: Quotient = 2, Remainder = 23 Is Remainder >= 7 ? Yes: Quotient = 3, Remainder = 16 Is Remainder >= 7 ? Yes: Quotient = 4, Remainder = 9 Is Remainder >= 7 ? Yes: Quotient = 5, Remainder = 2 Is Remainder >= 7 ? NO Answer: Quotient = 5, Remainder = 2 
Input A, B Q = 0; R = A; as long as ( R >= B ) { Q = Q + 1; R = R  B; } Print Q, R 
import java.util.Scanner; public class Divide01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int A, B; int Q, R; System.out.print("Enter A = "); A = in.nextInt(); // Read in number System.out.print("Enter B = "); B = in.nextInt(); // Read in number Q = 0; R = A; while ( R >= B ) { Q = Q + 1; R = R  B; } System.out.println("Quotient = " + Q); System.out.println("Remainder = " + R); } } 
How to run the program:
