Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during...

81

Transcript of Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during...

Page 1: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 2: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0501.java// This program (and the next few programs) demonstrate user keyboard input// during program execution. This particular program demonstrates keyboard // input of a string in a text window using the <Expo> class.

public class Java0501{

public static void main (String args[]) {

System.out.println(); System.out.println("JAVA0501.JAVA"); System.out.println();

System.out.print("Enter name ===>> "); // Line 1String name = Expo.enterString(); // Line 2System.out.println();System.out.println("Name Entered: " + name);System.out.println();

}}

Line 1 is called the prompt. It asks or “prompts” the user for information.

Line 2 is the line that actually enters the String and stores it in name.

Page 3: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0502.java// This program demonstrates how to use Expo.enterString() for // three separate String keyboard inputs. public class Java0502{

public static void main (String args[]){

System.out.println("\nJAVA0502.JAVA\n");System.out.print("Enter Line 1 ===>> ");String input1 = Expo.enterString();System.out.print("Enter Line 2 ===>> ");String input2 = Expo.enterString();System.out.print("Enter Line 3 ===>> ");String input3 = Expo.enterString();System.out.println();System.out.println(input1);System.out.println(input2);System.out.println(input3);System.out.println();

}}

Page 4: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0503.java// This program demonstrates <String> objects concatenation with // keyboard entered data.

public class Java0503{

public static void main (String args[]) {

System.out.println("\nJAVA0503.JAVA\n");System.out.print("Enter 1st Number ===>> ");String number1 = Expo.enterString();System.out.print("Enter 2nd Number ===>> ");String number2 = Expo.enterString();

String sum = number1 + number2;

System.out.println();System.out.println(number1 + " + " + number2 + " = " + sum);System.out.println();

}}

Page 5: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Addition vs. Concatenation

The problem with the previous program is that Strings were used instead of ints or doubles.

When the plus sign ( + ) is used with a numerical value, like an int or a double, it performs addition.

However, when the plus sign is used with Strings, it joins the Strings together.

This is called String Concatenation.

Page 6: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0504.java// This program uses the Expo.enterInt() method to enter integers from // the keyboard. It is now possible to correctly add the two numbers.

public class Java0504{

public static void main (String args[]) {

System.out.println("\nJAVA0504.JAVA\n");System.out.print("Enter 1st Number ===>> ");int number1 = Expo.enterInt();System.out.print("Enter 2nd Number ===>> ");int number2 = Expo.enterInt();

int sum = number1 + number2;

System.out.println();System.out.println(number1 + " + " + number2 + " = " + sum);System.out.println();

}}

Page 7: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0505.java// This program demonstrates how to use Expo.enterDouble() for three // separate double keyboard inputs, which are used to display the mean.

public class Java0505{

public static void main (String args[]) {

System.out.println("\nJAVA0505.JAVA\n");System.out.print("Enter Number 1 ===>> ");double n1 = Expo.enterDouble();System.out.print("Enter Number 2 ===>> ");double n2 = Expo.enterDouble();System.out.print("Enter Number 3 ===>> ");double n3 = Expo.enterDouble();System.out.println();System.out.println(n1);System.out.println(n2);System.out.println(n3);

double mean = (n1 + n2 + n3) / 3;System.out.println();System.out.println("The mean is " + mean);System.out.println();

}}

Page 8: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0506.java// This program demonstrates how to use Expo.enterChar() which is ideal // for entering a single letter.

public class Java0506{

public static void main (String args[]) {

System.out.println("\nJAVA0506.JAVA\n");System.out.print("Enter First Name: ===>> ");String firstName = Expo.enterString();System.out.print("Enter Middle Initial: ===>> ");

char middleInitial = Expo.enterChar();System.out.print("Enter Last Name: ===>> ");String lastName = Expo.enterString();System.out.println();System.out.println("Your full name is: " + firstName + " " +

middleInitial + ". " + lastName);System.out.println();

}}

Page 9: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Expo class Input Methods

Expo.enterInt() is used to enter an int from the text screen.

Expo.enterDouble() is used to enter a double from the text screen.

