> "); // Line 1 String name = Expo.enterString(); // Line 2 System.out.println("Name Entered: " + 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."> > "); // Line 1 String name = Expo.enterString(); // Line 2 System.out.println("Name Entered: " + 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.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 5.1 Keyboard Input.

Similar presentations


Presentation on theme: "Section 5.1 Keyboard Input."— Presentation transcript:

1 Section 5.1 Keyboard Input

2 // 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.print("Enter name ===>> "); // Line 1 String name = Expo.enterString(); // Line 2 System.out.println("Name Entered: " + 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.

3 // 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); }

4 String sum = number1 + number2;
// 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); }

5 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.

6 int sum = number1 + number2;
// 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); }

7 double mean = (n1 + n2 + n3) / 3;
// 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("The mean is " + mean); }

8 char middleInitial = Expo.enterChar();
// 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); }

9 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.

10 Section 5.2 Introduction to Control Structures

11 Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.

12 Section 5.3 Types of Control Structures

13 Types of Control Structures
Simple Sequence Selection also called: - Decision Making - Conditional Branching - Alternation Repetition also called: - Looping - Iteration

14 Simple Sequence Program Statement Program Statement Program Statement

15 One-Way Selection Program Statement Condition True False

16 Two-Way Selection Program Statement Condition Program Statement
True False Program Statement Program Statement Program Statement Program Statement

17 Multiple-Way Selection
Program Statement Match No Match Selection Constant Selection Variable

18 Repetition Program Statement Program Statement Program Statement
True Condition False Program Statement

19 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).

20 Section 5.4 Relational Operators

21 == != < > <= >=
Relational Operators Name Operator Expression Evaluates Equals == 5 == 5 5 == 10 true false Not Equals != 50 != 25 100 != 100 Less than < 100 < 200 200 < 100 Greater than > 200 > 100 200 > 200 Less than or equals <= 100 <= 200 200 <= 200 200 <= 100 Greater than or equals >= 100 >= 200 200 >= 200 200 >= 100

22 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.

23 Section 5.5 Selection

24 if (sales >= 500000.0) bonus = 1000.0; // 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 >= ) bonus = ; System.out.println(“Christmas bonus: " + bonus); }

25 Indentation Rule: The preferred way if(sales >= 500000)
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. The preferred way if(sales >= ) bonus = 1000; if(sales >=500000) bonus = 1000;

26 Important Note: It also applies to control structure headings!
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!

27 System.out.println("CONGRATULATIONS!");
// 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 >= ) 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 = ; System.out.println("Christmas bonus: " + bonus); }

28 // 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 >= ) 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 = ; } System.out.println("Christmas bonus: " + bonus);

29 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"); }

30 Section 5.6 Two Way Selection

31 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.

32 System.out.println("You are admitted"); else
// 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"); else System.out.println("You are not admitted"); }

33 System.out.println("You are admitted");
// 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.");

34 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");

35 Section 5.7 Multi-Way Selection

36 Multi-Way Selection Real Life Example

37 case 'A' : System.out.println("90 .. 100 Average");
// 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(" Average"); case 'B' : System.out.println(" Average"); case 'C' : System.out.println(" Average"); case 'D' : System.out.println(" Average"); case 'F' : System.out.println("Below 60 Average"); }

38

39 public static void main (String args[])
// 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(" Average"); break; case 'B' : System.out.println(" Average"); break; case 'C' : System.out.println(" Average"); break; case 'D' : System.out.println(" Average"); break; case 'F' : System.out.println("Below 60 Average"); break; default : System.out.println("No Match Found"); }

40

41 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.

42 // This program demonstrates that multiple program statements
// 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(" Average"); System.out.println("Excellent!"); break; case 'B' : System.out.println(" Average"); System.out.println("Good"); case 'C' : System.out.println(" Average"); System.out.println("Fair"); case 'D' : System.out.println(" Average"); System.out.println("Poor"); case 'F' : System.out.println("Below 60 Average"); System.out.println("Bad"); default : System.out.println("No Match Found"); }

