Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
// Java0501.java // This program (and the next few programs) demonstrate user keyboard input JAVA0501.JAVA // during program execution. This particular program demonstrates keyboard // input of a string in a text window using the <Expo> class. Enter name ===>> Isolde Schram public class Java0501 { Name Entered: Isolde Schram public static void main (String args[]) { System.out.println(); System.out.println("JAVA0501.JAVA"); System.out.println(); System.out.print("Enter name ===>> "); String name = Expo.enterString(); System.out.println(); System.out.println("\nName Entered: System.out.println(); // Line 1 // Line 2 " + name); } } 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. // Java0502.java JAVA0502.JAVA for // This program demonstrates how to use Expo.enterString() // three separate String keyboard inputs. Enter Line 1 Enter Line 2 Enter Line 3 public class Java0502 { public static void main (String args[]) Austrid { Ingrid System.out.println("\nJAVA0502.JAVA\n"); Raymond 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(); } } ===>> ===>> ===>> Austrid Ingrid Raymond // Java0503.java // This program demonstrates <String> objects concatenation with JAVA0503.JAVA // keyboard entered data. Enter 1st Number public Java0503 Enterclass 2nd Number { ===>> ===>> 100 200 public static void main (String args[]) 100 + 200 = 100200 { 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(); } } 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. // Java0504.java // This program uses the Expo.enterInt() method to enter integers from JAVA0504.JAVA // the keyboard. It is now possible to correctly add the two numbers. Enter 1st Number public Java0504 Enterclass 2nd Number { ===>> ===>> 100 200 public static void main (String args[]) 100 + 200 = 300 { 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(); } } // Java0505.java // This program demonstrates how to use Expo.enterDouble() for three JAVA0505.JAVA // separate double keyboard inputs, which are used to display the mean. Enter Number 1 ===>> public class Java0505 Enter Number 2 ===>> { Enter Number 3 ===>> public static void main (String args[]) { 1.1 System.out.println("\nJAVA0505.JAVA\n"); 22.22 System.out.print("Enter Number 1 ===>> "); 333.333 1.1 22.22 333.333 double n1 = Expo.enterDouble(); System.out.print("Enter Number 2 ===>> "); The mean is 118.88433333333334 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(); } } // Java0506.java JAVA0506.JAVA // This program demonstrates how to use Expo.enterChar() which is ideal // for entering a single letter. Enter First Name: ===>> John Enter Middle Initial: ===>> Q public class Java0506 Enter Last Name: ===>> Public { public static void main (String args[]) Your full name is: John Q. Public { 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(); } } 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. Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure. Types of Control Structures • Simple Sequence • Selection also called: - • Decision Making Conditional Branching Alternation Repetition also called: - Looping Iteration Simple Sequence Program Statement Program Statement Program Statement Program Statement One-Way Selection Program Statement Condition True False Program Statement Program Statement Program Statement Two-Way Selection Program Statement True Condition False Program Statement Program Statement Program Statement Program Statement Multiple-Way Selection Selection Variable Selection Constant No Match Selection Constant No Match Selection Constant Match Match Match No Match Program Statement Program Statement Program Statement Program Statement Repetition Program Statement Program Statement Program Statement Condition True False Program Statement 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). Relational Operators Name Operator Expression Evaluates Greater than == != < > Less than or equals <= Greater than or equals >= Equals Not Equals Less than 5 == 5 5 == 10 true false 50 != 25 100 != 100 true false 100 < 200 200 < 100 200 > 100 200 > 200 100 <= 200 200 <= 200 200 <= 100 100 >= 200 200 >= 200 200 >= 100 true false true false true true false false true true Important Note: The relational operators shown on the previous slide will be used in the Java example programs that demonstrate the different control structures. Be careful not to confuse the equality operator ( == ) with the assignment operator ( = ). Assignment ( = ) Equality ( == ) int x = 10; if (x == 10) Assigns a the value of 10 to x. Checks if x is equal to 10. // Java0507.java JAVA0507.JAVA // This program demonstrates one-wayJAVA0507.JAVA selection with <if>. // Run the program twice. First with Sales equals to 300,000 Enter Sales time ===>> 300000 Enter Sales ===>> // and a second with Sales equals 500,000. Christmas bonus: 0.0 Christmas bonus: 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: System.out.println(); } } " + bonus); 500000 1000.0 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; 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! // Java0508.java // This program demonstrates one-way selectionJAVA0508.JAVA with <if>. JAVA0508.JAVA // It also shows that only one statement is controlled. Run the program twice. // FirstSales with ===>> Sales equals time with Sales500000 equals to 500,000. Enter 300000to 300,000 and then a 2nd Enter Sales ===>> CONGRATULATIONS! public class You sold halfJava0508 a million dollars in merchandise! {You will receive a $1000 Christmas Bonus! public static void main (String args[]) Keep up the good work! { CONGRATULATIONS! You sold half a million dollars in merchandise! You will receive a $1000 Christmas Bonus! Keep up the good work! System.out.println("\nJAVA0508.JAVA\n"); 1000.0 System.out.print("Enter Sales ===>> "); double sales = Expo.enterDouble(); Christmas bonus: 1000.0 double bonus = 0.0; Christmas bonus: if (sales >= 500000.0) System.out.println(); 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: System.out.println(); } } " + bonus); // Java0509.java JAVA0509.JAVA 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. Enter Sales ===>> 300000 Enter Sales ===>> 500000 public class Java0509 {Christmas bonus: 0.0 CONGRATULATIONS! public static void main (StringYou args[]) sold half a million dollars in merchandise! { You will receive a $1000 Christmas Bonus! System.out.println("\nJAVA0509.JAVA\n"); Keep up the good work! System.out.print("Enter Sales ===>> "); double sales = Expo.enterDouble(); Christmas bonus: 1000.0 double bonus = 0.0; if (sales >= 500000.0) { System.out.println(); 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: System.out.println(); } } " + bonus); One-Way Selection General 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"); } Two-Way Selection Real Life Example I35W takes you to Fort Worth. I35E takes you to Dallas. Interstate 35 splits into I35W and I35E just North of Hillsboro. // Java0510.java // This program demonstrates two-wayJAVA0510.JAVA selection with <if..else>. JAVA0510.JAVA // Run the program twice: First with 1200, then with 1000. Enter SAT ===>> 1200 Enter SAT ===>> 1000 public class Java0510 { Youpublic arestatic admitted You are not admitted 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"); else System.out.println("You are not admitted"); System.out.println(); } } // Java0511.java JAVA0511.JAVA JAVA0511.JAVA // This program demonstrates two-way selection with <if..else>. // Multiple statements require the use of block structure. Enter SATprogram ===>>twice: 1100First with 1100, Enter SAT 1099. ===>> 1099 // Run the then with public class Java0511 {You are admitted You are not admitted public static voidstart main (String args[])Please try again when your SAT improves Orientation will in June { 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(); Two-Way Selection General Syntax: if (condition true) execute first program statement else // when condition is false execute second program statement Specific Example: if (gpa >= 90.0) System.out.println ("You’re an honor graduate"); else System.out.println ("You’re not an honor graduate"); Multi-Way Selection Real Life Example // 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(); } } JAVA0512.JAVA Enter Letter Grade 90 .. 80 .. 70 .. 60 .. Below ===>> A ===>> C ===>> F 100 Average 89 Average 79 Average 69 Average 60 Average JAVA0512.JAVA Enter Letter Grade 70 .. 79 Average 60 .. 69 Average Below 60 Average JAVA0512.JAVA Enter Letter Grade Below 60 Average // 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"); 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"); default : System.out.println("No Match Found"); } System.out.println(); } } break; break; break; break; break; JAVA0513.JAVA Enter Letter Grade JAVA0513.JAVA ===>> A Enter Letter Grade 90 .. 100 Average 80 .. 89 Average JAVA0513.JAVA JAVA0513.JAVA Enter Letter Grade ===>> C Enter Letter Grade 70 .. 79 Average 60 .. 69 Average JAVA0513.JAVA JAVA0513.JAVA Enter Letter Grade Below 60 Average ===>> F Enter Letter Grade No Match Found ===>> B ===>> D ===>> Q 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. // 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"); } JAVA0514.JAVA Enter Letter Grade ===>> B ===>> C ===>> d 80 .. 89 Average Good JAVA0514.JAVA Enter Letter Grade 70 .. 79 Average Fair JAVA0514.JAVA Enter Letter Grade No Match Found // 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"); } JAVA0515.JAVA Enter Letter Grade ===>> A ===>> a 90 .. 100 Average Excellent! JAVA0515.JAVA Enter Letter Grade 90 .. 100 Average Excellent! // 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(); } } JAVA0516.JAVA What grade are you in? JAVA0516.JAVA ===>> 9 What grade are you in? Freshman Sophomore JAVA0516.JAVA JAVA0516.JAVA What grade are you in? Junior ===>> 11 What grade are you in? ===>> 10 ===>> 12 ===>> 3 Senior JAVA0516.JAVA What grade are you in? You are not in high school. // 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"); } JAVA0517.JAVA What grade are you in? JAVA0517.JAVA ===>> 7 What grade are you in? Middle School High School JAVA0517.JAVA JAVA0517.JAVA What grade are you in? ===>> 2 What grade are you in? Elementary School College JAVA0517.JAVA JAVA0517.JAVA What grade are you in? Graduate School ===>> 18 What grade are you in? Elementary School ===>> 11 ===>> 13 ===>> 0 // Java0518.java JAVA0518.JAVA // This program demonstrates multi-way selection can // also be controlled with a <String> variable. Enter first of Leon Schram's children. // This isthe a new featurename with of Javaone Version 7 (jdk 1.7.0). ===>> Maria This Schram's older daughter. public is classMr. 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(); } } // 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; 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; 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."); } JAVA0519.JAVA Enter the first name of someone in Leon Schram's family. ===>> Braxton ===>> Blake This is one of Mr. Schram's grandsons. JAVA0519.JAVA Enter the first name of someone in Leon Schram's family. This is one of Mr. Schram's nephews. Multiple-Way Selection General Syntax switch(selectionVariable) { case selectionConstant: program statement; program statement; : : : break; case selectionConstant: program statement; program statement; : : : break; default program statement; program statement; : : : } Multiple-Way Selection Specific 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. // Java0520.java JAVA0520.JAVA // This program displays 40 identical lines very inefficiently Eat at Joe's friendly Eat at Joe's friendly // with 40 separate println statements. Eat at Joe's friendly Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat Eat at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at at Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's Joe's friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly friendly diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner diner for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for for the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best best lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch lunch public class Java0520 { public static void main(String args[]) { System.out.println("\nJAVA0501.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"); : : : : : : : : : : : : : : : : value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value value // 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"); } } // 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). // 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. // 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. // Java0522.java JAVA0522.JAVA // This program displays consecutive numbers 1 through 15. // 1 It2also3 shows 4 5how 6 the 7 loop 8 control 9 10 variable 11 12 may 13 be14 15 // 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(); } } Defining the Loop Control Variable Before the loop heading (not used much) int k; for (k = 1; k <= 10; k++) { System.out.println("Hello World"); } Inside the loop heading (preferred approach) for (int k = 1; k <= 10; k++) { System.out.println("Hello World"); } // Java0523.java JAVA0523.JAVA // This program demonstrates how to use block #################################### structure Line Number 1 // with a <for> loop control structure. #################################### Line Number 2 #################################### Line Number 3 public class Java0523 #################################### Line Number 4 { #################################### Line Number 5 public static void main(String args[]) #################################### Line Number 6 { #################################### Line Number 7 System.out.println("\nJAVA0523.JAVA\n"); #################################### Line Number 8 int k; #################################### Line Number 9 #################################### Line Number 10 for (k = 1; k <= 10; k++) { System.out.println("####################################"); System.out.println("Line Number " + k); } System.out.println(); } } // Java0524.java JAVA0524.JAVA // This program displays various counting schemes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // It also demonstrates the versatility of the <for> loop. 1 4 7 10 13 public class Java0524 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 { 0.0 public 0.5 static 1.0 1.5 2.0 2.5 3.0 void main(String args[]) A B{ C D E F G H I J K L M N O P Q R S T U V W X Y Z System.out.println("\nJAVA0524.JAVA\n"); for (int p = 1; p <= 15; p++) System.out.print(p + " "); You do NOT always have to use System.out.println("\n"); ++ in the 3rd part of a for loop. for (int q = 1; q <= 15; q+=3) System.out.print(q + " "); You can count by any amount. System.out.println("\n"); for (int r = 15; r >= 1; r--) System.out.print(r + " "); You can count backwards. System.out.println("\n"); for (double s = 0; s <= 3; s+=0.5) System.out.print(s + " "); You can count by fractional System.out.println("\n"); amounts. for (char t = 'A'; t <= 'Z'; t++) System.out.print(t + " "); You can even count with System.out.println("\n\n"); } characters. } Fixed Repetition Java 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: for (Part1; Part2; Part3) loop body; 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 (k = 1; k <= 10; k++) System.out.println("Java is 10 times more fun"); Conditional Repetition Real Life Examples JAVA0525.JAVA // Java0525.java // This program is supposed to keep repeating until a correct PIN# of 5678 is entered. Enteris4 used digit PIN. 1234 // The program does not work because the <for> loop in an-->inappropriate manner. // The <for> loop is meant for "fixed" repetition. That is not the correct PIN. Try Again. // Entering a PIN# is an example of "conditional" repetition. Enter 4 digit PIN. --> 2345 public class Java0525 That is not the correct PIN. Try Again. { Enter 4 digit PIN. --> 3456 public static void main(String args[]) { That is not the correct PIN. Try Again. System.out.println("\nJAVA0525.JAVA\n"); Enter 4 digit PIN. --> 4567 for (int j = 1; j <= 10; j++) That is not the correct PIN. Try Again. { Enter 4 digit PIN. --> 5678 System.out.println(); Enter 4PIN#. digit PIN. --> --> 5678 System.out.print("Enter 4 digit "); int pin = Expo.enterInt(); Enter 4 digit PIN. --> 5678 if (pin != 5678) Enter 4 digit PIN. --> 5678 System.out.println("That is not the correct PIN. Try Again."); Enter 4 digit PIN. --> 5678 } Enter 4 digit PIN. --> 0000 System.out.println("\nYou are now logged in. Welcome to the program."); } } That is not the correct PIN. Try Again. You are now logged in. Welcome to the program. JAVA0526.JAVA // Java0526.java // This program fixes the problem of the previous program by using a while command. PIN. --> 1234 // Now the loop will stop when the correct PIN# of Enter 5678 4isdigit entered. That is not the correct PIN. Try Again. public class Java0526 Enter 4 digit PIN. --> 2345 { public static void main(String args[]) That is not the correct PIN. Try Again. { Enter 4 digit PIN. --> 3456 System.out.println("\nJAVA0526.JAVA\n"); int pin = 0; That is not the correct PIN. Try Again. Enter 4 digit PIN. --> 4567 while (pin != 5678) That is not the correct PIN. Try Again. { Enter 4 digit PIN. --> 5678 System.out.println(); are nowPIN#. logged in. Welcome System.out.print("Enter 4You digit --> ");to the program. 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."); } } // 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"); } } Java0527.java Output The output may go on for several seconds… JAVA0527.JAVA 1666 3008 7042 1893 6421 9526 5382 7496 2699 2058 7617 6891 7319 3686 6950 6459 2149 6628 2719 5939 3539 4915 9914 2370 9808 7126 8482 6248 9386 3366 1153 6266 3113 6779 8070 6094 6373 4617 5644 2728 8055 5389 7109 8389 9644 7869 8526 4950 7399 9539 3266 9091 2184 8462 4930 2348 8660 3624 1431 5678 1325 6142 6819 4836 8262 9406 3952 5356 7056 3238 5810 7830 2943 1840 2884 6795 2035 1732 3986 7170 8310 6828 5186 9423 2740 2081 4166 1571 7061 4750 1411 4040 3152 2983 5297 8291 5881 5552 2439 3473 1370 4232 5840 5603 5117 9702 3262 8197 3682 7749 3066 3245 2250 2880 4205 4923 5333 6078 9605 5234 9744 5275 3737 9888 1182 4087 5694 6914 1853 9601 8531 1536 6384 2101 4637 9771 7848 5569 9402 7614 8142 4592 4077 3052 8236 1826 6560 6448 5392 8392 6628 3881 9382 8391 3240 6814 4276 3848 6742 3691 1969 9210 7312 1931 3881 7038 3602 5372 4244 5506 6482 7359 2608 4759 6484 7156 5219 5666 1487 7495 4889 5974 3487 3271 7498 1475 8626 1196 7860 2686 9572 3241 5370 7485 5513 4364 7414 9337 8525 3050 7286 9218 7682 2017 6749 8477 1702 8162 4905 1294 9701 5461 6737 6065 3506 4588 7586 3661 7490 2519 6939 5147 1679 4926 7115 5912 9647 2940 6593 7777 8926 1898 3027 5443 4816 4263 8277 7441 4392 6777 8590 3699 8064 1772 4280 9079 2744 8750 7102 5042 … but it will eventually stop at 5678. 9823 6741 9991 4327 4332 8240 3269 3861 2764 2517 7012 2731 5717 6712 3108 3734 7537 7124 1898 Flags The 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. Conditional Repetition General Syntax: initialize condition variable before the while loop while(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."); Program Segment NoNo #1 Program Segment YesYes #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. Program Segment NoNo #2 Program Segment YesYes #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. 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. // Java0528.java // This program demonstrates that <for> loopJAVA0528.JAVA structures // can be nested. It will display HELLO WORLD 6 times. HELLO public class Java0528 HELLO { HELLO public static void main(String args[]) { HELLO System.out.println("\nJAVA0528.JAVA\n"); HELLO for (int p = 1; p <= 3; p++) HELLO { for (int q = 1; q <= 2; q++) { System.out.println("HELLO WORLD"); } } System.out.println("\n\n"); } } WORLD WORLD WORLD WORLD WORLD WORLD // Java0529.java JAVA0529.JAVA // This program displays the values of the two OuterLCV loop values. control variable is 1 Inner loop control variable is 1 public class Java0529 Inner loop control variable is 2 { Outer loop control variable is 2 public static void main(String args[]) Inner loop control variable is 1 { Inner loop control variable is 2 System.out.println("\nJAVA0529.JAVA\n"); for (int p = 1; p <= 3; p++) Outer loop control variable is 3 { Inner loop control variable is 1 System.out.println("Outer loopInner control is " + p); loop variable control variable is 2 System.out.println(); for (int q = 1; q <= 2; q++) { System.out.println("Inner loop control variable is " + q); System.out.println(); } } System.out.println("\n\n"); } } // Java0530.java JAVA0530.JAVA // This program displays a multiplication table with 10 rows and 15 columns. 2 3 the 4 5 columns 6 7 8 9 10 13 up 14 perfectly. 15 // Do not worry about the fact1that do 11 not12line 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 // You will learn how to do that3 in chapter. 6 9another 12 15 18 21 24 27 30 33 36 39 42 45 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 public class Java0530 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 { 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 public static void main(String args[]) 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 { 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 System.out.println("\nJAVA0530.JAVA\n"); for (int r = 1; r <= 10; r++) { for (int c = 1; c <= 15; c++) { int product = r * c; System.out.print(product + " "); } System.out.println(); } System.out.println("\n\n"); } } // Java0531.java JAVA0531.JAVA // This program displays 3 multiplication tables. // Each of the multiplication tables displays a 5 10 15 20 25 // random table value is the [2,10] range. 30 35 40 45 50 2 4 6 8 10 12 14 16 18 20 public class Java0531 6 12 18 24 30 36 42 48 54 60 { public static void main(String args[]) { JAVA0531.JAVA System.out.println("\nJAVA0531.JAVA\n"); for (int p = 1; p <= 3; p++) 3 6 9 12 15 18 21 24 27 30 { int table = Expo.random(2,10); 9 18 27 36 45 54 63 72 81 90 for (int q = 1; q <= 10; q++) 4 8 12 16 20 24 28 32 36 40 { int product = q * table; System.out.print(product + " JAVA0531.JAVA "); } 8 16 24 32 40 48 56 64 72 80 System.out.println("\n\n"); } 10 20 30 40 50 60 70 80 90 100 System.out.println("\n\n"); } 8 16 24 32 40 48 56 64 72 80 } // Java0532.java // This program demonstrates nesting an <if> structure inside a <for> loop. // The previous SAT program is nested in a loop that will repeat 5 times. public class Java0532 { public static void main (String args[]) { System.out.println("\nJAVA0532JAVA\n"); for (int k = 1; k <= 5; k++) { 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(); JAVA0532.JAVA Enter SAT ===>> 900 You are not admitted Please try again when your SAT improves. Enter SAT ===>> 1000 You are not admitted Please try again when your SAT improves. Enter SAT ===>> 1099 You are not admitted Please try again when your SAT improves. Enter SAT ===>> 1100 You are admitted Orientation will start in June Enter SAT ===>> 1200 You are admitted Orientation will start in June // Java0533.java // This program demonstrates nesting an <if> structure inside a <do...while> loop. // The user will be asked if he/she wishes to interview another student. A response of 'Y' // will make the program repeat. Any other response will make the program terminate. public class Java0533 { public static void main (String args[]) { System.out.println("\nJAVA0533.JAVA\n"); char response; do { 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(); System.out.print("Do you want to interview another student? {Y/N} ===>> "); response = Expo.enterChar(); System.out.println(); } while (response == 'Y'); // Note: Only capital 'Y' will make the program repeat. } } JAVA0533.JAVA Enter SAT ===>> 900 You are not admitted Please try again when your SAT improves. Do you want to interview another student? Enter SAT ===>> {Y/N} ===>> Y {Y/N} ===>> Y {Y/N} ===>> Y {Y/N} ===>> N 1000 You are not admitted Please try again when your SAT improves. Do you want to interview another student? Enter SAT ===>> 1100 You are admitted Orientation will start in June Do you want to interview another student? Enter SAT ===>> 1200 You are admitted Orientation will start in June Do you want to interview another student? // Java0534.java // This program demonstrates nesting an <if> structure inside another <if> structure. // This will determine if a student is eligible for financial aid. // Note that this is not an issue for students whose SAT scores are below 1100. System.out.print("Enter SAT ===>> "); int sat = Expo.enterInt(); if (sat >= 1100) { System.out.println("You are admitted"); System.out.println("Orientation will start in June"); System.out.println(); System.out.print("What is your family income? ===>> "); double income = Expo.enterDouble(); if (income <= 20000) { System.out.println("You qualify for financial aid."); } else { System.out.println("You do not qualify for financial aid."); } } else { System.out.println("You are not admitted"); System.out.println("Please try again when your SAT improves."); } JAVA0534.JAVA Enter SAT ===>> 1350 You are admitted Orientation will start in June What is your family income? ===>> 18000 You qualify for financial aid. JAVA0534.JAVA Enter SAT JAVA0534.JAVA Enter SAT ===>> 1500 ===>> You are not admitted Please try again when your SAT improves. You are admitted Orientation will start in June What is your family income? 700 ===>> 90000 You do not qualify for financial aid. // Java0535.java // This program demonstrates nesting <for> loops inside an <if> structure. // The program also shows how to determine is a number is even or odd. System.out.println("\nJAVA0708.JAVA\n"); System.out.print("Enter an integer ===>> "); int number = Expo.enterInt(); System.out.println(); if (number % 2 == 0) // if the number is even { for (int k = 1; k <= 256; k++) { System.out.print("EVEN "); } } else // if the number is odd { for (int k = 1; k <= 256; k++) { System.out.print("ODD "); } } JAVA0535.JAVA Enter an integer ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD JAVA0535.JAVA ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD Enter an integer EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN ===>> ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ===>> EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN 1 ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN 2 EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN // Java0536.java // This program nests the previous program inside a <for> loop. // Now we have <for> loops nested inside an <if> structure nested // inside another <for> loop. for (int j = 1; j <= 5; j++) { if ( j % 2 == 0) // if the loop counter is even { for (int k = 1; k <= 64; k++) { System.out.print("EVEN "); } } else // if the loop counter is odd { for (int k = 1; k <= 64; k++) { } } } System.out.print("ODD "); JAVA0536.JAVA j = 1 ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD ODD j = 2 EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN j = 3 ODD ODD ODD ODD ODD ODD ODD ODD j = 4 EVEN EVEN EVEN EVEN EVEN EVEN EVEN EVEN j = 5 ODD ODD ODD ODD ODD ODD ODD ODD // Java0537.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 Java0537 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; } } } // Java0538.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 Java0538 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; } } } // Java0539.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 Java0539 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; } } } // Java0540.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 Java0540 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; } } } // Java0541.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 Java0541 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; } } }