Expo.enterString() is used to enter a String from the text screen.

Expo.enterChar() is used to enter a char from the text screen.

Page 10: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 11: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Program Flow

Program Flow follows the exact

sequence of listed program statements,

unless directed otherwise by a Java

control structure.

Page 12: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 13: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Types ofControl Structures

• Simple Sequence

• Selection also called:

- Decision Making

- Conditional Branching

- Alternation

• Repetition also called:

- Looping

- Iteration

Page 14: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Simple Sequence

Program Statement

Program Statement

Program Statement

Program Statement

Page 15: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

One-Way Selection

Program Statement

Program Statement

Program Statement

Program Statement

ConditionTrue

False

Page 16: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Two-Way Selection

Program Statement

Program Statement

Program Statement

ConditionTrue False

Program Statement

Program Statement

Page 17: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Multiple-Way Selection

Program Statement

Program StatementMatch

No Match

Program StatementMatch

No Match

Program StatementMatch

No Match

Selection Constant

Selection Constant

Selection Constant

Selection Variable

Page 18: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Repetition

Program Statement

Program Statement

ConditionTrue

False

Program Statement

Program Statement

Page 19: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Conditional Statement Definition

A conditional statement is a program expression that evaluates to true or false.

Most conditional statements require a relational operator.

All conditions must be placed inside (parentheses).

Page 20: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 21: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Relational OperatorsName Operator Expression Evaluates

Equals == 5 == 55 == 10

truefalse

Not Equals != 50 != 25100 != 100

truefalse

Less than < 100 < 200200 < 100

truefalse

Greater than > 200 > 100200 > 200

truefalse

Less than or equals <=

100 <= 200200 <= 200200 <= 100

truetruefalse

Greater than or equals >=

100 >= 200200 >= 200200 >= 100

falsetruetrue

Page 22: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Important Note:Be careful not to confuse the equality operator ( = = ) with the assignment operator ( = ).

Assignment ( = ) Equality ( == )

int x = 10;

Assigns a the value of 10 to x.

if (x == 10)

Checks if x is equal to 10.

Page 23: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 24: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0507.java// This program demonstrates one-way selection with <if>.// Run the program twice. First with Sales equals to 300,000 // and a second time with Sales equals 500,000.