43 // Java0515.java // This program demonstrates how to allow for both capital and lowercase letters, switch (grade) { case 'A' : case 'a' : System.out.println(" Average"); System.out.println("Excellent!"); break; case 'B' : case 'b' : System.out.println(" Average"); System.out.println("Good"); case 'C' : case 'c' : System.out.println(" Average"); System.out.println("Fair"); case 'D' : case 'd' : System.out.println(" Average"); System.out.println("Poor"); case 'F' : case 'f' : System.out.println("Below 60 Average"); System.out.println("Bad"); default : System.out.println("No Match Found"); }

44 case 9 : System.out.println("Freshman"); break;
// 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."); }

45

46 // 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"); case 9 : case 10 : case 11 : case 12 : System.out.println("High School"); case 13 : case 14 : case 15 : case 16 : System.out.println("College"); default : System.out.println("Graduate School"); }

47

48 // This program demonstrates multi-way selection can
// 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."); }

49 // 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."); case "Maria" : case "Heidi" : System.out.println("This is one of Mr. Schram's daughters."); case "Mike" : case "David" : System.out.println("This is one of Mr. Schram's sons-in-law.");

50 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."); case "Anthony" : case "Alec" : case "Maddox" : case "Jaxon" : case "Braxton" : System.out.println("This is one of Mr. Schram's grandsons."); case "Austrid" : case "Ingrid" : System.out.println("This is one of Mr. Schram's sisters."); case "Remy" : System.out.println("This is Mr. Schram's brother.");

51 System.out.println("This is one of Mr. Schram's nieces."); 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."); 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."); }

52 Multiple-Way Selection General Syntax
switch(selectionVariable) { case selectionConstant : program statement; : : : break; default }

53 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.

54 Section 5.8 Fixed Repetition

55 // 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"); : : : : : : : : : : : : : : : :

56 What if you want to display the message 100 or 1000 or
// 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"); } What if you want to display the message 100 or 1000 or 1 million times? Output is the same as the previous program.

57 Another look at the 3 parts of a for loop
// 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"); } Another look at the 3 parts of a for loop Part 1 is used to initialize the counter (Loop Control Variable).

58 Another look at the 3 parts of a for loop
// 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"); } Another look at the 3 parts of a for loop 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.

59 Another look at the 3 parts of a for loop
// 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"); } Another look at the 3 parts of a for loop 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.

60 System.out.print(k + " "); // 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(); }

61 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"); }

62 System.out.println("####################################");
// 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();

63 You do NOT always have to use ++ in the 3rd part of a for loop.
// 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 + " "); for (int r = 15; r >= 1; r--) System.out.print(r + " "); for (double s = 0; s <= 3; s+=0.5) System.out.print(s + " "); 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.

64 Fixed Repetition for (Part1; Part2; Part3) loop body;
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: 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");

65 Section 5.9 Conditional Repetition

66 Conditional Repetition Real Life Examples

67 for (int j = 1; j <= 10; j++) System.out.println();
// 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.");

68 System.out.println(“That is not the correct PIN. Try Again.");
// 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.");

69 // 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");

70 Java0527.java Output The output may go on for several seconds… … but it will eventually stop at 5678.

71 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.

72 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.");

73 Program Segment NoNo #1 YesYes #1 int x = 0; while(x < 10)
System.out.println(x); { 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.

74 Program Segment NoNo #2 YesYes #2 int x; while(x < 10) { x++;
System.out.println(x); } int x = 0; 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.

75 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.

76 Section 5.10 Control Structures with Graphics

77 // 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; }

78 // 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; }

79 // 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; }

80 // 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; }

81 // 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; }


Download ppt "Section 5.1 Keyboard Input."

Similar presentations


Ads by Google