> ");// Line 1 String name = Expo.enterString();// Line 2 System.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."> > ");// Line 1 String name = Expo.enterString();// Line 2 System.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.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "// Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates."— Presentation transcript:

1

2

3 // 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 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 1 String name = Expo.enterString();// Line 2 System.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.

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

5 // Java0503.java // This program demonstrates 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(); }

6 Addition vs. Concatenation The problem with the previous program is that String s were used instead of int s or double s. 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 String s, it joins the String s together. This is called String Concatenation.

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

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

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

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

11

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

13

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

15 Simple Sequence Program Statement

16 One-Way Selection Program Statement Condition True False

17 Two-Way Selection Program Statement Condition TrueFalse Program Statement

18 Multiple-Way Selection Program Statement Condition True False Program StatementCondition True False Program StatementCondition True False

19 Repetition Program Statement Condition True False Program Statement

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

21

22 Relational Operators NameOperatorExpressionEvaluates Equals == 5 == 5 5 == 10 true false Not Equals != 50 != 25 100 != 100 true false Less than < 100 < 200 200 < 100 true false Greater than > 200 > 100 200 > 200 true false Less than or equals <= 100 <= 200 200 <= 200 200 <= 100 true false Greater than or equals >= 100 >= 200 200 >= 200 200 >= 100 false true

23 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 ( = ).

24

25 // Java0507.java // This program demonstrates one-way selection with. // 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(); }

26 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;

27 // Java0508.java // This program demonstrates one-way selection with. // 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(); }

28 // Java0509.java // This program demonstrates one-way selection with. // 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(); }

29 One-Way Selection Syntax One-Way selection general syntax: if (condition true) execute program statement if (counter > 100) System.out.println("Counter exceeds 100"); Use braces { } and block structure to control multiple program statements. if (savings >= 10000) { System.out.println("It’s skiing time"); System.out.println("Let’s pack"); System.out.println("Remember your skis"); }

30

31 // Java0510.java // This program demonstrates two-way selection with. // 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"); System.out.println(); }

32 // Java0511.java // This program demonstrates two-way selection with. // 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(); }

33 Two-Way Selection Syntax Two-Way selection general syntax: if (condition true) execute first program statement else // when condition is false execute second program statement if (gpa >= 90.0) System.out.println ( "You’re an honor graduate"); else System.out.println ("You’re not an honor graduate");

34

35 // Java0512.java // This program demonstrates multi-way selection with and. // 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(); }

36 // Java0513.java // This program demonstrates multi-way selection with and. // The program adds and. The use of is required // for logical output. The 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(); }

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

38 // Java0514.java // This program demonstrates that multiple program statements // can be placed between the and the 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"); }

39 // Java0515.java // This program demonstrates how to allow 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"); }

40 // Java0516.java // This program demonstrates multi-way selection can also be controlled // with an variable. Both and work with. // Both and do not because they are not "ordinal". 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"); }

41

42 Multiple-Way Selection Syntax Multiple-Way selection General Syntax switch(selectionVariable) { case selectionConstant : program statement(s); break; default : program statement(s); } 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”); }

43

44 // Java0517.java // This program displays 40 identical lines very inefficiently // with 40 separate println statements. public class Java0517 { public static void main(String args[]) { System.out.println("\nJAVA0517.JAVA\n"); System.out.println("Eat at Joe's friendly diner for the best lunch value"); ::::::::::::::::

45 // Java0518.java // This program displays 40 identical lines efficiently // with one println statement and a loop structure. public class Java0518 { public static void main(String args[]) { System.out.println("\nJAVA0518.JAVA\n"); int k; for (k = 1; k <= 40; k++) System.out.println("Eat at Joe's friendly diner for the best lunch value"); }

46 // Java0518.java // This program displays 40 identical lines efficiently // with one println statement and a loop structure. public class Java0518 { public static void main(String args[]) { System.out.println("\nJAVA0518.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 ).

47 // Java0518.java // This program displays 40 identical lines efficiently // with one println statement and a loop structure. public class Java0518 { public static void main(String args[]) { System.out.println("\nJAVA0518.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 3 indicates what the counter counts by. ++ means count by 1.

48 // Java0518.java // This program displays 40 identical lines efficiently // with one println statement and a loop structure. public class Java0518 { public static void main(String args[]) { System.out.println("\nJAVA0518.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 3 indicates what the counter counts by. ++ means count by 1. Part 2 is a condition. As long as it is true the loop will keep repeating.

49 // Java0519.java // This program displays consecutive numbers 1 through 15. // It also shows how the loop control variable may be // defined inside the program statement. public class Java0519 { public static void main(String args[]) { System.out.println("\nJAVA0519.JAVA\n"); for (int k = 1; k <= 15; k++) System.out.print(k + " "); System.out.println(); }

50 // Java0520.java // This program demonstrates how to use block structure // with a loop control structure. public class Java0520 { public static void main(String args[]) { System.out.println("\nJAVA0520.JAVA\n"); int k; for (k = 1; k <= 10; k++) { System.out.println("####################################"); System.out.println("Line Number " + k); } System.out.println(); }

51 // Java0521.java // This program displays various counting schemes. // It also demonstrates the versatility of the loop. public class Java0521 { public static void main(String args[]) { System.out.println("\nJAVA0521.JAVA\n"); for (int p = 1; p <= 15; p++) System.out.print(p + " "); System.out.println(); for (int q = 1; q <= 15; q+=3) System.out.print(q + " "); System.out.println(); for (int r = 15; r >= 1; r--) System.out.print(r + " "); System.out.println(); for (double s = 0; s <= 3; s+=0.5) System.out.print(s + " "); System.out.println(); 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 3 rd 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.

52 Repetion Control Structures With for 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. for loop syntax: The for loop has three distinct parts: Part1 initializes the Loop Control Variable (LCV). Part2 sets the exit condition for the loop. Part3 determines how the LCV changes. for (Part1; Part2; Part3) loop body; for (k = 1; k <= 10; k++) System.out.println("Java is 10 times more fun");

53

54 // Java0522.java // This program is supposed to keep repeating until a correct PIN# of 5678 is entered. // The program does not work because the loop is used in an inappropriate manner. // The loop is meant for "fixed" repetition. // Entering a PIN# is an example of "conditional" repetition. public class Java0522 { public static void main(String args[]) { System.out.println("\nJAVA0522.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("Incorrect Password. Try Again."); } System.out.println("\nYou are now logged in. Welcome to the program."); }

55 // Java0523.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 Java0523 { public static void main(String args[]) { System.out.println("\nJAVA0523.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("Incorrect Password. Try Again."); } System.out.println("\nYou are now logged in. Welcome to the program."); }

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

57 Repetion Control Structures With while while loop syntax: initialize condition variable while(condition is true) loop body alter condition variable in loop body x = 0; // initialize condition variable while(x < 10) { x++; // alter condition variable System.out.println("x = " + x); }

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

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

60 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 then 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.

61

62 // Java0524.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 Java0524 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; }

63 // Java0525.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 Java0525 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; }

64 // Java0526.java // This program shows how a control structure can be used with graphics. // This program draws diagonal lines, because all 4 variables -- x1, y1, y2, y2 -- change. import java.awt.*; import java.applet.*; public class Java0526 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; }

65 // Java0527.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 Java0527 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; }

66 // Java0528.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 Java0528 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 "// Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates."

Similar presentations


Ads by Google