public class Java0507{

public static void main (String args[]) {

System.out.println("\nJAVA0507.JAVA\n");System.out.print("Enter Sales ===>> ");double sales = Expo.enterDouble();double bonus = 0.0;System.out.println();if (sales >= 500000.0)

bonus = 1000.0;System.out.println(“Christmas bonus: " + bonus);System.out.println();

}}

Page 25: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Indentation Rule:Java syntax uses freeform program style. Program statements may be placed on multiple lines with or without indentation.

By convention, control structures and their conditional statements are placed on one line. The program statement that is executed, if the condition is true, is placed on the next line, and indented below the conditional statement.

if(sales >= 500000) bonus = 1000;

if(sales >=500000) bonus = 1000;

Page 26: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Important Note:Headings are NOT program statements and therefore do not get a semicolon!

This applies to class headings and method headings.

It also applies to control structure headings!

Page 27: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0508.java// This program demonstrates one-way selection with <if>.// It also shows that only one statement is controlled. Run the program twice. // First with Sales equals to 300,000 and then a 2nd time with Sales equals to 500,000.

public class Java0508{

public static void main (String args[]) {

System.out.println("\nJAVA0508.JAVA\n");System.out.print("Enter Sales ===>> ");double sales = Expo.enterDouble();double bonus = 0.0;System.out.println();

if (sales >= 500000.0)System.out.println("CONGRATULATIONS!");System.out.println("You sold half a million dollars in merchandise!");System.out.println("You will receive a $1000 Christmas Bonus!"); System.out.println("Keep up the good work!"); bonus = 1000.0;

System.out.println(); System.out.println("Christmas bonus: " + bonus);System.out.println();

}}

Page 28: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0509.java// This program demonstrates one-way selection with <if>.// It fixes the logic problem of the previous program with block structure by using braces.

public class Java0509{

public static void main (String args[]){

System.out.println("\nJAVA0509.JAVA\n");System.out.print("Enter Sales ===>> ");double sales = Expo.enterDouble();double bonus = 0.0;System.out.println();

if (sales >= 500000.0){

System.out.println("CONGRATULATIONS!");System.out.println("You sold half a million dollars in merchandise!");System.out.println("You will receive a $1000 Christmas Bonus!"); System.out.println("Keep up the good work!"); bonus = 1000.0;

}

System.out.println(); System.out.println("Christmas bonus: " + bonus);System.out.println();

}}

Page 29: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

One-Way SelectionGeneral Syntax:

if (condition true) execute program statement

Specific Examples:

if (counter > 100) System.out.println("Counter exceeds 100");

if (savings >= 10000){ System.out.println("It’s skiing time"); System.out.println("Let’s pack"); System.out.println("Remember your skis");}

Page 30: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 31: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Two-Way SelectionReal Life Example

Interstate 35 splits into I35W and I35E just North of Hillsboro.

I35W takes you to Fort Worth.

I35E takes you to Dallas.

Page 32: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0510.java// This program demonstrates two-way selection with <if..else>.// Run the program twice: First with 1200, then with 1000.

public class Java0510{

public static void main (String args[]) {

System.out.println("\nJAVA0510.JAVA\n");System.out.print("Enter SAT ===>> ");int sat = Expo.enterInt();System.out.println();

if (sat >= 1100)System.out.println("You are admitted");

elseSystem.out.println("You are not admitted");

System.out.println();}

}

Page 33: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0511.java// This program demonstrates two-way selection with <if..else>.// Multiple statements require the use of block structure.// Run the program twice: First with 1100, then with 1099.public class Java0511{

public static void main (String args[]){

System.out.println("\nJAVA0511.JAVA\n");System.out.print("Enter SAT ===>> ");int sat = Expo.enterInt();System.out.println();

if (sat >= 1100){

System.out.println("You are admitted");System.out.println("Orientation will start in June");

}else{

System.out.println("You are not admitted");System.out.println("Please try again when your SAT improves.");

}System.out.println();

}}

Page 34: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Two-Way SelectionGeneral Syntax:

if (condition true)execute first program statement

else // when condition is falseexecute second program statement

Specific Example:

if (gpa >= 90.0)System.out.println ("You’re an honor graduate");

elseSystem.out.println ("You’re not an honor graduate");

Page 35: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 36: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Multi-Way SelectionReal Life Example

Page 37: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0512.java// This program demonstrates multi-way selection with <switch> and <case>.// This program compiles, but displays illogical output. public class Java0512{

public static void main (String args[]){

System.out.println("\nJAVA0512.JAVA\n"); System.out.print("Enter Letter Grade ===>> ");

char grade = Expo.enterChar();System.out.println();switch (grade){

case 'A' : System.out.println("90 .. 100 Average");case 'B' : System.out.println("80 .. 89 Average");case 'C' : System.out.println("70 .. 79 Average");case 'D' : System.out.println("60 .. 69 Average");case 'F' : System.out.println("Below 60 Average");

}System.out.println();

}}

Page 38: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 39: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0513.java// This program demonstrates multi-way selection with <switch> and <case>.// The program adds <break> and <default>. The use of <break> is required // for logical output. The <default> case occurs when no other case matches. public class Java0513{

public static void main (String args[]){

System.out.println("\nJAVA0513.JAVA\n"); System.out.print("Enter Letter Grade ===>> ");

char grade = Expo.enterChar();System.out.println();switch (grade){

case 'A' : System.out.println("90 .. 100 Average"); break;case 'B' : System.out.println("80 .. 89 Average"); break;case 'C' : System.out.println("70 .. 79 Average"); break;case 'D' : System.out.println("60 .. 69 Average"); break;case 'F' : System.out.println("Below 60 Average"); break;default : System.out.println("No Match Found");

}System.out.println();

}}

Page 40: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 41: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Program Note

In order to focus on the important and relevant parts of each program, several programs will not be shown in their entirety. Rather, a segment of the program will be shown that focuses on the key point. You have the complete programs on your computer.

Page 42: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0514.java// This program demonstrates that multiple program statements// can be placed between the <case> and the <break> statements. System.out.print("Enter Letter Grade ===>> ");char grade = Expo.enterChar();switch (grade){

case 'A' :System.out.println("90 .. 100 Average");System.out.println("Excellent!");break;

case 'B' :System.out.println("80 .. 89 Average");System.out.println("Good");break;

case 'C' :System.out.println("70 .. 79 Average");System.out.println("Fair");break;

case 'D' :System.out.println("60 .. 69 Average");System.out.println("Poor");break;

case 'F' :System.out.println("Below 60 Average");System.out.println("Bad");break;

default :System.out.println("No Match Found");

}

Page 43: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0515.java// This program demonstrates how to allow for both capital and lowercase letters,

switch (grade){

case 'A' :case 'a' :

System.out.println("90 .. 100 Average");System.out.println("Excellent!");break;

case 'B' :case 'b' :

System.out.println("80 .. 89 Average");System.out.println("Good");break;

case 'C' :case 'c' :

System.out.println("70 .. 79 Average");System.out.println("Fair");break;

case 'D' :case 'd' :

System.out.println("60 .. 69 Average");System.out.println("Poor");break;

case 'F' :case 'f' :

System.out.println("Below 60 Average");System.out.println("Bad");break;

default :System.out.println("No Match Found");

}

Page 44: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0516.java// This program demonstrates multi-way selection can// also be controlled with an <int> variable.

public class Java0516{

public static void main (String args[]){

System.out.println("\nJAVA0516.JAVA\n"); System.out.print("What grade are you in? ===>> ");

int grade = Expo.enterInt();System.out.println();

switch (grade){

case 9 : System.out.println("Freshman"); break;case 10 : System.out.println("Sophomore"); break;case 11 : System.out.println("Junior"); break;case 12 : System.out.println("Senior"); break;default : System.out.println("You are not in high school.");

}System.out.println();

}}

Page 45: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 46: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0517.java// This is a more complicated program where <int> is being used// to control multi-way selection.// In this example multiple cases can yield the same result.

System.out.print("What grade are you in? ===>> ");int grade = Expo.enterInt();

switch (grade){

case 0 : case 1 : case 2 : case 3 : case 4 : case 5 :System.out.println("Elementary School");break;

case 6 : case 7 : case 8 :System.out.println("Middle School");break;

case 9 : case 10 : case 11 : case 12 :System.out.println("High School");break;

case 13 : case 14 : case 15 : case 16 :System.out.println("College");break;

default :System.out.println("Graduate School");

}

Page 47: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 48: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0518.java// This program demonstrates multi-way selection can // also be controlled with a <String> variable.// This is a new feature with Java Version 7 (jdk 1.7.0).

public class Java0518{

public static void main (String args[]){

System.out.println("\nJAVA0518.JAVA\n"); System.out.print("Enter the first name of one of Leon Schram's children. ===>> ");

String firstName = Expo.enterString();System.out.println();

switch (firstName){

case "John" : System.out.println("This is Mr. Schram's older son."); break;case "Greg" : System.out.println("This is Mr. Schram's younger son."); break;case "Maria" : System.out.println("This is Mr. Schram's older daughter."); break;case "Heidi" : System.out.println("This is Mr. Schram's younger daughter."); break;default : System.out.println("This is not one of Mr. Schram's children.");

}System.out.println();

}}

Page 49: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0519.java// This is a more complicated program where <String> is being used// to control multi-way selection.// In this example multiple cases can yield the same result.

System.out.print("Enter the first name of someone in Leon Schram's family. ===>> ");String firstName = Expo.enterString();

switch (firstName){

case "Isolde" :System.out.println("This is Mr. Schram's wife.");break;

case "John" :case "Greg" :

System.out.println("This is one of Mr. Schram's sons.");break;

case "Maria" :case "Heidi" :

System.out.println("This is one of Mr. Schram's daughters.");break;

case "Mike" :case "David" :

System.out.println("This is one of Mr. Schram's sons-in-law.");break;

Page 50: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

case "Diana" :System.out.println("This is Mr. Schram's daughter-in-law.");break;

case "Jessica" :case "Haley" :case "Brenda" :case "Mari" :

System.out.println("This is one of Mr. Schram's granddaughters.");break;

case "Anthony" :case "Alec" :case "Maddox" :case "Jaxon" :case "Braxton" :

System.out.println("This is one of Mr. Schram's grandsons.");break;

case "Austrid" :case "Ingrid" :

System.out.println("This is one of Mr. Schram's sisters.");break;

case "Remy" :System.out.println("This is Mr. Schram's brother.");break;

Page 51: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

case "Darlene" :case "Kassi" :case "Holli" :

System.out.println("This is one of Mr. Schram's nieces.");break;

case "Gene" :case "Sean" :case "Blake" :

System.out.println("This is one of Mr. Schram's nephews.");break;

default :System.out.println("This is not someone in Mr. Schram's immediate family.");System.out.println("Make sure you spell the name correctly and only capitalize the first letter.");

}

Page 52: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Multiple-Way SelectionGeneral Syntax

switch(selectionVariable){ case selectionConstant :

program statement; program statement; : : :break;

 

case selectionConstant : program statement; program statement; : : :break;

 

defaultprogram statement; program statement; : : :

}

Page 53: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Multiple-Way SelectionSpecific Example

switch(courseGrade){ case 'A' : points = 4; break; case 'B' : points = 3; break; case 'C' : points = 2; break; case 'D' : points = 1; break;

case 'F' : points = 0; break; default : System.out.println("Error");}

The default statement is used to handle the situation when a proper match is not found. Frequently an error message is used to indicate that no match was found.

Page 54: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 55: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0520.java// This program displays 40 identical lines very inefficiently// with 40 separate println statements.public class Java0520{

public static void main(String args[]){

System.out.println("\nJAVA0520.JAVA\n");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");System.out.println("Eat at Joe's friendly diner for the best lunch value");: : : : : : : : : : : : : : : :

Page 56: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0521.java// This program displays 40 identical lines efficiently// with one println statement and a loop structure.

public class Java0521{

public static void main(String args[]){

System.out.println("\nJAVA0521.JAVA\n");int k;

for (k = 1; k <= 40; k++) System.out.println("Eat at Joe's friendly diner for the best lunch value");

}}

Page 57: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0521.java// This program displays 40 identical lines efficiently// with one println statement and a loop structure.

public class Java0521{

public static void main(String args[]){

System.out.println("\nJAVA0521.JAVA\n");int k;

for (k = 1; k <= 40; k++) System.out.println("Eat at Joe's friendly diner for the best lunch value");

}} Part 1 is used to

initialize the counter (Loop Control Variable).

Page 58: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0521.java// This program displays 40 identical lines efficiently// with one println statement and a loop structure.

public class Java0521{

public static void main(String args[]){

System.out.println("\nJAVA0521.JAVA\n");int k;

for (k = 1; k <= 40; k++) System.out.println("Eat at Joe's friendly diner for the best lunch value");

}} Part 1 is used to

initialize the counter (Loop Control Variable).

Part 2 is a condition. As long as it is true the loop will keep repeating.

Page 59: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0521.java// This program displays 40 identical lines efficiently// with one println statement and a loop structure.

public class Java0521{

public static void main(String args[]){

System.out.println("\nJAVA0521.JAVA\n");int k;

for (k = 1; k <= 40; k++) System.out.println("Eat at Joe's friendly diner for the best lunch value");

}} Part 1 is used to

initialize the counter (Loop Control Variable).

Part 2 is a condition. As long as it is true the loop will keep repeating.

Part 3 indicates what the counter counts by. ++ means count by 1.

Page 60: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0522.java// This program displays consecutive numbers 1 through 15.// It also shows how the loop control variable may be // defined inside the <for> program statement.

public class Java0522{

public static void main(String args[]){

System.out.println("\nJAVA0522.JAVA\n");

for (int k = 1; k <= 15; k++) System.out.print(k + " ");

System.out.println();}

}

Page 61: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Defining the Loop Control Variable

Before the loop heading (not used much) 

Inside the loop heading (preferred approach)

int k;for (k = 1; k <= 10; k++) {

System.out.println("Hello World");}

for (int k = 1; k <= 10; k++) {

System.out.println("Hello World");}

Page 62: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0523.java// This program demonstrates how to use block structure// with a <for> loop control structure.

public class Java0523{

public static void main(String args[]){

System.out.println("\nJAVA0523.JAVA\n");int k;

for (k = 1; k <= 10; k++){

System.out.println("####################################");System.out.println("Line Number " + k);

}

System.out.println();}

}

Page 63: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0524.java// This program displays various counting schemes.// It also demonstrates the versatility of the <for> loop.

public class Java0524{

public static void main(String args[]){

System.out.println("\nJAVA0524.JAVA\n");for (int p = 1; p <= 15; p++)

System.out.print(p + " ");System.out.println("\n");for (int q = 1; q <= 15; q+=3)

System.out.print(q + " ");System.out.println("\n");for (int r = 15; r >= 1; r--)

System.out.print(r + " ");System.out.println("\n");for (double s = 0; s <= 3; s+=0.5)

System.out.print(s + " ");System.out.println("\n");for (char t = 'A'; t <= 'Z'; t++)

System.out.print(t + " ");System.out.println("\n\n");

}}

You do NOT always have to use ++ in the 3rd part of a for loop.

You can count by any amount.

You can count backwards.

You can count by fractional amounts.

You can even count with characters.

Page 64: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Fixed RepetitionJava has a variety of control structures for repetition.

Other computer science terms for repetition are looping and iteration.

Fixed Iteration is done with the for loop structure.

General Syntax:

The for loop has three distinct parts:

Part1 initializes the Loop Control Variable.Part2 sets the exit condition for the loop.Part3 determines how the LCV changes.

Specific Example:

for (Part1; Part2; Part3) loop body;

for (k = 1; k <= 10; k++) System.out.println("Java is 10 times more fun");

Page 65: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 66: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Conditional RepetitionReal Life Examples

Page 67: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0525.java// This program is supposed to keep repeating until a correct PIN# of 5678 is entered.// The program does not work because the <for> loop is used in an inappropriate manner.// The <for> loop is meant for "fixed" repetition.// Entering a PIN# is an example of "conditional" repetition.

public class Java0525{

public static void main(String args[]){

System.out.println("\nJAVA0525.JAVA\n");

for (int j = 1; j <= 10; j++){

System.out.println();System.out.print("Enter 4 digit PIN#. --> ");int pin = Expo.enterInt();if (pin != 5678)

System.out.println(“That is not the correct PIN. Try Again.");}

System.out.println("\nYou are now logged in. Welcome to the program.");}

}

Page 68: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0526.java// This program fixes the problem of the previous program by using a while command.// Now the loop will stop when the correct PIN# of 5678 is entered.

public class Java0526{

public static void main(String args[]){

System.out.println("\nJAVA0526.JAVA\n");int pin = 0;

while (pin != 5678){

System.out.println();System.out.print("Enter 4 digit PIN#. --> ");pin = Expo.enterInt();if (pin != 5678)

System.out.println(“That is not the correct PIN. Try Again.");}

System.out.println("\nYou are now logged in. Welcome to the program.");}

}

Page 69: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0527.java// This program removes the keyboard input and instead generates// random 4-digit numbers until it is granted access.

public class Java0527{

public static void main(String args[]){

System.out.println("\nJAVA0527.JAVA\n");int randomValue = 0;

while (randomValue != 5678){

randomValue = Expo.random(1000,9999);System.out.print(randomValue + " ");

}

System.out.println("\n\n");}

}

Page 70: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Java0527.java OutputThe output may go on for several seconds…

… but it will eventually stop at 5678.

Page 71: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

FlagsThe flag is the special value that makes the loop stop.

Maybe this value is entered by the user.

Maybe it is computed in the loop.

Whatever special value makes the loop stop is the flag.

A good way to remember that is to think of a race track.

Cars go in a loop again and again. The race keeps going until the checkered flag is waved and the race is over.

Page 72: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Conditional RepetitionGeneral Syntax:

initialize condition variable before the while loopwhile(condition is true){ loop body alter condition variable in the loop body }

Specific Example:

int pin = 0; // initialize condition variable while(pin != 5678){

System.out.print("Enter pin. ===>> ");pin = Expo.enterInt(); // alter condition variable

}System.out.println("Welcome.");

Page 73: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Program SegmentNoNo #1

Program SegmentYesYes #1

int x = 0; while(x < 10) System.out.println(x);

int x = 0; while(x < 10) { x++; System.out.println(x); }

The loop condition variable, x, never changes. The loop will not exit.

The loop condition variable, x, changes. The loop exits when x reaches 10.

Page 74: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Program SegmentNoNo #2

Program SegmentYesYes #2

int x; while(x < 10) { x++; System.out.println(x); }

int x = 0; while(x < 10) { x++; System.out.println(x); }

The loop condition variable, x, is never initialized. This program will not compile in Java.

The loop condition variable, x, is initialized. The program will compile and execute normally.

Page 75: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

Fixed Repetition vs. Conditional Repetition

Fixed Repetition describes a situation where you know – ahead of time – how many times you want the loop to repeat.

An example would be drawing exactly 100 circles on the screen.

The command for fixed repetition is for.

Conditional Repetition describes a situation where you do NOT know how many times the loop will repeat.

The loop has to repeat until some condition is met.

An example would be entering a password.

The command for conditional repetition is while.

Page 76: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Page 77: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0528.java// This program shows how a control structure can be used with graphics.// This program draws vertical lines, because the starting x and // ending x values are the same.

import java.awt.*;import java.applet.*;

public class Java0528 extends Applet{

public void paint(Graphics g){

int x1 = 100;int y1 = 100;int x2 = 100;int y2 = 500;for (int k = 1; k <= 81; k++){

Expo.drawLine(g,x1,y1,x2,y2);x1 += 10;x2 += 10;

} }

}

Page 78: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0529.java// This program shows how a control structure can be used with graphics.// This program draws horizontal lines, because the starting y and // ending y values are the same.

import java.awt.*;import java.applet.*;

public class Java0529 extends Applet{

public void paint(Graphics g){

int x1 = 100;int y1 = 50;int x2 = 900;int y2 = 50;for (int k = 1; k <= 50; k++){

Expo.drawLine(g,x1,y1,x2,y2);y1 += 10;y2 += 10;

} }

}

Page 79: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0530.java// This program shows how a control structure can be used with graphics.// This program draws parallel diagonal lines and changes all 4 variables.

import java.awt.*;import java.applet.*;

public class Java0530 extends Applet{

public void paint(Graphics g){

int x1 = 50;int y1 = 50;int x2 = 200;int y2 = 300;for (int k = 1; k <= 60; k++){

Expo.drawLine(g,x1,y1,x2,y2);x1 += 10;x2 += 10;y1 += 5;y2 += 5;

}}

}

Page 80: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0531.java// This program demonstrates several lines with the same starting point.// In this case the (x1,y1) coordinate stays fixed and the (x2,y2) point changes.

import java.awt.*;import java.applet.*;

public class Java0531 extends Applet{

public void paint(Graphics g){

int x1 = 50;int y1 = 50;int x2 = 900;int y2 = 50;for (int k = 1; k <= 50; k++){

Expo.drawLine(g,x1,y1,x2,y2);y2 += 10;x2 -= 15;

}}

}

Page 81: Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.

// Java0532.java// This program demonstrates several ovals. // All of the ovals have the same center and vertical radius.// The horizontal radius keeps changing.

import java.awt.*;import java.applet.*;

public class Java0532 extends Applet{

public void paint(Graphics g){

int x = 500;int y = 325;int hr = 50;int vr = 100;for (int k = 1; k <= 40; k++){

Expo.drawOval(g,x,y,hr,vr);hr += 10;

}}

}